"use client";
import { stringify as N } from "../../comma-separated-tokens/index.js";
import { ok as p } from "../../devlop/lib/default.js";
import { svg as f, html as h } from "../../property-information/index.js";
import { stringify as g } from "../../space-separated-tokens/index.js";
import { webNamespaces as l } from "../../web-namespaces/index.js";
import { zwitch as y } from "../../zwitch/index.js";
import { find as b } from "../../property-information/lib/find.js";
const x = {}, w = {}.hasOwnProperty, u = y("type", { handlers: { root: O, element: q, text: L, comment: S, doctype: C } });
function $(e, n) {
  const r = (n || x).space;
  return u(e, r === "svg" ? f : h);
}
function O(e, n) {
  const t = {
    nodeName: "#document",
    // @ts-expect-error: `parse5` uses enums, which are actually strings.
    mode: (e.data || {}).quirksMode ? "quirks" : "no-quirks",
    childNodes: []
  };
  return t.childNodes = a(e.children, t, n), i(e, t), t;
}
function k(e, n) {
  const t = { nodeName: "#document-fragment", childNodes: [] };
  return t.childNodes = a(e.children, t, n), i(e, t), t;
}
function C(e) {
  const n = {
    nodeName: "#documentType",
    name: "html",
    publicId: "",
    systemId: "",
    parentNode: null
  };
  return i(e, n), n;
}
function L(e) {
  const n = {
    nodeName: "#text",
    value: e.value,
    parentNode: null
  };
  return i(e, n), n;
}
function S(e) {
  const n = {
    nodeName: "#comment",
    data: e.value,
    parentNode: null
  };
  return i(e, n), n;
}
function q(e, n) {
  const t = n;
  let r = t;
  e.type === "element" && e.tagName.toLowerCase() === "svg" && t.space === "html" && (r = f);
  const s = [];
  let o;
  if (e.properties) {
    for (o in e.properties)
      if (o !== "children" && w.call(e.properties, o)) {
        const m = I(
          r,
          o,
          e.properties[o]
        );
        m && s.push(m);
      }
  }
  const d = r.space, c = {
    nodeName: e.tagName,
    tagName: e.tagName,
    attrs: s,
    // @ts-expect-error: `parse5` types are wrong.
    namespaceURI: l[d],
    childNodes: [],
    parentNode: null
  };
  return c.childNodes = a(e.children, c, r), i(e, c), e.tagName === "template" && e.content && (c.content = k(e.content, r)), c;
}
function I(e, n, t) {
  const r = b(e, n);
  if (t === !1 || t === null || t === void 0 || typeof t == "number" && Number.isNaN(t) || !t && r.boolean)
    return;
  Array.isArray(t) && (t = r.commaSeparated ? N(t) : g(t));
  const s = {
    name: r.attribute,
    value: t === !0 ? "" : String(t)
  };
  if (r.space && r.space !== "html" && r.space !== "svg") {
    const o = s.name.indexOf(":");
    o < 0 ? s.prefix = "" : (s.name = s.name.slice(o + 1), s.prefix = r.attribute.slice(0, o)), s.namespace = l[r.space];
  }
  return s;
}
function a(e, n, t) {
  let r = -1;
  const s = [];
  if (e)
    for (; ++r < e.length; ) {
      const o = u(e[r], t);
      o.parentNode = n, s.push(o);
    }
  return s;
}
function i(e, n) {
  const t = e.position;
  t && t.start && t.end && (p(typeof t.start.offset == "number"), p(typeof t.end.offset == "number"), n.sourceCodeLocation = {
    startLine: t.start.line,
    startCol: t.start.column,
    startOffset: t.start.offset,
    endLine: t.end.line,
    endCol: t.end.column,
    endOffset: t.end.offset
  });
}
export {
  $ as toParse5
};
