import { Text as m, Tag as u, Style as s, Script as g, CDATA as x, Comment as h, Directive as M, Doctype as v, Root as b } from "./index-esm6.js";
import { elementNames as T, attributeNames as E } from "./index-foreignNames.js";
import { encodeXML as f, escapeText as $, escapeAttribute as A } from "./index-escape.js";
const C = /* @__PURE__ */ new Set([
  "style",
  "script",
  "xmp",
  "iframe",
  "noembed",
  "noframes",
  "plaintext",
  "noscript"
]);
function w(e) {
  return e.replace(/"/g, "&quot;");
}
function S(e, n) {
  var r;
  if (!e)
    return;
  const t = ((r = n.encodeEntities) !== null && r !== void 0 ? r : n.decodeEntities) === !1 ? w : n.xmlMode || n.encodeEntities !== "utf8" ? f : A;
  return Object.keys(e).map((a) => {
    var i, c;
    const o = (i = e[a]) !== null && i !== void 0 ? i : "";
    return n.xmlMode === "foreign" && (a = (c = E.get(a)) !== null && c !== void 0 ? c : a), !n.emptyAttrs && !n.xmlMode && o === "" ? a : `${a}="${t(o)}"`;
  }).join(" ");
}
const d = /* @__PURE__ */ new Set([
  "area",
  "base",
  "basefont",
  "br",
  "col",
  "command",
  "embed",
  "frame",
  "hr",
  "img",
  "input",
  "isindex",
  "keygen",
  "link",
  "meta",
  "param",
  "source",
  "track",
  "wbr"
]);
function l(e, n = {}) {
  const r = "length" in e ? e : [e];
  let t = "";
  for (let a = 0; a < r.length; a++)
    t += D(r[a], n);
  return t;
}
function D(e, n) {
  switch (e.type) {
    case b:
      return l(e.children, n);
    // @ts-expect-error We don't use `Doctype` yet
    case v:
    case M:
      return y(e);
    case h:
      return I(e);
    case x:
      return q(e);
    case g:
    case s:
    case u:
      return N(e, n);
    case m:
      return O(e, n);
  }
}
const _ = /* @__PURE__ */ new Set([
  "mi",
  "mo",
  "mn",
  "ms",
  "mtext",
  "annotation-xml",
  "foreignObject",
  "desc",
  "title"
]), j = /* @__PURE__ */ new Set(["svg", "math"]);
function N(e, n) {
  var r;
  n.xmlMode === "foreign" && (e.name = (r = T.get(e.name)) !== null && r !== void 0 ? r : e.name, e.parent && _.has(e.parent.name) && (n = { ...n, xmlMode: !1 })), !n.xmlMode && j.has(e.name) && (n = { ...n, xmlMode: "foreign" });
  let t = `<${e.name}`;
  const a = S(e.attribs, n);
  return a && (t += ` ${a}`), e.children.length === 0 && (n.xmlMode ? (
    // In XML mode or foreign mode, and user hasn't explicitly turned off self-closing tags
    n.selfClosingTags !== !1
  ) : (
    // User explicitly asked for self-closing tags, even in HTML mode
    n.selfClosingTags && d.has(e.name)
  )) ? (n.xmlMode || (t += " "), t += "/>") : (t += ">", e.children.length > 0 && (t += l(e.children, n)), (n.xmlMode || !d.has(e.name)) && (t += `</${e.name}>`)), t;
}
function y(e) {
  return `<${e.data}>`;
}
function O(e, n) {
  var r;
  let t = e.data || "";
  return ((r = n.encodeEntities) !== null && r !== void 0 ? r : n.decodeEntities) !== !1 && !(!n.xmlMode && e.parent && C.has(e.parent.name)) && (t = n.xmlMode || n.encodeEntities !== "utf8" ? f(t) : $(t)), t;
}
function q(e) {
  return `<![CDATA[${e.children[0].data}]]>`;
}
function I(e) {
  return `<!--${e.data}-->`;
}
export {
  l as default,
  l as render
};
