"use client";
import T from "../../@ungap/structured-clone/esm/index.js";
import { htmlVoidElements as L } from "../../html-void-elements/index.js";
import { Parser as g } from "../../parse5/dist/parser/index.js";
import { getTagID as y } from "../../parse5/dist/common/html.js";
import "../../parse5/dist/serializer/index.js";
import "../../parse5/dist/common/error-codes.js";
import "../../parse5/dist/common/foreign-content.js";
import { TokenType as p } from "../../parse5/dist/common/token.js";
import { TokenizerMode as i } from "../../parse5/dist/tokenizer/index.js";
import { webNamespaces as f } from "../../web-namespaces/index.js";
import { zwitch as N } from "../../zwitch/index.js";
import { pointStart as a, pointEnd as C } from "../../unist-util-position/lib/index.js";
import { fromParse5 as v } from "../../hast-util-from-parse5/lib/index.js";
import { visit as I } from "../../unist-util-visit/lib/index.js";
import { toParse5 as S } from "../../hast-util-to-parse5/lib/index.js";
const P = /<(\/?)(iframe|noembed|noframes|plaintext|script|style|textarea|title|xmp)(?=[\t\n\f\r />])/gi, _ = /* @__PURE__ */ new Set([
  "mdxFlowExpression",
  "mdxJsxFlowElement",
  "mdxJsxTextElement",
  "mdxTextExpression",
  "mdxjsEsm"
]), z = { sourceCodeLocationInfo: !0, scriptingEnabled: !1 };
function D(r, e) {
  const n = B(r), o = N("type", {
    handlers: { root: R, element: b, text: O, comment: x, doctype: M, raw: X },
    unknown: W
  }), t = {
    parser: n ? new g(z) : g.getFragmentParser(void 0, z),
    handle(m) {
      o(m, t);
    },
    stitches: !1,
    options: e || {}
  };
  o(r, t), c(t, a());
  const u = n ? t.parser.document : t.parser.getFragment(), s = v(u, {
    // To do: support `space`?
    file: t.options.file
  });
  return t.stitches && I(s, "comment", function(m, k, h) {
    const d = (
      /** @type {Stitch} */
      /** @type {unknown} */
      m
    );
    if (d.value.stitch && h && k !== void 0) {
      const A = h.children;
      return A[k] = d.value.stitch, k;
    }
  }), s.type === "root" && s.children.length === 1 && s.children[0].type === r.type ? s.children[0] : s;
}
function w(r, e) {
  let n = -1;
  if (r)
    for (; ++n < r.length; )
      e.handle(r[n]);
}
function R(r, e) {
  w(r.children, e);
}
function b(r, e) {
  G(r, e), w(r.children, e), j(r, e);
}
function O(r, e) {
  e.parser.tokenizer.state > 4 && (e.parser.tokenizer.state = 0);
  const n = {
    type: p.CHARACTER,
    chars: r.value,
    location: l(r)
  };
  c(e, a(r)), e.parser.currentToken = n, e.parser._processToken(e.parser.currentToken);
}
function M(r, e) {
  const n = {
    type: p.DOCTYPE,
    name: "html",
    forceQuirks: !1,
    publicId: "",
    systemId: "",
    location: l(r)
  };
  c(e, a(r)), e.parser.currentToken = n, e.parser._processToken(e.parser.currentToken);
}
function F(r, e) {
  e.stitches = !0;
  const n = H(r);
  if ("children" in r && "children" in n) {
    const o = (
      /** @type {Root} */
      D({ type: "root", children: r.children }, e.options)
    );
    n.children = o.children;
  }
  x({ type: "comment", value: { stitch: n } }, e);
}
function x(r, e) {
  const n = r.value, o = {
    type: p.COMMENT,
    data: n,
    location: l(r)
  };
  c(e, a(r)), e.parser.currentToken = o, e.parser._processToken(e.parser.currentToken);
}
function X(r, e) {
  if (e.parser.tokenizer.preprocessor.html = "", e.parser.tokenizer.preprocessor.pos = -1, e.parser.tokenizer.preprocessor.lastGapPos = -2, e.parser.tokenizer.preprocessor.gapStack = [], e.parser.tokenizer.preprocessor.skipNextNewLine = !1, e.parser.tokenizer.preprocessor.lastChunkWritten = !1, e.parser.tokenizer.preprocessor.endOfChunkHit = !1, e.parser.tokenizer.preprocessor.isEol = !1, E(e, a(r)), e.parser.tokenizer.write(
    e.options.tagfilter ? r.value.replace(P, "&lt;$1$2") : r.value,
    !1
  ), e.parser.tokenizer._runParsingLoop(), e.parser.tokenizer.state === 72 || // @ts-expect-error: removed.
  e.parser.tokenizer.state === 78) {
    e.parser.tokenizer.preprocessor.lastChunkWritten = !0;
    const n = e.parser.tokenizer._consume();
    e.parser.tokenizer._callState(n);
  }
}
function W(r, e) {
  const n = (
    /** @type {Nodes} */
    r
  );
  if (e.options.passThrough && e.options.passThrough.includes(n.type))
    F(n, e);
  else {
    let o = "";
    throw _.has(n.type) && (o = ". It looks like you are using MDX nodes with `hast-util-raw` (or `rehype-raw`). If you use this because you are using remark or rehype plugins that inject `'html'` nodes, then please raise an issue with that plugin, as its a bad and slow idea. If you use this because you are using markdown syntax, then you have to configure this utility (or plugin) to pass through these nodes (see `passThrough` in docs), but you can also migrate to use the MDX syntax"), new Error("Cannot compile `" + n.type + "` node" + o);
  }
}
function c(r, e) {
  E(r, e);
  const n = r.parser.tokenizer.currentCharacterToken;
  n && n.location && (n.location.endLine = r.parser.tokenizer.preprocessor.line, n.location.endCol = r.parser.tokenizer.preprocessor.col + 1, n.location.endOffset = r.parser.tokenizer.preprocessor.offset + 1, r.parser.currentToken = n, r.parser._processToken(r.parser.currentToken)), r.parser.tokenizer.paused = !1, r.parser.tokenizer.inLoop = !1, r.parser.tokenizer.active = !1, r.parser.tokenizer.returnState = i.DATA, r.parser.tokenizer.charRefCode = -1, r.parser.tokenizer.consumedAfterSnapshot = -1, r.parser.tokenizer.currentLocation = null, r.parser.tokenizer.currentCharacterToken = null, r.parser.tokenizer.currentToken = null, r.parser.tokenizer.currentAttr = { name: "", value: "" };
}
function E(r, e) {
  if (e && e.offset !== void 0) {
    const n = {
      startLine: e.line,
      startCol: e.column,
      startOffset: e.offset,
      endLine: -1,
      endCol: -1,
      endOffset: -1
    };
    r.parser.tokenizer.preprocessor.lineStartPos = -e.column + 1, r.parser.tokenizer.preprocessor.droppedBufferSize = e.offset, r.parser.tokenizer.preprocessor.line = e.line, r.parser.tokenizer.currentLocation = n;
  }
}
function G(r, e) {
  const n = r.tagName.toLowerCase();
  if (e.parser.tokenizer.state === i.PLAINTEXT) return;
  c(e, a(r));
  const o = e.parser.openElements.current;
  let t = "namespaceURI" in o ? o.namespaceURI : f.html;
  t === f.html && n === "svg" && (t = f.svg);
  const u = S(
    // Shallow clone to not delve into `children`: we only need the attributes.
    { ...r, children: [] },
    { space: t === f.svg ? "svg" : "html" }
  ), s = {
    type: p.START_TAG,
    tagName: n,
    tagID: y(n),
    // We always send start and end tags.
    selfClosing: !1,
    ackSelfClosing: !1,
    // Always element.
    /* c8 ignore next */
    attrs: "attrs" in u ? u.attrs : [],
    location: l(r)
  };
  e.parser.currentToken = s, e.parser._processToken(e.parser.currentToken), e.parser.tokenizer.lastStartTagName = n;
}
function j(r, e) {
  const n = r.tagName.toLowerCase();
  if (!e.parser.tokenizer.inForeignNode && L.includes(n) || e.parser.tokenizer.state === i.PLAINTEXT) return;
  c(e, C(r));
  const o = {
    type: p.END_TAG,
    tagName: n,
    tagID: y(n),
    selfClosing: !1,
    ackSelfClosing: !1,
    attrs: [],
    location: l(r)
  };
  e.parser.currentToken = o, e.parser._processToken(e.parser.currentToken), // Current element is closed.
  n === e.parser.tokenizer.lastStartTagName && // `<textarea>` and `<title>`
  (e.parser.tokenizer.state === i.RCDATA || // `<iframe>`, `<noembed>`, `<noframes>`, `<style>`, `<xmp>`
  e.parser.tokenizer.state === i.RAWTEXT || // `<script>`
  e.parser.tokenizer.state === i.SCRIPT_DATA) && (e.parser.tokenizer.state = i.DATA);
}
function B(r) {
  const e = r.type === "root" ? r.children[0] : r;
  return !!(e && (e.type === "doctype" || e.type === "element" && e.tagName.toLowerCase() === "html"));
}
function l(r) {
  const e = a(r) || {
    line: void 0,
    column: void 0,
    offset: void 0
  }, n = C(r) || {
    line: void 0,
    column: void 0,
    offset: void 0
  };
  return {
    startLine: e.line,
    startCol: e.column,
    startOffset: e.offset,
    endLine: n.line,
    endCol: n.column,
    endOffset: n.offset
  };
}
function H(r) {
  return "children" in r ? T({ ...r, children: [] }) : T(r);
}
export {
  D as raw
};
