import { useCallback as b, Fragment as T, createElement as d } from "react";
import { jsx as u, Fragment as A } from "react/jsx-runtime";
import p from "./index-esm2.js";
import { domToReact as w } from "./index-html-react-parser.js";
import { parseDocument as L } from "./index-esm3.js";
import { Element as h } from "./index-node.js";
import g from "./index-_virtual.js";
const E = ({
  children: r,
  html: e,
  acceptUnknown: c
}) => {
  const i = b((t, o, l, s) => {
    if (!t || typeof t != "string")
      return u(T, {
        children: s
      }, l);
    const a = r[t], f = () => d(t, {
      ...o,
      children: s,
      key: l
    });
    return a === null ? f() : typeof a > "u" ? c ? f() : null : d(a, {
      ...o,
      index: l,
      key: l
    }, s);
  }, [c, r]), n = {
    replace: (t) => {
      if (t instanceof h) {
        const o = t.tagName.toLowerCase(), l = {
          ...t.attribs
        }, s = w(t.children, n);
        return i(o, l, Math.random(), s);
      }
    }
  };
  return u(A, {
    children: g(e, n)
  });
}, M = function(r) {
  return r ? typeof r != "string" ? r : {
    children: u(E, {
      html: r,
      children: {
        br: () => u("br", {}),
        b: (e) => u("b", {
          ...e
        }),
        em: (e) => u("em", {
          ...e
        }),
        link: (e) => u("link", {
          ...e
        }),
        span: (e) => u("span", {
          ...e
        })
      }
    })
  } : {
    children: null
  };
}, R = /* @__PURE__ */ new Set(["script", "style", "iframe", "object", "embed", "noscript"]), S = /* @__PURE__ */ new Set(["a", "abbr", "b", "blockquote", "br", "code", "div", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "li", "ol", "p", "pre", "span", "strong", "sub", "sup", "table", "thead", "tbody", "td", "th", "tr", "u", "ul"]), y = {
  a: ["href", "name", "target", "rel"]
}, _ = /^\s*(javascript|vbscript|data)\s*:/i, D = /* @__PURE__ */ new Set(["href", "src", "action", "formaction", "xlink:href"]);
function k(r, e, c, i) {
  if (e.toLowerCase().startsWith("on") || D.has(e) && _.test(c)) return !1;
  const n = i[r];
  return n ? n.includes(e) : !1;
}
function m(r, e, c, i) {
  const n = [];
  for (const t of r) {
    if (t.type === "text") {
      n.push(t);
      continue;
    }
    if (t.type !== "comment" && t instanceof h) {
      const o = t.tagName.toLowerCase();
      if (R.has(o))
        continue;
      const l = m(t.children, e, c, i);
      if (!e.has(o)) {
        n.push(...l);
        continue;
      }
      const s = {};
      for (const [a, f] of Object.entries(t.attribs))
        k(o, a, f, c) && (s[a] = f);
      if (i && "class" in s) {
        const a = s.class.split(/\s+/).filter((f) => f && i.has(f)).join(" ");
        a ? s.class = a : delete s.class;
      }
      t.attribs = s, t.children = l, n.push(t);
    }
  }
  return n;
}
function G(r, e) {
  if (!r) return "";
  const c = e != null && e.allowedTags ? new Set(e.allowedTags) : S, i = (e == null ? void 0 : e.allowedAttributes) ?? y, n = e != null && e.allowedClasses ? new Set(e.allowedClasses) : void 0, t = L(r);
  return t.children = m(t.children, c, i, n), p(t);
}
export {
  M as sanitizeHTMLToReactNode,
  G as sanitizeHtml
};
