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

type Delimiter = string | [begin: string, end: string];

function getBeginToken(delimiter: Delimiter): string {
  return Array.isArray(delimiter) ? delimiter[0] : delimiter;
}

function getEndToken(delimiter: Delimiter): string {
  return Array.isArray(delimiter) ? delimiter[1] : delimiter;
}

function createRegExps(delimiters: Delimiter[]): [RegExp, RegExp] {
  const beginPattern = "(" + delimiters.map(getBeginToken).join("|") + ")";
  const pattern = "^(" +
    "\\ufeff?" + // Maybe byte order mark
    beginPattern +
    "$([\\s\\S]+?)" +
    "^(?:" + delimiters.map(getEndToken).join("|") + ")\\s*" +
    "$" +
    (globalThis?.Deno?.build?.os === "windows" ? "\\r?" : "") +
    "(?:\\n)?)";

  return [
    new RegExp("^" + beginPattern + "$", "im"),
    new RegExp(pattern, "im"),
  ];
}

const [RECOGNIZE_YAML_REGEXP, EXTRACT_YAML_REGEXP] = createRegExps(
  [
    ["---yaml", "---"],
    "= yaml =",
    "---",
  ],
);
const [RECOGNIZE_TOML_REGEXP, EXTRACT_TOML_REGEXP] = createRegExps(
  [
    ["---toml", "---"],
    "\\+\\+\\+",
    "= toml =",
  ],
);
const [RECOGNIZE_JSON_REGEXP, EXTRACT_JSON_REGEXP] = createRegExps(
  [
    ["---json", "---"],
    "= json =",
  ],
);

export const RECOGNIZE_REGEXP_MAP = {
  yaml: RECOGNIZE_YAML_REGEXP,
  toml: RECOGNIZE_TOML_REGEXP,
  json: RECOGNIZE_JSON_REGEXP,
} as const;

export const EXTRACT_REGEXP_MAP = {
  yaml: EXTRACT_YAML_REGEXP,
  toml: EXTRACT_TOML_REGEXP,
  json: EXTRACT_JSON_REGEXP,
} as const;

// denoCacheMetadata={"headers":{"cf-ray":"a223e657fb247db6-LAX","date":"Tue, 28 Jul 2026 12:31:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=8y9xkIxTsQMFik56fWuvHBcBqC3qZU4p%2Ft5jaQl41GwfA%2Batm8oKFimiNzxcsGxL%2BSJMuESvnjfmzRRrdOq33wq9eaeuKbv2ztiLbzCyqqq0IqX4bFB2xDI%3D\"}]}","cache-control":"public, max-age=31536000, immutable","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';","content-length":"1544","x-robots-tag":"noindex","access-control-expose-headers":"*","access-control-allow-origin":"*","cross-origin-resource-policy":"cross-origin","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","etag":"\"5e14152b3f07be742c9a41c6a6a5769e\"","x-content-type-options":"nosniff","x-jsr-backend":"modules","set-cookie":"__cf_bm=gXpISKbKTQywZ_YhT.Wa9szpO7jasYX1aUuUDfDX7tI-1785241875.2030826-1.0.1.1-Bw6PRJahRZ6_1hxy8ezIRDpHcElnmpV4IFHsgnPqVxND7O3aDJiaw29xGXSx9qp9pb55rNsHYn8hJGtdmm4ulPHIx2tsqGw9QzZYsnUPDUdQGig3MjMTb5MGRUp6BcoA; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","content-type":"text/typescript","server":"cloudflare"},"url":"https://jsr.io/@std/front-matter/0.224.3/_formats.ts","time":1785241875}