import { D as SKIP, O as visitParents, _ as s, g as h$1, h as webNamespaces, k as convert, n as longestStreak, t as factorySpace, u as markdownLineEnding, w as ok } from "./micromark-factory-space-CwHmg6iz.js";
import { c as katex } from "./katex-C_XRmjAP.js";
function fromDom(o, F) {
  return transform(o, F || {}) || {
    type: "root",
    children: []
  };
}
function transform(o, F) {
  let I = one(o, F);
  return I && F.afterTransform && F.afterTransform(o, I), I;
}
function one(o, F) {
  switch (o.nodeType) {
    case 1:
      return element$1(o, F);
    case 3:
      return text(o);
    case 8:
      return comment(o);
    case 9:
      return root(o, F);
    case 10:
      return doctype();
    case 11:
      return root(o, F);
    default:
      return;
  }
}
function root(o, F) {
  return {
    type: "root",
    children: all(o, F)
  };
}
function doctype() {
  return { type: "doctype" };
}
function text(o) {
  return {
    type: "text",
    value: o.nodeValue || ""
  };
}
function comment(o) {
  return {
    type: "comment",
    value: o.nodeValue || ""
  };
}
function element$1(o, F) {
  let z = o.namespaceURI, B = z === webNamespaces.svg ? s : h$1, V = z === webNamespaces.html ? o.tagName.toLowerCase() : o.tagName, H = z === webNamespaces.html && V === "template" ? o.content : o, U = o.getAttributeNames(), W = {}, G = -1;
  for (; ++G < U.length; ) W[U[G]] = o.getAttribute(U[G]) || "";
  return B(V, W, all(H, F));
}
function all(o, F) {
  let I = o.childNodes, L = [], R = -1;
  for (; ++R < I.length; ) {
    let o2 = transform(I[R], F);
    o2 !== void 0 && L.push(o2);
  }
  return L;
}
var parser = new DOMParser();
function fromHtmlIsomorphic(o, F) {
  return fromDom((F == null ? void 0 : F.fragment) ? parseFragment(o) : parser.parseFromString(o, "text/html"));
}
function parseFragment(o) {
  let F = document.createElement("template");
  return F.innerHTML = o, F.content;
}
const findAfter = (function(o, F, I) {
  let L = convert(I);
  if (!o || !o.type || !o.children) throw Error("Expected parent node");
  if (typeof F == "number") {
    if (F < 0 || F === Infinity) throw Error("Expected positive finite number as index");
  } else if (F = o.children.indexOf(F), F < 0) throw Error("Expected child node or index");
  for (; ++F < o.children.length; ) if (L(o.children[F], F, o)) return o.children[F];
}), convertElement = (function(o) {
  if (o == null) return element;
  if (typeof o == "string") return tagNameFactory(o);
  if (typeof o == "object") return anyFactory(o);
  if (typeof o == "function") return castFactory(o);
  throw Error("Expected function, string, or array as `test`");
});
function anyFactory(o) {
  let F = [], I = -1;
  for (; ++I < o.length; ) F[I] = convertElement(o[I]);
  return castFactory(L);
  function L(...o2) {
    let I2 = -1;
    for (; ++I2 < F.length; ) if (F[I2].apply(this, o2)) return true;
    return false;
  }
}
function tagNameFactory(o) {
  return castFactory(F);
  function F(F2) {
    return F2.tagName === o;
  }
}
function castFactory(o) {
  return F;
  function F(F2, I, L) {
    return !!(looksLikeAnElement(F2) && o.call(this, F2, typeof I == "number" ? I : void 0, L || void 0));
  }
}
function element(o) {
  return !!(o && typeof o == "object" && "type" in o && o.type === "element" && "tagName" in o && typeof o.tagName == "string");
}
function looksLikeAnElement(o) {
  return typeof o == "object" && !!o && "type" in o && "tagName" in o;
}
var searchLineFeeds = /\n/g, searchTabOrSpaces = /[\t ]+/g, br = convertElement("br"), cell = convertElement(isCell), p = convertElement("p"), row = convertElement("tr"), notRendered = convertElement([
  "datalist",
  "head",
  "noembed",
  "noframes",
  "noscript",
  "rp",
  "script",
  "style",
  "template",
  "title",
  hidden,
  closedDialog
]), blockOrCaption = convertElement(/* @__PURE__ */ "address.article.aside.blockquote.body.caption.center.dd.dialog.dir.dl.dt.div.figure.figcaption.footer.form,.h1.h2.h3.h4.h5.h6.header.hgroup.hr.html.legend.li.listing.main.menu.nav.ol.p.plaintext.pre.section.ul.xmp".split("."));
function toText(o, F) {
  let I = F || {}, L = "children" in o ? o.children : [], R = blockOrCaption(o), z = inferWhitespace(o, {
    whitespace: I.whitespace || "normal",
    breakBefore: false,
    breakAfter: false
  }), B = [];
  (o.type === "text" || o.type === "comment") && B.push(...collectText(o, {
    whitespace: z,
    breakBefore: true,
    breakAfter: true
  }));
  let V = -1;
  for (; ++V < L.length; ) B.push(...renderedTextCollection(L[V], o, {
    whitespace: z,
    breakBefore: V ? void 0 : R,
    breakAfter: V < L.length - 1 ? br(L[V + 1]) : R
  }));
  let H = [], U;
  for (V = -1; ++V < B.length; ) {
    let o2 = B[V];
    typeof o2 == "number" ? U !== void 0 && o2 > U && (U = o2) : o2 && (U !== void 0 && U > -1 && H.push("\n".repeat(U) || " "), U = -1, H.push(o2));
  }
  return H.join("");
}
function renderedTextCollection(o, F, I) {
  return o.type === "element" ? collectElement(o, F, I) : o.type === "text" ? I.whitespace === "normal" ? collectText(o, I) : collectPreText(o) : [];
}
function collectElement(o, F, I) {
  let L = inferWhitespace(o, I), R = o.children || [], z = -1, B = [];
  if (notRendered(o)) return B;
  let V, H;
  for (br(o) || row(o) && findAfter(F, o, row) ? H = "\n" : p(o) ? (V = 2, H = 2) : blockOrCaption(o) && (V = 1, H = 1); ++z < R.length; ) B = B.concat(renderedTextCollection(R[z], o, {
    whitespace: L,
    breakBefore: z ? void 0 : V,
    breakAfter: z < R.length - 1 ? br(R[z + 1]) : H
  }));
  return cell(o) && findAfter(F, o, cell) && B.push("	"), V && B.unshift(V), H && B.push(H), B;
}
function collectText(o, F) {
  let I = String(o.value), L = [], R = [], z = 0;
  for (; z <= I.length; ) {
    searchLineFeeds.lastIndex = z;
    let o2 = searchLineFeeds.exec(I), R2 = o2 && "index" in o2 ? o2.index : I.length;
    L.push(trimAndCollapseSpacesAndTabs(I.slice(z, R2).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""), z === 0 ? F.breakBefore : true, R2 === I.length ? F.breakAfter : true)), z = R2 + 1;
  }
  let B = -1, V;
  for (; ++B < L.length; ) L[B].charCodeAt(L[B].length - 1) === 8203 || B < L.length - 1 && L[B + 1].charCodeAt(0) === 8203 ? (R.push(L[B]), V = void 0) : L[B] ? (typeof V == "number" && R.push(V), R.push(L[B]), V = 0) : (B === 0 || B === L.length - 1) && R.push(0);
  return R;
}
function collectPreText(o) {
  return [String(o.value)];
}
function trimAndCollapseSpacesAndTabs(o, F, I) {
  let L = [], R = 0, z;
  for (; R < o.length; ) {
    searchTabOrSpaces.lastIndex = R;
    let I2 = searchTabOrSpaces.exec(o);
    z = I2 ? I2.index : o.length, !R && !z && I2 && !F && L.push(""), R !== z && L.push(o.slice(R, z)), R = I2 ? z + I2[0].length : z;
  }
  return R !== z && !I && L.push(""), L.join(" ");
}
function inferWhitespace(o, F) {
  if (o.type === "element") {
    let I = o.properties || {};
    switch (o.tagName) {
      case "listing":
      case "plaintext":
      case "xmp":
        return "pre";
      case "nobr":
        return "nowrap";
      case "pre":
        return I.wrap ? "pre-wrap" : "pre";
      case "td":
      case "th":
        return I.noWrap ? "nowrap" : F.whitespace;
      case "textarea":
        return "pre-wrap";
      default:
    }
  }
  return F.whitespace;
}
function hidden(o) {
  return !!(o.properties || {}).hidden;
}
function isCell(o) {
  return o.tagName === "td" || o.tagName === "th";
}
function closedDialog(o) {
  return o.tagName === "dialog" && !(o.properties || {}).open;
}
var emptyOptions$1 = {}, emptyClasses = [];
function rehypeKatex(I) {
  let L = I || emptyOptions$1;
  return function(I2, R) {
    visitParents(I2, "element", function(F, I3) {
      let z = Array.isArray(F.properties.className) ? F.properties.className : emptyClasses, B = z.includes("language-math"), V = z.includes("math-display"), H = z.includes("math-inline"), U = V;
      if (!B && !V && !H) return;
      let G = I3[I3.length - 1], K = F;
      if (F.tagName === "code" && B && G && G.type === "element" && G.tagName === "pre" && (K = G, G = I3[I3.length - 2], U = true), !G) return;
      let q = toText(K, { whitespace: "pre" }), J;
      try {
        J = katex.renderToString(q, {
          ...L,
          displayMode: U,
          throwOnError: true
        });
      } catch (o) {
        let z2 = o, B2 = z2.name.toLowerCase();
        R.message("Could not render math with KaTeX", {
          ancestors: [...I3, F],
          cause: z2,
          place: F.position,
          ruleId: B2,
          source: "rehype-katex"
        });
        try {
          J = katex.renderToString(q, {
            ...L,
            displayMode: U,
            strict: "ignore",
            throwOnError: false
          });
        } catch {
          J = [{
            type: "element",
            tagName: "span",
            properties: {
              className: ["katex-error"],
              style: "color:" + (L.errorColor || "#cc0000"),
              title: String(o)
            },
            children: [{
              type: "text",
              value: q
            }]
          }];
        }
      }
      typeof J == "string" && (J = fromHtmlIsomorphic(J, { fragment: true }).children);
      let Y = G.children.indexOf(K);
      return G.children.splice(Y, 1, ...J), SKIP;
    });
  };
}
function mathFromMarkdown() {
  return {
    enter: {
      mathFlow: o,
      mathFlowFenceMeta: F,
      mathText: z
    },
    exit: {
      mathFlow: R,
      mathFlowFence: L,
      mathFlowFenceMeta: I,
      mathFlowValue: V,
      mathText: B,
      mathTextData: V
    }
  };
  function o(o2) {
    this.enter({
      type: "math",
      meta: null,
      value: "",
      data: {
        hName: "pre",
        hChildren: [{
          type: "element",
          tagName: "code",
          properties: { className: ["language-math", "math-display"] },
          children: []
        }]
      }
    }, o2);
  }
  function F() {
    this.buffer();
  }
  function I() {
    let o2 = this.resume(), F2 = this.stack[this.stack.length - 1];
    F2.type, F2.meta = o2;
  }
  function L() {
    this.data.mathFlowInside || (this.buffer(), this.data.mathFlowInside = true);
  }
  function R(o2) {
    let F2 = this.resume().replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, ""), I2 = this.stack[this.stack.length - 1];
    I2.type, this.exit(o2), I2.value = F2;
    let L2 = I2.data.hChildren[0];
    L2.type, L2.tagName, L2.children.push({
      type: "text",
      value: F2
    }), this.data.mathFlowInside = void 0;
  }
  function z(o2) {
    this.enter({
      type: "inlineMath",
      value: "",
      data: {
        hName: "code",
        hProperties: { className: ["language-math", "math-inline"] },
        hChildren: []
      }
    }, o2), this.buffer();
  }
  function B(o2) {
    let F2 = this.resume(), I2 = this.stack[this.stack.length - 1];
    I2.type, this.exit(o2), I2.value = F2, I2.data.hChildren.push({
      type: "text",
      value: F2
    });
  }
  function V(o2) {
    this.config.enter.data.call(this, o2), this.config.exit.data.call(this, o2);
  }
}
function mathToMarkdown(o) {
  let F = (o || {}).singleDollarTextMath;
  return F ?? (F = true), L.peek = R, {
    unsafe: [
      {
        character: "\r",
        inConstruct: "mathFlowMeta"
      },
      {
        character: "\n",
        inConstruct: "mathFlowMeta"
      },
      {
        character: "$",
        after: F ? void 0 : "\\$",
        inConstruct: "phrasing"
      },
      {
        character: "$",
        inConstruct: "mathFlowMeta"
      },
      {
        atBreak: true,
        character: "$",
        after: "\\$"
      }
    ],
    handlers: {
      math: I,
      inlineMath: L
    }
  };
  function I(o2, F2, I2, L2) {
    let R2 = o2.value || "", z = I2.createTracker(L2), V = "$".repeat(Math.max(longestStreak(R2, "$") + 1, 2)), H = I2.enter("mathFlow"), U = z.move(V);
    if (o2.meta) {
      let F3 = I2.enter("mathFlowMeta");
      U += z.move(I2.safe(o2.meta, {
        after: "\n",
        before: U,
        encode: ["$"],
        ...z.current()
      })), F3();
    }
    return U += z.move("\n"), R2 && (U += z.move(R2 + "\n")), U += z.move(V), H(), U;
  }
  function L(o2, I2, L2) {
    let R2 = o2.value || "", z = 1;
    for (F || z++; (/* @__PURE__ */ RegExp("(^|[^$])" + "\\$".repeat(z) + "([^$]|$)")).test(R2); ) z++;
    let B = "$".repeat(z);
    /[^ \r\n]/.test(R2) && (/^[ \r\n]/.test(R2) && /[ \r\n]$/.test(R2) || /^\$|\$$/.test(R2)) && (R2 = " " + R2 + " ");
    let V = -1;
    for (; ++V < L2.unsafe.length; ) {
      let o3 = L2.unsafe[V];
      if (!o3.atBreak) continue;
      let F2 = L2.compilePattern(o3), I3;
      for (; I3 = F2.exec(R2); ) {
        let o4 = I3.index;
        R2.codePointAt(o4) === 10 && R2.codePointAt(o4 - 1) === 13 && o4--, R2 = R2.slice(0, o4) + " " + R2.slice(I3.index + 1);
      }
    }
    return B + R2 + B;
  }
  function R() {
    return "$";
  }
}
const mathFlow = {
  tokenize: tokenizeMathFenced,
  concrete: true,
  name: "mathFlow"
};
var nonLazyContinuation = {
  tokenize: tokenizeNonLazyContinuation,
  partial: true
};
function tokenizeMathFenced(o, F, I) {
  let L = this, R = L.events[L.events.length - 1], z = R && R[1].type === "linePrefix" ? R[2].sliceSerialize(R[1], true).length : 0, B = 0;
  return U;
  function U(F2) {
    return o.enter("mathFlow"), o.enter("mathFlowFence"), o.enter("mathFlowFenceSequence"), W(F2);
  }
  function W(F2) {
    return F2 === 36 ? (o.consume(F2), B++, W) : B < 2 ? I(F2) : (o.exit("mathFlowFenceSequence"), factorySpace(o, G, "whitespace")(F2));
  }
  function G(F2) {
    return F2 === null || markdownLineEnding(F2) ? q(F2) : (o.enter("mathFlowFenceMeta"), o.enter("chunkString", { contentType: "string" }), K(F2));
  }
  function K(F2) {
    return F2 === null || markdownLineEnding(F2) ? (o.exit("chunkString"), o.exit("mathFlowFenceMeta"), q(F2)) : F2 === 36 ? I(F2) : (o.consume(F2), K);
  }
  function q(I2) {
    return o.exit("mathFlowFence"), L.interrupt ? F(I2) : o.attempt(nonLazyContinuation, J, Q)(I2);
  }
  function J(F2) {
    return o.attempt({
      tokenize: $,
      partial: true
    }, Q, Y)(F2);
  }
  function Y(F2) {
    return (z ? factorySpace(o, X, "linePrefix", z + 1) : X)(F2);
  }
  function X(F2) {
    return F2 === null ? Q(F2) : markdownLineEnding(F2) ? o.attempt(nonLazyContinuation, J, Q)(F2) : (o.enter("mathFlowValue"), Z(F2));
  }
  function Z(F2) {
    return F2 === null || markdownLineEnding(F2) ? (o.exit("mathFlowValue"), X(F2)) : (o.consume(F2), Z);
  }
  function Q(I2) {
    return o.exit("mathFlow"), F(I2);
  }
  function $(o2, F2, I2) {
    let R2 = 0;
    return factorySpace(o2, z2, "linePrefix", L.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4);
    function z2(F3) {
      return o2.enter("mathFlowFence"), o2.enter("mathFlowFenceSequence"), U2(F3);
    }
    function U2(F3) {
      return F3 === 36 ? (R2++, o2.consume(F3), U2) : R2 < B ? I2(F3) : (o2.exit("mathFlowFenceSequence"), factorySpace(o2, W2, "whitespace")(F3));
    }
    function W2(L2) {
      return L2 === null || markdownLineEnding(L2) ? (o2.exit("mathFlowFence"), F2(L2)) : I2(L2);
    }
  }
}
function tokenizeNonLazyContinuation(o, F, I) {
  let L = this;
  return R;
  function R(I2) {
    return I2 === null ? F(I2) : (o.enter("lineEnding"), o.consume(I2), o.exit("lineEnding"), z);
  }
  function z(o2) {
    return L.parser.lazy[L.now().line] ? I(o2) : F(o2);
  }
}
function mathText(o) {
  let F = (o || {}).singleDollarTextMath;
  return F ?? (F = true), {
    tokenize: I,
    resolve: resolveMathText,
    previous,
    name: "mathText"
  };
  function I(o2, I2, L) {
    let R = 0, z, B;
    return V;
    function V(F2) {
      return o2.enter("mathText"), o2.enter("mathTextSequence"), U(F2);
    }
    function U(I3) {
      return I3 === 36 ? (o2.consume(I3), R++, U) : R < 2 && !F ? L(I3) : (o2.exit("mathTextSequence"), W(I3));
    }
    function W(F2) {
      return F2 === null ? L(F2) : F2 === 36 ? (B = o2.enter("mathTextSequence"), z = 0, K(F2)) : F2 === 32 ? (o2.enter("space"), o2.consume(F2), o2.exit("space"), W) : markdownLineEnding(F2) ? (o2.enter("lineEnding"), o2.consume(F2), o2.exit("lineEnding"), W) : (o2.enter("mathTextData"), G(F2));
    }
    function G(F2) {
      return F2 === null || F2 === 32 || F2 === 36 || markdownLineEnding(F2) ? (o2.exit("mathTextData"), W(F2)) : (o2.consume(F2), G);
    }
    function K(F2) {
      return F2 === 36 ? (o2.consume(F2), z++, K) : z === R ? (o2.exit("mathTextSequence"), o2.exit("mathText"), I2(F2)) : (B.type = "mathTextData", G(F2));
    }
  }
}
function resolveMathText(o) {
  let F = o.length - 4, I = 3, L, R;
  if ((o[I][1].type === "lineEnding" || o[I][1].type === "space") && (o[F][1].type === "lineEnding" || o[F][1].type === "space")) {
    for (L = I; ++L < F; ) if (o[L][1].type === "mathTextData") {
      o[F][1].type = "mathTextPadding", o[I][1].type = "mathTextPadding", I += 2, F -= 2;
      break;
    }
  }
  for (L = I - 1, F++; ++L <= F; ) R === void 0 ? L !== F && o[L][1].type !== "lineEnding" && (R = L) : (L === F || o[L][1].type === "lineEnding") && (o[R][1].type = "mathTextData", L !== R + 2 && (o[R][1].end = o[L - 1][1].end, o.splice(R + 2, L - R - 2), F -= L - R - 2, L = R + 2), R = void 0);
  return o;
}
function previous(o) {
  return o !== 36 || this.events[this.events.length - 1][1].type === "characterEscape";
}
function math(o) {
  return {
    flow: { 36: mathFlow },
    text: { 36: mathText(o) }
  };
}
var emptyOptions = {};
function remarkMath(o) {
  let F = this, I = o || emptyOptions, L = F.data(), R = L.micromarkExtensions || (L.micromarkExtensions = []), z = L.fromMarkdownExtensions || (L.fromMarkdownExtensions = []), B = L.toMarkdownExtensions || (L.toMarkdownExtensions = []);
  R.push(math(I)), z.push(mathFromMarkdown()), B.push(mathToMarkdown(I));
}
function g(o = {}) {
  return {
    name: "katex",
    type: "math",
    remarkPlugin: [remarkMath, { singleDollarTextMath: o.singleDollarTextMath ?? false }],
    rehypePlugin: [rehypeKatex, { errorColor: o.errorColor ?? "var(--color-muted-foreground)" }],
    getStyles() {
      return "katex/dist/katex.min.css";
    }
  };
}
var h = g();
export {
  h as math
};
