import { __exports as u } from "./index-LexicalClipboard.dev2.js";
import { __require as A } from "./index-LexicalHtml2.js";
import { __require as q } from "./index-LexicalSelection.js";
import { __require as F } from "./index-LexicalUtils.js";
import { __require as M } from "./index-Lexical.js";
var T;
function j() {
  if (T) return u;
  T = 1;
  var g = A(), m = q(), D = F(), a = M();
  const E = typeof window < "u" && typeof window.document < "u" && typeof window.document.createElement < "u", h = (t) => E ? (t || window).getSelection() : null;
  function N(t) {
    const e = a.$getSelection();
    if (e == null)
      throw Error("Expected valid LexicalSelection");
    return a.$isRangeSelection(e) && e.isCollapsed() || e.getNodes().length === 0 ? "" : g.$generateHtmlFromNodes(t, e);
  }
  function S(t) {
    const e = a.$getSelection();
    if (e == null)
      throw Error("Expected valid LexicalSelection");
    return a.$isRangeSelection(e) && e.isCollapsed() || e.getNodes().length === 0 ? null : JSON.stringify(_(t, e));
  }
  function y(t, e) {
    const n = t.getData("text/plain") || t.getData("text/uri-list");
    n != null && e.insertRawText(n);
  }
  function O(t, e, n) {
    const d = t.getData("application/x-lexical-editor");
    if (d)
      try {
        const o = JSON.parse(d);
        if (o.namespace === n._config.namespace && Array.isArray(o.nodes)) {
          const s = w(o.nodes);
          return x(n, s, e);
        }
      } catch {
      }
    const i = t.getData("text/html");
    if (i)
      try {
        const s = new DOMParser().parseFromString(i, "text/html"), c = g.$generateNodesFromDOM(n, s);
        return x(n, c, e);
      } catch {
      }
    const l = t.getData("text/plain") || t.getData("text/uri-list");
    if (l != null)
      if (a.$isRangeSelection(e)) {
        const o = l.split(/(\r?\n|\t)/);
        o[o.length - 1] === "" && o.pop();
        for (let s = 0; s < o.length; s++) {
          const c = o[s];
          c === `
` || c === `\r
` ? e.insertParagraph() : c === "	" ? e.insertNodes([a.$createTabNode()]) : e.insertText(c);
        }
      } else
        e.insertRawText(l);
  }
  function x(t, e, n) {
    t.dispatchCommand(a.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, {
      nodes: e,
      selection: n
    }) || n.insertNodes(e);
  }
  function L(t) {
    const e = t.exportJSON(), n = t.constructor;
    if (e.type !== n.getType())
      throw Error(`LexicalNode: Node ${n.name} does not implement .exportJSON().`);
    if (a.$isElementNode(t)) {
      const d = e.children;
      if (!Array.isArray(d))
        throw Error(`LexicalNode: Node ${n.name} is an element but .exportJSON() does not have a children array.`);
    }
    return e;
  }
  function C(t, e, n, d = []) {
    let i = e != null ? n.isSelected(e) : !0;
    const l = a.$isElementNode(n) && n.excludeFromCopy("html");
    let o = n;
    if (e !== null) {
      let r = m.$cloneWithProperties(n);
      r = a.$isTextNode(r) && e != null ? m.$sliceSelectedTextNodeContent(e, r) : r, o = r;
    }
    const s = a.$isElementNode(o) ? o.getChildren() : [], c = L(o);
    if (a.$isTextNode(o)) {
      const r = o.__text;
      r.length > 0 ? c.text = r : i = !1;
    }
    for (let r = 0; r < s.length; r++) {
      const p = s[r], b = C(t, e, p, c.children);
      !i && a.$isElementNode(n) && b && n.extractWithChild(p, e, "clone") && (i = !0);
    }
    if (i && !l)
      d.push(c);
    else if (Array.isArray(c.children))
      for (let r = 0; r < c.children.length; r++) {
        const p = c.children[r];
        d.push(p);
      }
    return i;
  }
  function _(t, e) {
    const n = [], i = a.$getRoot().getChildren();
    for (let l = 0; l < i.length; l++) {
      const o = i[l];
      C(t, e, o, n);
    }
    return {
      namespace: t._config.namespace,
      nodes: n
    };
  }
  function w(t) {
    const e = [];
    for (let n = 0; n < t.length; n++) {
      const d = t[n], i = a.$parseSerializedNode(d);
      a.$isTextNode(i) && m.$addNodeStyle(i), e.push(i);
    }
    return e;
  }
  const R = 50;
  let f = null;
  async function v(t, e) {
    if (f !== null)
      return !1;
    if (e !== null)
      return new Promise((s, c) => {
        t.update(() => {
          s($(t, e));
        });
      });
    const n = t.getRootElement(), d = t._window == null ? window.document : t._window.document, i = h(t._window);
    if (n === null || i === null)
      return !1;
    const l = d.createElement("span");
    l.style.cssText = "position: fixed; top: -1000px;", l.append(d.createTextNode("#")), n.append(l);
    const o = new Range();
    return o.setStart(l, 0), o.setEnd(l, 1), i.removeAllRanges(), i.addRange(o), new Promise((s, c) => {
      const r = t.registerCommand(a.COPY_COMMAND, (p) => (D.objectKlassEquals(p, ClipboardEvent) && (r(), f !== null && (window.clearTimeout(f), f = null), s($(t, p))), !0), a.COMMAND_PRIORITY_CRITICAL);
      f = window.setTimeout(() => {
        r(), f = null, s(!1);
      }, R), d.execCommand("copy"), l.remove();
    });
  }
  function $(t, e) {
    const n = h(t._window);
    if (!n)
      return !1;
    const d = n.anchorNode, i = n.focusNode;
    if (d !== null && i !== null && !a.isSelectionWithinEditor(t, d, i))
      return !1;
    e.preventDefault();
    const l = e.clipboardData, o = a.$getSelection();
    if (l === null || o === null)
      return !1;
    const s = N(t), c = S(t);
    let r = "";
    return o !== null && (r = o.getTextContent()), s !== null && l.setData("text/html", s), c !== null && l.setData("application/x-lexical-editor", c), l.setData("text/plain", r), !0;
  }
  return u.$generateJSONFromSelectedNodes = _, u.$generateNodesFromSerializedNodes = w, u.$getHtmlContent = N, u.$getLexicalContent = S, u.$insertDataTransferForPlainText = y, u.$insertDataTransferForRichText = O, u.$insertGeneratedNodes = x, u.copyToClipboard = v, u;
}
export {
  j as __require
};
