"use client";
import { ccount as a } from "../../../ccount/index.js";
import { stringify as u } from "../../../comma-separated-tokens/index.js";
import { svg as h } from "../../../property-information/index.js";
import { stringify as b } from "../../../space-separated-tokens/index.js";
import { closing as d } from "../omission/closing.js";
import { opening as y } from "../omission/opening.js";
import { find as A } from "../../../property-information/lib/find.js";
import { stringifyEntities as m } from "../../../stringify-entities/lib/index.js";
const g = {
  // See: <https://html.spec.whatwg.org/#attribute-name-state>.
  name: [
    [`	
\f\r &/=>`.split(""), `	
\f\r "&'/=>\``.split("")],
    [`\0	
\f\r "&'/<=>`.split(""), `\0	
\f\r "&'/<=>\``.split("")]
  ],
  // See: <https://html.spec.whatwg.org/#attribute-value-(unquoted)-state>.
  unquoted: [
    [`	
\f\r &>`.split(""), `\0	
\f\r "&'<=>\``.split("")],
    [`\0	
\f\r "&'<=>\``.split(""), `\0	
\f\r "&'<=>\``.split("")]
  ],
  // See: <https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state>.
  single: [
    ["&'".split(""), "\"&'`".split("")],
    ["\0&'".split(""), "\0\"&'`".split("")]
  ],
  // See: <https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state>.
  double: [
    ['"&'.split(""), "\"&'`".split("")],
    ['\0"&'.split(""), "\0\"&'`".split("")]
  ]
};
function R(s, o, t, i) {
  const e = i.schema, r = e.space === "svg" ? !1 : i.settings.omitOptionalTags;
  let f = e.space === "svg" ? i.settings.closeEmptyElements : i.settings.voids.includes(s.tagName.toLowerCase());
  const n = [];
  let l;
  e.space === "html" && s.tagName === "svg" && (i.schema = h);
  const c = N(i, s.properties), p = i.all(
    e.space === "html" && s.tagName === "template" ? s.content : s
  );
  return i.schema = e, p && (f = !1), (c || !r || !y(s, o, t)) && (n.push("<", s.tagName, c ? " " + c : ""), f && (e.space === "svg" || i.settings.closeSelfClosing) && (l = c.charAt(c.length - 1), (!i.settings.tightSelfClosing || l === "/" || l && l !== '"' && l !== "'") && n.push(" "), n.push("/")), n.push(">")), n.push(p), !f && (!r || !d(s, o, t)) && n.push("</" + s.tagName + ">"), n.join("");
}
function N(s, o) {
  const t = [];
  let i = -1, e;
  if (o) {
    for (e in o)
      if (o[e] !== null && o[e] !== void 0) {
        const r = q(s, e, o[e]);
        r && t.push(r);
      }
  }
  for (; ++i < t.length; ) {
    const r = s.settings.tightAttributes ? t[i].charAt(t[i].length - 1) : void 0;
    i !== t.length - 1 && r !== '"' && r !== "'" && (t[i] += " ");
  }
  return t.join("");
}
function q(s, o, t) {
  const i = A(s.schema, o), e = s.settings.allowParseErrors && s.schema.space === "html" ? 0 : 1, r = s.settings.allowDangerousCharacters ? 0 : 1;
  let f = s.quote, n;
  if (i.overloadedBoolean && (t === i.attribute || t === "") ? t = !0 : (i.boolean || i.overloadedBoolean) && (typeof t != "string" || t === i.attribute || t === "") && (t = !!t), t == null || t === !1 || typeof t == "number" && Number.isNaN(t))
    return "";
  const l = m(
    i.attribute,
    Object.assign({}, s.settings.characterReferences, {
      // Always encode without parse errors in non-HTML.
      subset: g.name[e][r]
    })
  );
  return t === !0 || (t = Array.isArray(t) ? (i.commaSeparated ? u : b)(t, {
    padLeft: !s.settings.tightCommaSeparatedLists
  }) : String(t), s.settings.collapseEmptyAttributes && !t) ? l : (s.settings.preferUnquoted && (n = m(
    t,
    Object.assign({}, s.settings.characterReferences, {
      attribute: !0,
      subset: g.unquoted[e][r]
    })
  )), n !== t && (s.settings.quoteSmart && a(t, f) > a(t, s.alternative) && (f = s.alternative), n = f + m(
    t,
    Object.assign({}, s.settings.characterReferences, {
      // Always encode without parse errors in non-HTML.
      subset: (f === "'" ? g.single : g.double)[e][r],
      attribute: !0
    })
  ) + f), l + (n && "=" + n));
}
export {
  R as element
};
