"use client";
import { $sliceSelectedTextNodeContent as E } from "../selection/LexicalSelection.prod.js";
import { ArtificialNode__DO_NOT_USE as $, $createLineBreakNode as v, $isRootOrShadowRoot as O, $isBlockElementNode as y, isBlockDomNode as C, isInlineDomNode as S, $isElementNode as N, $getRoot as _, $cloneWithProperties as k, $isTextNode as L, isHTMLElement as P, isDocumentFragment as b, $createParagraphNode as B } from "../../lexical/Lexical.prod.js";
function I(l, o) {
  const c = o.body ? o.body.childNodes : [];
  let r = [];
  const s = [];
  for (let a = 0; a < c.length; a++) {
    const e = c[a];
    if (!F.has(e.nodeName)) {
      const n = T(e, l, s, !1);
      n !== null && (r = r.concat(n));
    }
  }
  return (function(a) {
    for (const e of a) e.getNextSibling() instanceof $ && e.insertAfter(v());
    for (const e of a) {
      const n = e.getChildren();
      for (const f of n) e.insertBefore(f);
      e.remove();
    }
  })(s), r;
}
function z(l, o) {
  if (typeof document > "u" || typeof window > "u" && global.window === void 0) throw new Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");
  const c = document.createElement("div"), r = _().getChildren();
  for (let s = 0; s < r.length; s++)
    D(l, r[s], c, o);
  return c.innerHTML;
}
function D(l, o, c, r = null) {
  let s = r === null || o.isSelected(r);
  const a = N(o) && o.excludeFromCopy("html");
  let e = o;
  if (r !== null) {
    let i = k(o);
    i = L(i) && r !== null ? E(r, i) : i, e = i;
  }
  const n = N(e) ? e.getChildren() : [], f = l._nodes.get(e.getType());
  let h;
  h = f && f.exportDOM !== void 0 ? f.exportDOM(l, e) : e.exportDOM(l);
  const { element: d, after: m } = h;
  if (!d) return !1;
  const u = document.createDocumentFragment();
  for (let i = 0; i < n.length; i++) {
    const t = n[i], p = D(l, t, u, r);
    !s && N(o) && p && o.extractWithChild(t, r, "html") && (s = !0);
  }
  if (s && !a) {
    if ((P(d) || b(d)) && d.append(u), c.append(d), m) {
      const i = m.call(e, d);
      i && (b(d) ? d.replaceChildren(i) : d.replaceWith(i));
    }
  } else c.append(u);
  return s;
}
const F = /* @__PURE__ */ new Set(["STYLE", "SCRIPT"]);
function T(l, o, c, r, s = /* @__PURE__ */ new Map(), a) {
  let e = [];
  if (F.has(l.nodeName)) return e;
  let n = null;
  const f = (function(t, p) {
    const { nodeName: A } = t, x = p._htmlConversions.get(A.toLowerCase());
    let g = null;
    if (x !== void 0) for (const M of x) {
      const w = M(t);
      w !== null && (g === null || (g.priority || 0) <= (w.priority || 0)) && (g = w);
    }
    return g !== null ? g.conversion : null;
  })(l, o), h = f ? f(l) : null;
  let d = null;
  if (h !== null) {
    d = h.after;
    const t = h.node;
    if (n = Array.isArray(t) ? t[t.length - 1] : t, n !== null) {
      for (const [, p] of s) if (n = p(n, a), !n) break;
      n && e.push(...Array.isArray(t) ? t : [n]);
    }
    h.forChild != null && s.set(l.nodeName, h.forChild);
  }
  const m = l.childNodes;
  let u = [];
  const i = (n == null || !O(n)) && (n != null && y(n) || r);
  for (let t = 0; t < m.length; t++) u.push(...T(m[t], o, c, i, new Map(s), n));
  return d != null && (u = d(u)), C(l) && (u = H(l, u, i ? () => {
    const t = new $();
    return c.push(t), t;
  } : B)), n == null ? u.length > 0 ? e = e.concat(u) : C(l) && (function(t) {
    return t.nextSibling == null || t.previousSibling == null ? !1 : S(t.nextSibling) && S(t.previousSibling);
  })(l) && (e = e.concat(v())) : N(n) && n.append(...u), e;
}
function H(l, o, c) {
  const r = l.style.textAlign, s = [];
  let a = [];
  for (let e = 0; e < o.length; e++) {
    const n = o[e];
    if (y(n)) r && !n.getFormat() && n.setFormat(r), s.push(n);
    else if (a.push(n), e === o.length - 1 || e < o.length - 1 && y(o[e + 1])) {
      const f = c();
      f.setFormat(r), f.append(...a), s.push(f), a = [];
    }
  }
  return s;
}
export {
  z as $generateHtmlFromNodes,
  I as $generateNodesFromDOM
};
