import { __exports as a } from "./index-LexicalUtils.dev2.js";
import { __require as se } from "./index-LexicalSelection.js";
import { __require as le } from "./index-Lexical.js";
var I;
function ae() {
  if (I) return a;
  I = 1;
  var A = se(), c = le();
  function D(...e) {
    return () => {
      e.forEach((n) => n());
    };
  }
  function $(e) {
    return `${e}px`;
  }
  const q = {
    attributes: !0,
    characterData: !0,
    childList: !0,
    subtree: !0
  };
  function F(e, n, t) {
    let r = null, s = null, o = null, i = [];
    const l = document.createElement("div");
    function d() {
      if (r === null)
        throw Error("Unexpected null rootDOMNode");
      if (s === null)
        throw Error("Unexpected null parentDOMNode");
      const {
        left: g,
        top: E
      } = r.getBoundingClientRect(), T = s, m = A.createRectsFromDOMRange(e, n);
      l.isConnected || T.append(l);
      let N = !1;
      for (let x = 0; x < m.length; x++) {
        const L = m[x], y = i[x] || document.createElement("div"), O = y.style;
        O.position !== "absolute" && (O.position = "absolute", N = !0);
        const w = $(L.left - g);
        O.left !== w && (O.left = w, N = !0);
        const v = $(L.top - E);
        O.top !== v && (y.style.top = v, N = !0);
        const M = $(L.width);
        O.width !== M && (y.style.width = M, N = !0);
        const R = $(L.height);
        O.height !== R && (y.style.height = R, N = !0), y.parentNode !== l && (l.append(y), N = !0), i[x] = y;
      }
      for (; i.length > m.length; )
        i.pop();
      N && t(i);
    }
    function f() {
      s = null, r = null, o !== null && o.disconnect(), o = null, l.remove();
      for (const g of i)
        g.remove();
      i = [];
    }
    function u() {
      const g = e.getRootElement();
      if (g === null)
        return f();
      const E = g.parentElement;
      if (!(E instanceof HTMLElement))
        return f();
      f(), r = g, s = E, o = new MutationObserver((T) => {
        const m = e.getRootElement(), N = m && m.parentElement;
        if (m !== r || N !== s)
          return u();
        for (const x of T)
          if (!l.contains(x.target))
            return d();
      }), o.observe(E, q), d();
    }
    const p = e.registerRootListener(u);
    return () => {
      p(), f();
    };
  }
  function U(e, n) {
    let t = null, r = null, s = null, o = null, i = () => {
    };
    function l(d) {
      d.read(() => {
        const f = c.$getSelection();
        if (!c.$isRangeSelection(f)) {
          t = null, r = null, s = null, o = null, i(), i = () => {
          };
          return;
        }
        const {
          anchor: u,
          focus: p
        } = f, g = u.getNode(), E = g.getKey(), T = u.offset, m = p.getNode(), N = m.getKey(), x = p.offset, L = e.getElementByKey(E), y = e.getElementByKey(N), O = t === null || L === null || T !== r || E !== t.getKey() || g !== t && (!(t instanceof c.TextNode) || g.updateDOM(t, L, e._config)), w = s === null || y === null || x !== o || N !== s.getKey() || m !== s && (!(s instanceof c.TextNode) || m.updateDOM(s, y, e._config));
        if (O || w) {
          const v = e.getElementByKey(u.getNode().getKey()), M = e.getElementByKey(p.getNode().getKey());
          if (v !== null && M !== null && v.tagName === "SPAN" && M.tagName === "SPAN") {
            const R = document.createRange();
            let S, _, b, C;
            p.isBefore(u) ? (S = M, _ = p.offset, b = v, C = u.offset) : (S = v, _ = u.offset, b = M, C = p.offset);
            const K = S.firstChild;
            if (K === null)
              throw Error("Expected text node to be first child of span");
            const H = b.firstChild;
            if (H === null)
              throw Error("Expected text node to be first child of span");
            R.setStart(K, _), R.setEnd(H, C), i(), i = F(e, R, (k) => {
              for (const re of k) {
                const h = re.style;
                h.background !== "Highlight" && (h.background = "Highlight"), h.color !== "HighlightText" && (h.color = "HighlightText"), h.zIndex !== "-1" && (h.zIndex = "-1"), h.pointerEvents !== "none" && (h.pointerEvents = "none"), h.marginTop !== $(-1.5) && (h.marginTop = $(-1.5)), h.paddingTop !== $(4) && (h.paddingTop = $(4)), h.paddingBottom !== $(0) && (h.paddingBottom = $(0));
              }
              n !== void 0 && n(k);
            });
          }
        }
        t = g, r = T, s = m, o = x;
      });
    }
    return l(e.getEditorState()), D(e.registerUpdateListener(({
      editorState: d
    }) => l(d)), i, () => {
      i();
    });
  }
  function z(e, ...n) {
    n.forEach((t) => {
      if (typeof t == "string") {
        const r = t.split(" ").filter((s) => s !== "");
        e.classList.add(...r);
      }
    });
  }
  function W(e, ...n) {
    n.forEach((t) => {
      typeof t == "string" && e.classList.remove(...t.split(" "));
    });
  }
  function P(e, n) {
    for (const t of n)
      if (e.type.startsWith(t))
        return !0;
    return !1;
  }
  function j(e, n) {
    const t = e[Symbol.iterator]();
    return new Promise((r, s) => {
      const o = [], i = () => {
        const {
          done: l,
          value: d
        } = t.next();
        if (l)
          return r(o);
        const f = new FileReader();
        f.addEventListener("error", s), f.addEventListener("load", () => {
          const u = f.result;
          typeof u == "string" && o.push({
            file: d,
            result: u
          }), i();
        }), P(d, n) ? f.readAsDataURL(d) : i();
      };
      i();
    });
  }
  function G(e, n) {
    const t = [], r = (e || c.$getRoot()).getLatest(), s = n || (c.$isElementNode(r) ? r.getLastDescendant() : r);
    let o = r, i = J(o);
    for (; o !== null && !o.is(s); )
      if (t.push({
        depth: i,
        node: o
      }), c.$isElementNode(o) && o.getChildrenSize() > 0)
        o = o.getFirstChild(), i++;
      else {
        let l = null;
        for (; l === null && o !== null; )
          l = o.getNextSibling(), l === null ? (o = o.getParent(), i--) : o = l;
      }
    return o !== null && o.is(s) && t.push({
      depth: i,
      node: o
    }), t;
  }
  function J(e) {
    let n = e, t = 0;
    for (; (n = n.getParent()) !== null; )
      t++;
    return t;
  }
  function Q(e, n) {
    let t = e;
    for (; t != null; ) {
      if (t instanceof n)
        return t;
      t = t.getParent();
    }
    return null;
  }
  function V(e) {
    const n = B(e, (t) => c.$isElementNode(t) && !t.isInline());
    if (!c.$isElementNode(n))
      throw Error(`Expected node ${e.__key} to have closest block element node.`);
    return n;
  }
  const B = (e, n) => {
    let t = e;
    for (; t !== c.$getRoot() && t != null; ) {
      if (n(t))
        return t;
      t = t.getParent();
    }
    return null;
  };
  function X(e, n, t, r) {
    const s = (l) => l instanceof n, o = (l) => {
      const d = l.getChildren();
      for (let p = 0; p < d.length; p++) {
        const g = d[p];
        if (s(g))
          return null;
      }
      let f = l, u = l;
      for (; f !== null; )
        if (u = f, f = f.getParent(), s(f))
          return {
            child: u,
            parent: f
          };
      return null;
    }, i = (l) => {
      const d = o(l);
      if (d !== null) {
        const {
          child: f,
          parent: u
        } = d;
        if (f.is(l)) {
          r(u, l);
          const p = f.getNextSiblings(), g = p.length;
          if (u.insertAfter(f), g !== 0) {
            const E = t(u);
            f.insertAfter(E);
            for (let T = 0; T < g; T++)
              E.append(p[T]);
          }
          !u.canBeEmpty() && u.getChildrenSize() === 0 && u.remove();
        }
      }
    };
    return e.registerNodeTransform(n, i);
  }
  function Y(e, n) {
    const r = /* @__PURE__ */ new Map(), s = e._pendingEditorState;
    for (const [i, l] of n._nodeMap) {
      const d = A.$cloneWithProperties(l);
      if (c.$isTextNode(d)) {
        if (!c.$isTextNode(l))
          throw Error("Expected node be a TextNode");
        d.__text = l.__text;
      }
      r.set(i, d);
    }
    s && (s._nodeMap = r), e._dirtyType = 2;
    const o = n._selection;
    c.$setSelection(o === null ? null : o.clone());
  }
  function Z(e) {
    const n = c.$getSelection() || c.$getPreviousSelection();
    if (c.$isRangeSelection(n)) {
      const {
        focus: t
      } = n, r = t.getNode(), s = t.offset;
      if (c.$isRootOrShadowRoot(r)) {
        const o = r.getChildAtIndex(s);
        o == null ? r.append(e) : o.insertBefore(e), e.selectNext();
      } else {
        let o, i;
        c.$isTextNode(r) ? (o = r.getParentOrThrow(), i = r.getIndexWithinParent(), s > 0 && (i += 1, r.splitText(s))) : (o = r, i = s);
        const [, l] = c.$splitNode(o, i);
        l.insertBefore(e), l.selectStart();
      }
    } else {
      if (n != null) {
        const r = n.getNodes();
        r[r.length - 1].getTopLevelElementOrThrow().insertAfter(e);
      } else
        c.$getRoot().append(e);
      const t = c.$createParagraphNode();
      e.insertAfter(t), t.select();
    }
    return e.getLatest();
  }
  function ee(e, n) {
    const t = n();
    return e.replace(t), t.append(e), t;
  }
  function te(e, n) {
    return e !== null ? Object.getPrototypeOf(e).constructor.name === n.name : !1;
  }
  function ne(e, n) {
    const t = [];
    for (let r = 0; r < e.length; r++) {
      const s = n(e[r]);
      s !== null && t.push(s);
    }
    return t;
  }
  function oe(e, n) {
    const t = e.getFirstChild();
    t !== null ? t.insertBefore(n) : e.append(n);
  }
  return a.$splitNode = c.$splitNode, a.isHTMLAnchorElement = c.isHTMLAnchorElement, a.isHTMLElement = c.isHTMLElement, a.$dfs = G, a.$filter = ne, a.$findMatchingParent = B, a.$getNearestBlockElementAncestorOrThrow = V, a.$getNearestNodeOfType = Q, a.$insertFirst = oe, a.$insertNodeToNearestRoot = Z, a.$restoreEditorState = Y, a.$wrapNodeInElement = ee, a.addClassNamesToElement = z, a.isMimeType = P, a.markSelection = U, a.mediaFileReader = j, a.mergeRegister = D, a.objectKlassEquals = te, a.positionNodeOnRange = F, a.registerNestedElementResolver = X, a.removeClassNamesFromElement = W, a;
}
export {
  ae as __require
};
