// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { EXTRACT_REGEXP_MAP } from "./_formats.ts";
import type { Format } from "./_types.ts";

export type { Format };

/**
 * Tests if a string has valid front matter.
 * Supports {@link https://yaml.org | YAML}, {@link https://toml.io | TOML} and
 * {@link https://www.json.org/ | JSON}.
 *
 * @param str String to test.
 * @param formats A list of formats to test for. Defaults to all supported formats.
 * @returns `true` if the string has valid front matter, otherwise `false`.
 *
 * @example Test for valid YAML front matter
 * ```ts
 * import { test } from "@std/front-matter/test";
 * import { assert } from "@std/assert";
 *
 * const result = test(
 * `---
 * title: Three dashes marks the spot
 * ---
 * `);
 * assert(result);
 * ```
 *
 * @example Test for valid TOML front matter
 * ```ts
 * import { test } from "@std/front-matter/test";
 * import { assert } from "@std/assert";
 *
 * const result = test(
 * `---toml
 * title = 'Three dashes followed by format marks the spot'
 * ---
 * `);
 * assert(result);
 * ```
 *
 * @example Test for valid JSON front matter
 * ```ts
 * import { test } from "@std/front-matter/test";
 * import { assert } from "@std/assert";
 *
 * const result = test(
 * `---json
 * {"title": "Three dashes followed by format marks the spot"}
 * ---
 * `);
 * assert(result);
 * ```
 *
 * @example JSON front matter is not valid as YAML
 * ```ts
 * import { test } from "@std/front-matter/test";
 * import { assertFalse } from "@std/assert";
 *
 * const result = test(
 * `---json
 * {"title": "Three dashes followed by format marks the spot"}
 * ---
 * `, ["yaml"]);
 * assertFalse(result);
 * ```
 */
export function test(
  str: string,
  formats?: Format[],
): boolean {
  if (!formats) {
    formats = Object.keys(EXTRACT_REGEXP_MAP) as Format[];
  }

  for (const format of formats) {
    if (format === "unknown") {
      throw new TypeError("Unable to test for unknown front matter format");
    }

    const match = EXTRACT_REGEXP_MAP[format].exec(str);
    if (match?.index === 0) {
      return true;
    }
  }

  return false;
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cf-ray":"a223e656afc27db6-LAX","content-length":"2156","x-jsr-backend":"modules","access-control-expose-headers":"*","access-control-allow-origin":"*","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=2JrZHpBG1rz1OcVkCP554FeSVJzVyYCbh7zw37aAEyoq2U0pjJRkBJhrYmI%2B6vISPM3%2FQqx47A9%2FLYGVkMhnqPreqGOkqF2jFdYzrcC6ZTdi9kQRtX5T4hA%3D\"}]}","etag":"\"30608aa6e6090529b7bf270c39da7cd3\"","date":"Tue, 28 Jul 2026 12:31:15 GMT","cache-control":"public, max-age=31536000, immutable","vary":"Origin","content-type":"text/typescript","server":"cloudflare","set-cookie":"__cf_bm=xSZeOBOesaFwY_R0xRRWVKPrmLV9UtSasJR27rITy5o-1785241874.9942563-1.0.1.1-yPSCxWfAd98PeGvct8jg.G8GivlE7quX98jAB6pFdC7Y4fzfgNEEg4tneJKQtFFiYu3JhpKfX6Ze1Q7IGInFzj.J4J7sSDVaFT2f7rA4M.Aga9ob3mXgoVWMmFwFYNRm; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/front-matter/0.224.3/test.ts","time":1785241875}