"use client";
import { $generateNodesFromDOM as y, $generateHtmlFromNodes as $ } from "../html/LexicalHtml.prod.js";
import { $addNodeStyle as D, $sliceSelectedTextNodeContent as O } from "../selection/LexicalSelection.prod.js";
import { objectKlassEquals as R } from "../utils/LexicalUtils.prod.js";
import { $isRangeSelection as h, $getSelection as p, $createTabNode as E, getDOMSelection as T, COPY_COMMAND as _, COMMAND_PRIORITY_CRITICAL as A, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND as M, $parseSerializedNode as v, $isTextNode as N, isSelectionWithinEditor as L, $getEditor as P, $getRoot as b, $isElementNode as g, $cloneWithProperties as F } from "../../lexical/Lexical.prod.js";
function x(t, ...e) {
  const n = new URL("https://lexical.dev/docs/error"), i = new URLSearchParams();
  i.append("code", t);
  for (const r of e) i.append("v", r);
  throw n.search = i.toString(), Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`);
}
function I(t, e = p()) {
  return e == null && x(166), h(e) && e.isCollapsed() || e.getNodes().length === 0 ? "" : $(t, e);
}
function H(t, e = p()) {
  return e == null && x(166), h(e) && e.isCollapsed() || e.getNodes().length === 0 ? null : JSON.stringify(J(t, e));
}
function G(t, e, n) {
  const i = t.getData("application/x-lexical-editor");
  if (i) try {
    const s = JSON.parse(i);
    if (s.namespace === n._config.namespace && Array.isArray(s.nodes))
      return w(n, z(s.nodes), e);
  } catch {
  }
  const r = t.getData("text/html"), d = t.getData("text/plain");
  if (r && d !== r) try {
    const s = new DOMParser().parseFromString((function(l) {
      return window.trustedTypes && window.trustedTypes.createPolicy ? window.trustedTypes.createPolicy("lexical", { createHTML: (o) => o }).createHTML(l) : l;
    })(r), "text/html");
    return w(n, y(n, s), e);
  } catch {
  }
  const a = d || t.getData("text/uri-list");
  if (a != null) if (h(e)) {
    const s = a.split(/(\r?\n|\t)/);
    s[s.length - 1] === "" && s.pop();
    for (let l = 0; l < s.length; l++) {
      const o = p();
      if (h(o)) {
        const c = s[l];
        c === `
` || c === `\r
` ? o.insertParagraph() : c === "	" ? o.insertNodes([E()]) : o.insertText(c);
      }
    }
  } else e.insertRawText(a);
}
function w(t, e, n) {
  t.dispatchCommand(M, { nodes: e, selection: n }) || n.insertNodes(e);
}
function S(t, e, n, i = []) {
  let r = e === null || n.isSelected(e);
  const d = g(n) && n.excludeFromCopy("html");
  let a = n;
  if (e !== null) {
    let o = F(n);
    o = N(o) && e !== null ? O(e, o) : o, a = o;
  }
  const s = g(a) ? a.getChildren() : [], l = (function(o) {
    const c = o.exportJSON(), u = o.constructor;
    if (c.type !== u.getType() && x(58, u.name), g(o)) {
      const m = c.children;
      Array.isArray(m) || x(59, u.name);
    }
    return c;
  })(a);
  if (N(a)) {
    const o = a.__text;
    o.length > 0 ? l.text = o : r = !1;
  }
  for (let o = 0; o < s.length; o++) {
    const c = s[o], u = S(t, e, c, l.children);
    !r && g(n) && u && n.extractWithChild(c, e, "clone") && (r = !0);
  }
  if (r && !d) i.push(l);
  else if (Array.isArray(l.children)) for (let o = 0; o < l.children.length; o++) {
    const c = l.children[o];
    i.push(c);
  }
  return r;
}
function J(t, e) {
  const n = [], i = b().getChildren();
  for (let r = 0; r < i.length; r++)
    S(t, e, i[r], n);
  return { namespace: t._config.namespace, nodes: n };
}
function z(t) {
  const e = [];
  for (let n = 0; n < t.length; n++) {
    const i = t[n], r = v(i);
    N(r) && D(r), e.push(r);
  }
  return e;
}
let f = null;
async function K(t, e, n) {
  if (f !== null) return !1;
  if (e !== null) return new Promise(((o, c) => {
    t.update((() => {
      o(C(t, e, n));
    }));
  }));
  const i = t.getRootElement(), r = t._window || window, d = window.document, a = T(r);
  if (i === null || a === null) return !1;
  const s = d.createElement("span");
  s.style.cssText = "position: fixed; top: -1000px;", s.append(d.createTextNode("#")), i.append(s);
  const l = new Range();
  return l.setStart(s, 0), l.setEnd(s, 1), a.removeAllRanges(), a.addRange(l), new Promise(((o, c) => {
    const u = t.registerCommand(_, ((m) => (R(m, ClipboardEvent) && (u(), f !== null && (window.clearTimeout(f), f = null), o(C(t, m, n))), !0)), A);
    f = window.setTimeout((() => {
      u(), f = null, o(!1);
    }), 50), d.execCommand("copy"), s.remove();
  }));
}
function C(t, e, n) {
  if (n === void 0) {
    const r = T(t._window);
    if (!r) return !1;
    const d = r.anchorNode, a = r.focusNode;
    if (d !== null && a !== null && !L(t, d, a)) return !1;
    const s = p();
    if (s === null) return !1;
    n = B(s);
  }
  e.preventDefault();
  const i = e.clipboardData;
  return i !== null && (U(i, n), !0);
}
const W = [["text/html", I], ["application/x-lexical-editor", H]];
function B(t = p()) {
  const e = { "text/plain": t ? t.getTextContent() : "" };
  if (t) {
    const n = P();
    for (const [i, r] of W) {
      const d = r(n, t);
      d !== null && (e[i] = d);
    }
  }
  return e;
}
function U(t, e) {
  for (const n in e) {
    const i = e[n];
    i !== void 0 && t.setData(n, i);
  }
}
export {
  J as $generateJSONFromSelectedNodes,
  z as $generateNodesFromSerializedNodes,
  B as $getClipboardDataFromSelection,
  I as $getHtmlContent,
  H as $getLexicalContent,
  G as $insertDataTransferForRichText,
  w as $insertGeneratedNodes,
  K as copyToClipboard,
  U as setLexicalClipboardDataTransfer
};
