import { __exports as $ } from "./index-LexicalSelection.dev2.js";
import { __require as se } from "./index-Lexical.js";
var k;
function re() {
  if (k) return $;
  k = 1;
  var r = se();
  const O = /* @__PURE__ */ new Map();
  function w(e) {
    let t = e;
    for (; t != null; ) {
      if (t.nodeType === Node.TEXT_NODE)
        return t;
      t = t.firstChild;
    }
    return null;
  }
  function F(e) {
    const t = e.parentNode;
    if (t == null)
      throw new Error("Should never happen");
    return [t, Array.from(t.childNodes).indexOf(e)];
  }
  function D(e, t, n, o, a) {
    const l = t.getKey(), d = o.getKey(), i = document.createRange();
    let s = e.getElementByKey(l), c = e.getElementByKey(d), u = n, h = a;
    if (r.$isTextNode(t) && (s = w(s)), r.$isTextNode(o) && (c = w(c)), t === void 0 || o === void 0 || s === null || c === null)
      return null;
    s.nodeName === "BR" && ([s, u] = F(s)), c.nodeName === "BR" && ([c, h] = F(c));
    const S = s.firstChild;
    s === c && S != null && S.nodeName === "BR" && u === 0 && h === 0 && (h = 1);
    try {
      i.setStart(s, u), i.setEnd(c, h);
    } catch {
      return null;
    }
    return i.collapsed && (u !== h || l !== d) && (i.setStart(c, h), i.setEnd(s, u)), i;
  }
  function z(e, t) {
    const n = e.getRootElement();
    if (n === null)
      return [];
    const o = n.getBoundingClientRect(), a = getComputedStyle(n), l = parseFloat(a.paddingLeft) + parseFloat(a.paddingRight), d = Array.from(t.getClientRects());
    let i = d.length;
    d.sort((c, u) => {
      const h = c.top - u.top;
      return Math.abs(h) <= 3 ? c.left - u.left : h;
    });
    let s;
    for (let c = 0; c < i; c++) {
      const u = d[c], h = s && s.top <= u.top && s.top + s.height > u.top && s.left + s.width > u.left, S = u.width + l === o.width;
      if (h || S) {
        d.splice(c--, 1), i--;
        continue;
      }
      s = u;
    }
    return d;
  }
  function b(e) {
    const t = {}, n = e.split(";");
    for (const o of n)
      if (o !== "") {
        const [a, l] = o.split(/:([^]+)/);
        a && l && (t[a.trim()] = l.trim());
      }
    return t;
  }
  function C(e) {
    let t = O.get(e);
    return t === void 0 && (t = b(e), O.set(e, t)), Object.freeze(t), t;
  }
  function M(e) {
    let t = "";
    for (const n in e)
      n && (t += `${n}: ${e[n]};`);
    return t;
  }
  function j(e, t) {
    return e.__first = t.__first, e.__last = t.__last, e.__size = t.__size, e.__format = t.__format, e.__indent = t.__indent, e.__dir = t.__dir, e;
  }
  function q(e, t) {
    return e.__format = t.__format, e.__style = t.__style, e.__mode = t.__mode, e.__detail = t.__detail, e;
  }
  function W(e) {
    const n = e.constructor.clone(e);
    return n.__parent = e.__parent, n.__next = e.__next, n.__prev = e.__prev, r.$isElementNode(e) && r.$isElementNode(n) ? j(n, e) : r.$isTextNode(e) && r.$isTextNode(n) ? q(n, e) : n;
  }
  function V(e, t) {
    if (t.isSelected(e) && !t.isSegmented() && !t.isToken() && r.$INTERNAL_isPointSelection(e)) {
      const n = e.anchor.getNode(), o = e.focus.getNode(), a = t.is(n), l = t.is(o);
      if (a || l) {
        const d = e.isBackward(), [i, s] = e.getCharacterOffsets(), c = n.is(o), u = t.is(d ? o : n), h = t.is(d ? n : o);
        let S = 0, N;
        if (c)
          S = i > s ? s : i, N = i > s ? i : s;
        else if (u)
          S = d ? s : i, N = void 0;
        else if (h) {
          const f = d ? i : s;
          S = 0, N = f;
        }
        return t.__text = t.__text.slice(S, N), t;
      }
    }
    return t;
  }
  function X(e) {
    if (e.type === "text")
      return e.offset === e.getNode().getTextContentSize();
    const t = e.getNode();
    if (!r.$isElementNode(t))
      throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");
    return e.offset === t.getChildrenSize();
  }
  function G(e, t, n) {
    let o = t.getNode(), a = n;
    if (r.$isElementNode(o)) {
      const l = o.getDescendantByIndex(t.offset);
      l !== null && (o = l);
    }
    for (; a > 0 && o !== null; ) {
      if (r.$isElementNode(o)) {
        const c = o.getLastDescendant();
        c !== null && (o = c);
      }
      let l = o.getPreviousSibling(), d = 0;
      if (l === null) {
        let c = o.getParentOrThrow(), u = c.getPreviousSibling();
        for (; u === null; ) {
          if (c = c.getParent(), c === null) {
            l = null;
            break;
          }
          u = c.getPreviousSibling();
        }
        c !== null && (d = c.isInline() ? 0 : 2, l = u);
      }
      let i = o.getTextContent();
      i === "" && r.$isElementNode(o) && !o.isInline() && (i = `

`);
      const s = i.length;
      if (!r.$isTextNode(o) || a >= s) {
        const c = o.getParent();
        o.remove(), c != null && c.getChildrenSize() === 0 && !r.$isRootNode(c) && c.remove(), a -= s + d, o = l;
      } else {
        const c = o.getKey(), u = e.getEditorState().read(() => {
          const N = r.$getNodeByKey(c);
          return r.$isTextNode(N) && N.isSimpleText() ? N.getTextContent() : null;
        }), h = s - a, S = i.slice(0, h);
        if (u !== null && u !== i) {
          const N = r.$getPreviousSelection();
          let f = o;
          if (o.isSimpleText())
            o.setTextContent(u);
          else {
            const g = r.$createTextNode(u);
            o.replace(g), f = g;
          }
          if (r.$isRangeSelection(N) && N.isCollapsed()) {
            const g = N.anchor.offset;
            f.select(g, g);
          }
        } else if (o.isSimpleText()) {
          const N = t.key === c;
          let f = t.offset;
          f < a && (f = s);
          const g = N ? f - a : 0, m = N ? f : h;
          if (N && g === 0) {
            const [T] = o.splitText(g, m);
            T.remove();
          } else {
            const [, T] = o.splitText(g, m);
            T.remove();
          }
        } else {
          const N = r.$createTextNode(S);
          o.replace(N);
        }
        a = 0;
      }
    }
  }
  function Y(e) {
    const t = e.getStyle(), n = b(t);
    O.set(t, n);
  }
  function _(e, t) {
    const n = C("getStyle" in e ? e.getStyle() : e.style), o = Object.entries(t).reduce((l, [d, i]) => (i === null ? delete l[d] : l[d] = i, l), {
      ...n
    }), a = M(o);
    e.setStyle(a), O.set(a, o);
  }
  function I(e, t) {
    const n = e.getNodes(), o = n.length;
    if (!r.$isRangeSelection(e)) {
      const p = r.$createRangeSelection(), y = p.anchor, E = p.focus;
      for (let P = 0; P < o; P++) {
        const R = n[P];
        r.DEPRECATED_$isGridCellNode(R) && (y.set(R.getKey(), 0, "element"), E.set(R.getKey(), R.getChildrenSize(), "element"), I(r.$normalizeSelection__EXPERIMENTAL(p), t));
      }
      r.$setSelection(e);
      return;
    }
    const a = o - 1;
    let l = n[0], d = n[a];
    if (e.isCollapsed() && r.$isRangeSelection(e)) {
      _(e, t);
      return;
    }
    const i = e.anchor, s = e.focus, u = l.getTextContent().length, h = s.offset;
    let S = i.offset;
    const N = i.isBefore(s);
    let f = N ? S : h, g = N ? h : S;
    const m = N ? i.type : s.type, T = N ? s.type : i.type, x = N ? s.key : i.key;
    if (r.$isTextNode(l) && f === u) {
      const p = l.getNextSibling();
      r.$isTextNode(p) && (S = 0, f = 0, l = p);
    }
    if (n.length === 1) {
      if (r.$isTextNode(l)) {
        if (f = m === "element" ? 0 : S > h ? h : S, g = T === "element" ? u : S > h ? S : h, f === g)
          return;
        if (f === 0 && g === u)
          _(l, t), l.select(f, g);
        else {
          const p = l.splitText(f, g), y = f === 0 ? p[0] : p[1];
          _(y, t), y.select(0, g - f);
        }
      }
    } else {
      if (r.$isTextNode(l) && f < l.getTextContentSize() && (f !== 0 && (l = l.splitText(f)[1], f = 0, i.set(l.getKey(), f, "text")), _(l, t)), r.$isTextNode(d)) {
        const y = d.getTextContent().length;
        d.__key !== x && g !== 0 && (g = y), g !== y && ([d] = d.splitText(g)), g !== 0 && _(d, t);
      }
      for (let p = 1; p < a; p++) {
        const y = n[p], E = y.getKey();
        r.$isTextNode(y) && E !== l.getKey() && E !== d.getKey() && !y.isToken() && _(y, t);
      }
    }
  }
  function H(e, t) {
    if (e.anchor.key === "root") {
      const a = t(), l = r.$getRoot(), d = l.getFirstChild();
      d ? d.replace(a, !0) : l.append(a);
      return;
    }
    const n = e.getNodes(), o = oe(e.anchor.getNode(), B);
    o && n.indexOf(o) === -1 && n.push(o);
    for (let a = 0; a < n.length; a++) {
      const l = n[a];
      if (!B(l))
        continue;
      if (!r.$isElementNode(l))
        throw Error("Expected block node to be an ElementNode");
      const d = t();
      d.setFormat(l.getFormatType()), d.setIndent(l.getIndent()), l.replace(d, !0);
    }
  }
  function K(e) {
    return e.getNode().isAttached();
  }
  function J(e) {
    let t = e;
    for (; t !== null && !r.$isRootOrShadowRoot(t); ) {
      const n = t.getLatest(), o = t.getParent();
      n.getChildrenSize() === 0 && t.remove(!0), t = o;
    }
  }
  function Q(e, t, n = null) {
    const o = e.getNodes(), a = o.length, l = e.anchor;
    if (a === 0 || a === 1 && l.type === "element" && l.getNode().getChildrenSize() === 0) {
      const s = l.type === "text" ? l.getNode().getParentOrThrow() : l.getNode(), c = s.getChildren();
      let u = t();
      u.setFormat(s.getFormatType()), u.setIndent(s.getIndent()), c.forEach((h) => u.append(h)), n && (u = n.append(u)), s.replace(u);
      return;
    }
    let d = null, i = [];
    for (let s = 0; s < a; s++) {
      const c = o[s];
      r.$isRootOrShadowRoot(c) ? (v(e, i, i.length, t, n), i = [], d = c) : d === null || d !== null && r.$hasAncestor(c, d) ? i.push(c) : (v(e, i, i.length, t, n), i = [c]);
    }
    v(e, i, i.length, t, n);
  }
  function v(e, t, n, o, a = null) {
    if (t.length === 0)
      return;
    const l = t[0], d = /* @__PURE__ */ new Map(), i = [];
    let s = r.$isElementNode(l) ? l : l.getParentOrThrow();
    s.isInline() && (s = s.getParentOrThrow());
    let c = !1;
    for (; s !== null; ) {
      const f = s.getPreviousSibling();
      if (f !== null) {
        s = f, c = !0;
        break;
      }
      if (s = s.getParentOrThrow(), r.$isRootOrShadowRoot(s))
        break;
    }
    const u = /* @__PURE__ */ new Set();
    for (let f = 0; f < n; f++) {
      const g = t[f];
      r.$isElementNode(g) && g.getChildrenSize() === 0 && u.add(g.getKey());
    }
    const h = /* @__PURE__ */ new Set();
    for (let f = 0; f < n; f++) {
      const g = t[f];
      let m = g.getParent();
      if (m !== null && m.isInline() && (m = m.getParent()), m !== null && r.$isLeafNode(g) && !h.has(g.getKey())) {
        const T = m.getKey();
        if (d.get(T) === void 0) {
          const x = o();
          x.setFormat(m.getFormatType()), x.setIndent(m.getIndent()), i.push(x), d.set(T, x), m.getChildren().forEach((p) => {
            x.append(p), h.add(p.getKey()), r.$isElementNode(p) && p.getChildrenKeys().forEach((y) => h.add(y));
          }), J(m);
        }
      } else if (u.has(g.getKey())) {
        if (!r.$isElementNode(g))
          throw Error("Expected node in emptyElements to be an ElementNode");
        const T = o();
        T.setFormat(g.getFormatType()), T.setIndent(g.getIndent()), i.push(T), g.remove(!0);
      }
    }
    if (a !== null)
      for (let f = 0; f < i.length; f++) {
        const g = i[f];
        a.append(g);
      }
    let S = null;
    if (r.$isRootOrShadowRoot(s))
      if (c)
        if (a !== null)
          s.insertAfter(a);
        else
          for (let f = i.length - 1; f >= 0; f--) {
            const g = i[f];
            s.insertAfter(g);
          }
      else {
        const f = s.getFirstChild();
        if (r.$isElementNode(f) && (s = f), f === null)
          if (a)
            s.append(a);
          else
            for (let g = 0; g < i.length; g++) {
              const m = i[g];
              s.append(m), S = m;
            }
        else if (a !== null)
          f.insertBefore(a);
        else
          for (let g = 0; g < i.length; g++) {
            const m = i[g];
            f.insertBefore(m), S = m;
          }
      }
    else if (a)
      s.insertAfter(a);
    else
      for (let f = i.length - 1; f >= 0; f--) {
        const g = i[f];
        s.insertAfter(g), S = g;
      }
    const N = r.$getPreviousSelection();
    r.$isRangeSelection(N) && K(N.anchor) && K(N.focus) ? r.$setSelection(N.clone()) : S !== null ? S.selectEnd() : e.dirty = !0;
  }
  function U(e, t) {
    const n = r.$getAdjacentNode(e.focus, t);
    return r.$isDecoratorNode(n) && !n.isIsolated() || r.$isElementNode(n) && !n.isInline() && !n.canBeEmpty();
  }
  function L(e, t, n, o) {
    e.modify(t ? "extend" : "move", n, o);
  }
  function A(e) {
    const t = e.anchor.getNode();
    return (r.$isRootNode(t) ? t : t.getParentOrThrow()).getDirection() === "rtl";
  }
  function Z(e, t, n) {
    const o = A(e);
    L(e, t, n ? !o : o, "character");
  }
  function ee(e) {
    const t = e.anchor, n = e.focus, l = t.getNode().getTopLevelElementOrThrow().getParentOrThrow();
    let d = l.getFirstDescendant(), i = l.getLastDescendant(), s = "element", c = "element", u = 0;
    r.$isTextNode(d) ? s = "text" : !r.$isElementNode(d) && d !== null && (d = d.getParentOrThrow()), r.$isTextNode(i) ? (c = "text", u = i.getTextContentSize()) : !r.$isElementNode(i) && i !== null && (i = i.getParentOrThrow()), d && i && (t.set(d.getKey(), 0, s), n.set(i.getKey(), u, c));
  }
  function te(e, t, n) {
    const o = e.getStyle(), a = C(o);
    return a !== null && a[t] || n;
  }
  function ne(e, t, n = "") {
    let o = null;
    const a = e.getNodes(), l = e.anchor, d = e.focus, i = e.isBackward(), s = i ? d.offset : l.offset, c = i ? d.getNode() : l.getNode();
    if (e.isCollapsed() && e.style !== "") {
      const u = e.style, h = C(u);
      if (h !== null && t in h)
        return h[t];
    }
    for (let u = 0; u < a.length; u++) {
      const h = a[u];
      if (!(u !== 0 && s === 0 && h.is(c)) && r.$isTextNode(h)) {
        const S = te(h, t, n);
        if (o === null)
          o = S;
        else if (o !== S) {
          o = "";
          break;
        }
      }
    }
    return o === null ? n : o;
  }
  function B(e) {
    if (r.$isDecoratorNode(e) && !e.isInline())
      return !0;
    if (!r.$isElementNode(e) || r.$isRootOrShadowRoot(e))
      return !1;
    const t = e.getFirstChild(), n = t === null || r.$isLineBreakNode(t) || r.$isTextNode(t) || t.isInline();
    return !e.isInline() && e.canBeEmpty() !== !1 && n;
  }
  function oe(e, t) {
    let n = e;
    for (; n !== null && n.getParent() !== null && !t(n); )
      n = n.getParentOrThrow();
    return t(n) ? n : null;
  }
  return $.$addNodeStyle = Y, $.$cloneWithProperties = W, $.$getSelectionStyleValueForProperty = ne, $.$isAtNodeEnd = X, $.$isParentElementRTL = A, $.$moveCaretSelection = L, $.$moveCharacter = Z, $.$patchStyleText = I, $.$selectAll = ee, $.$setBlocksType = H, $.$shouldOverrideDefaultCharacterSelection = U, $.$sliceSelectedTextNodeContent = V, $.$wrapNodes = Q, $.createDOMRange = D, $.createRectsFromDOMRange = z, $.getStyleObjectFromCSS = C, $.trimTextContentFromAnchor = G, $;
}
export {
  re as __require
};
