import { __exports as _ } from "./index-LexicalSelection.prod2.js";
import { __require as F } from "./index-Lexical.js";
var P;
function k() {
  if (P) return _;
  P = 1;
  var d = F();
  let T = /* @__PURE__ */ new Map();
  function v(e) {
    for (; e != null; ) {
      if (e.nodeType === Node.TEXT_NODE) return e;
      e = e.firstChild;
    }
    return null;
  }
  function E(e) {
    let t = e.parentNode;
    if (t == null) throw Error("Should never happen");
    return [t, Array.from(t.childNodes).indexOf(e)];
  }
  function a(e) {
    let t = {};
    e = e.split(";");
    for (let o of e) if (o !== "") {
      let [r, n] = o.split(/:([^]+)/);
      r && n && (t[r.trim()] = n.trim());
    }
    return t;
  }
  function c(e) {
    let t = T.get(e);
    return t === void 0 && (t = a(e), T.set(e, t)), t;
  }
  function O(e) {
    let t = "";
    for (let o in e) o && (t += `${o}: ${e[o]};`);
    return t;
  }
  function y(e, t) {
    var o = c("getStyle" in e ? e.getStyle() : e.style);
    t = Object.entries(t).reduce((r, [n, l]) => (l === null ? delete r[n] : r[n] = l, r), { ...o }), o = O(t), e.setStyle(o), T.set(o, t);
  }
  function C(e, t) {
    var o = e.getNodes(), r = o.length;
    if (d.$isRangeSelection(e)) {
      --r;
      var n = o[0], l = o[r];
      if (e.isCollapsed() && d.$isRangeSelection(e)) y(e, t);
      else {
        var s = e.anchor, i = e.focus, u = n.getTextContent().length, g = i.offset, $ = s.offset, N = s.isBefore(i), f = N ? $ : g;
        e = N ? g : $;
        var h = N ? s.type : i.type, p = N ? i.type : s.type;
        if (i = N ? i.key : s.key, d.$isTextNode(n) && f === u && (N = n.getNextSibling(), d.$isTextNode(N) && (f = $ = 0, n = N)), o.length === 1) d.$isTextNode(n) && (f = h === "element" ? 0 : $ > g ? g : $, e = p === "element" ? u : $ > g ? $ : g, f !== e && (f === 0 && e === u ? (y(
          n,
          t
        ), n.select(f, e)) : (o = n.splitText(f, e), o = f === 0 ? o[0] : o[1], y(o, t), o.select(0, e - f))));
        else for (d.$isTextNode(n) && f < n.getTextContentSize() && (f !== 0 && (n = n.splitText(f)[1], f = 0, s.set(n.getKey(), f, "text")), y(n, t)), d.$isTextNode(l) && (f = l.getTextContent().length, l.__key !== i && e !== 0 && (e = f), e !== f && ([l] = l.splitText(e)), e !== 0 && y(l, t)), e = 1; e < r; e++) f = o[e], s = f.getKey(), d.$isTextNode(f) && s !== n.getKey() && s !== l.getKey() && !f.isToken() && y(f, t);
      }
    } else {
      for (n = d.$createRangeSelection(), l = n.anchor, f = n.focus, s = 0; s < r; s++) u = o[s], d.DEPRECATED_$isGridCellNode(u) && (l.set(u.getKey(), 0, "element"), f.set(u.getKey(), u.getChildrenSize(), "element"), C(d.$normalizeSelection__EXPERIMENTAL(n), t));
      d.$setSelection(e);
    }
  }
  function I(e) {
    for (; e !== null && !d.$isRootOrShadowRoot(e); ) {
      let t = e.getLatest(), o = e.getParent();
      t.getChildrenSize() === 0 && e.remove(!0), e = o;
    }
  }
  function m(e, t, o, r, n = null) {
    if (t.length !== 0) {
      var l = t[0], s = /* @__PURE__ */ new Map(), i = [];
      l = d.$isElementNode(l) ? l : l.getParentOrThrow(), l.isInline() && (l = l.getParentOrThrow());
      for (var u = !1; l !== null; ) {
        var g = l.getPreviousSibling();
        if (g !== null) {
          l = g, u = !0;
          break;
        }
        if (l = l.getParentOrThrow(), d.$isRootOrShadowRoot(l)) break;
      }
      g = /* @__PURE__ */ new Set();
      for (var $ = 0; $ < o; $++) {
        var N = t[$];
        d.$isElementNode(N) && N.getChildrenSize() === 0 && g.add(N.getKey());
      }
      var f = /* @__PURE__ */ new Set();
      for ($ = 0; $ < o; $++) {
        N = t[$];
        var h = N.getParent();
        if (h !== null && h.isInline() && (h = h.getParent()), h !== null && d.$isLeafNode(N) && !f.has(N.getKey())) {
          if (N = h.getKey(), s.get(N) === void 0) {
            let p = r();
            p.setFormat(h.getFormatType()), p.setIndent(h.getIndent()), i.push(p), s.set(N, p), h.getChildren().forEach((S) => {
              p.append(S), f.add(S.getKey()), d.$isElementNode(S) && S.getChildrenKeys().forEach((K) => f.add(K));
            }), I(h);
          }
        } else if (g.has(N.getKey())) {
          if (!d.$isElementNode(N)) throw Error("Expected node in emptyElements to be an ElementNode");
          h = r(), h.setFormat(N.getFormatType()), h.setIndent(N.getIndent()), i.push(h), N.remove(!0);
        }
      }
      if (n !== null) for (t = 0; t < i.length; t++) n.append(i[t]);
      if (t = null, d.$isRootOrShadowRoot(l)) if (u) if (n !== null) l.insertAfter(n);
      else for (n = i.length - 1; 0 <= n; n--) l.insertAfter(i[n]);
      else if (u = l.getFirstChild(), d.$isElementNode(u) && (l = u), u === null) if (n) l.append(n);
      else for (n = 0; n < i.length; n++) u = i[n], l.append(u), t = u;
      else if (n !== null) u.insertBefore(n);
      else for (l = 0; l < i.length; l++) n = i[l], u.insertBefore(n), t = n;
      else if (n) l.insertAfter(n);
      else for (n = i.length - 1; 0 <= n; n--) u = i[n], l.insertAfter(u), t = u;
      i = d.$getPreviousSelection(), d.$isRangeSelection(i) && i.anchor.getNode().isAttached() && i.focus.getNode().isAttached() ? d.$setSelection(i.clone()) : t !== null ? t.selectEnd() : e.dirty = !0;
    }
  }
  function R(e, t, o, r) {
    e.modify(t ? "extend" : "move", o, r);
  }
  function w(e) {
    return e = e.anchor.getNode(), (d.$isRootNode(e) ? e : e.getParentOrThrow()).getDirection() === "rtl";
  }
  function x(e) {
    if (d.$isDecoratorNode(e) && !e.isInline()) return !0;
    if (!d.$isElementNode(e) || d.$isRootOrShadowRoot(e)) return !1;
    var t = e.getFirstChild();
    return t = t === null || d.$isLineBreakNode(t) || d.$isTextNode(t) || t.isInline(), !e.isInline() && e.canBeEmpty() !== !1 && t;
  }
  return _.$addNodeStyle = function(e) {
    e = e.getStyle();
    let t = a(e);
    T.set(e, t);
  }, _.$cloneWithProperties = function(e) {
    let t = e.constructor.clone(e);
    return t.__parent = e.__parent, t.__next = e.__next, t.__prev = e.__prev, d.$isElementNode(e) && d.$isElementNode(t) ? (t.__first = e.__first, t.__last = e.__last, t.__size = e.__size, t.__format = e.__format, t.__indent = e.__indent, t.__dir = e.__dir, t) : (d.$isTextNode(e) && d.$isTextNode(t) && (t.__format = e.__format, t.__style = e.__style, t.__mode = e.__mode, t.__detail = e.__detail), t);
  }, _.$getSelectionStyleValueForProperty = function(e, t, o = "") {
    let r = null, n = e.getNodes();
    var l = e.anchor, s = e.focus, i = e.isBackward();
    let u = i ? s.offset : l.offset;
    if (l = i ? s.getNode() : l.getNode(), e.isCollapsed() && e.style !== "" && (e = c(e.style), e !== null && t in e)) return e[t];
    for (e = 0; e < n.length; e++) {
      var g = n[e];
      if ((e === 0 || u !== 0 || !g.is(l)) && d.$isTextNode(g)) {
        if (s = t, i = o, g = g.getStyle(), g = c(g), s = g !== null && g[s] || i, r === null) r = s;
        else if (r !== s) {
          r = "";
          break;
        }
      }
    }
    return r === null ? o : r;
  }, _.$isAtNodeEnd = function(e) {
    if (e.type === "text") return e.offset === e.getNode().getTextContentSize();
    let t = e.getNode();
    if (!d.$isElementNode(t)) throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");
    return e.offset === t.getChildrenSize();
  }, _.$isParentElementRTL = w, _.$moveCaretSelection = R, _.$moveCharacter = function(e, t, o) {
    let r = w(e);
    R(e, t, o ? !r : r, "character");
  }, _.$patchStyleText = C, _.$selectAll = function(e) {
    let t = e.anchor;
    e = e.focus;
    var o = t.getNode().getTopLevelElementOrThrow().getParentOrThrow();
    let r = o.getFirstDescendant();
    o = o.getLastDescendant();
    let n = "element", l = "element", s = 0;
    d.$isTextNode(r) ? n = "text" : d.$isElementNode(r) || r === null || (r = r.getParentOrThrow()), d.$isTextNode(o) ? (l = "text", s = o.getTextContentSize()) : d.$isElementNode(o) || o === null || (o = o.getParentOrThrow()), r && o && (t.set(r.getKey(), 0, n), e.set(o.getKey(), s, l));
  }, _.$setBlocksType = function(e, t) {
    if (e.anchor.key === "root") {
      t = t();
      var o = d.$getRoot();
      (e = o.getFirstChild()) ? e.replace(t, !0) : o.append(t);
    } else {
      for (o = e.getNodes(), e = e.anchor.getNode(); e !== null && e.getParent() !== null && !x(e); ) e = e.getParentOrThrow();
      for ((e = x(e) ? e : null) && o.indexOf(e) === -1 && o.push(e), e = 0; e < o.length; e++) {
        let r = o[e];
        if (!x(r)) continue;
        if (!d.$isElementNode(r)) throw Error("Expected block node to be an ElementNode");
        let n = t();
        n.setFormat(r.getFormatType()), n.setIndent(r.getIndent()), r.replace(
          n,
          !0
        );
      }
    }
  }, _.$shouldOverrideDefaultCharacterSelection = function(e, t) {
    return e = d.$getAdjacentNode(e.focus, t), d.$isDecoratorNode(e) && !e.isIsolated() || d.$isElementNode(e) && !e.isInline() && !e.canBeEmpty();
  }, _.$sliceSelectedTextNodeContent = function(e, t) {
    if (t.isSelected(e) && !t.isSegmented() && !t.isToken() && d.$INTERNAL_isPointSelection(e)) {
      var o = e.anchor.getNode(), r = e.focus.getNode(), n = t.is(o), l = t.is(r);
      if (n || l) {
        n = e.isBackward();
        let [s, i] = e.getCharacterOffsets();
        e = o.is(r), l = t.is(n ? r : o), r = t.is(n ? o : r), o = 0;
        let u;
        e ? (o = s > i ? i : s, u = s > i ? s : i) : l ? (o = n ? i : s, u = void 0) : r && (n = n ? s : i, o = 0, u = n), t.__text = t.__text.slice(o, u);
      }
    }
    return t;
  }, _.$wrapNodes = function(e, t, o = null) {
    var r = e.getNodes();
    let n = r.length;
    var l = e.anchor;
    if (n === 0 || n === 1 && l.type === "element" && l.getNode().getChildrenSize() === 0) {
      e = l.type === "text" ? l.getNode().getParentOrThrow() : l.getNode(), r = e.getChildren();
      let i = t();
      i.setFormat(e.getFormatType()), i.setIndent(e.getIndent()), r.forEach((u) => i.append(u)), o && (i = o.append(i)), e.replace(i);
    } else {
      l = null;
      var s = [];
      for (let i = 0; i < n; i++) {
        let u = r[i];
        d.$isRootOrShadowRoot(u) ? (m(e, s, s.length, t, o), s = [], l = u) : l === null || l !== null && d.$hasAncestor(
          u,
          l
        ) ? s.push(u) : (m(e, s, s.length, t, o), s = [u]);
      }
      m(e, s, s.length, t, o);
    }
  }, _.createDOMRange = function(e, t, o, r, n) {
    let l = t.getKey(), s = r.getKey(), i = document.createRange(), u = e.getElementByKey(l);
    if (e = e.getElementByKey(s), d.$isTextNode(t) && (u = v(u)), d.$isTextNode(r) && (e = v(e)), t === void 0 || r === void 0 || u === null || e === null) return null;
    u.nodeName === "BR" && ([u, o] = E(u)), e.nodeName === "BR" && ([e, n] = E(e)), t = u.firstChild, u === e && t != null && t.nodeName === "BR" && o === 0 && n === 0 && (n = 1);
    try {
      i.setStart(u, o), i.setEnd(e, n);
    } catch {
      return null;
    }
    return !i.collapsed || o === n && l === s || (i.setStart(e, n), i.setEnd(
      u,
      o
    )), i;
  }, _.createRectsFromDOMRange = function(e, t) {
    var o = e.getRootElement();
    if (o === null) return [];
    e = o.getBoundingClientRect(), o = getComputedStyle(o), o = parseFloat(o.paddingLeft) + parseFloat(o.paddingRight), t = Array.from(t.getClientRects());
    let r = t.length;
    t.sort((l, s) => {
      let i = l.top - s.top;
      return 3 >= Math.abs(i) ? l.left - s.left : i;
    });
    let n;
    for (let l = 0; l < r; l++) {
      let s = t[l], i = s.width + o === e.width;
      n && n.top <= s.top && n.top + n.height > s.top && n.left + n.width > s.left || i ? (t.splice(l--, 1), r--) : n = s;
    }
    return t;
  }, _.getStyleObjectFromCSS = c, _.trimTextContentFromAnchor = function(e, t, o) {
    let r = t.getNode();
    if (d.$isElementNode(r)) {
      var n = r.getDescendantByIndex(t.offset);
      n !== null && (r = n);
    }
    for (; 0 < o && r !== null; ) {
      d.$isElementNode(r) && (n = r.getLastDescendant(), n !== null && (r = n));
      var l = r.getPreviousSibling(), s = 0;
      if (l === null) {
        n = r.getParentOrThrow();
        for (var i = n.getPreviousSibling(); i === null; ) {
          if (n = n.getParent(), n === null) {
            l = null;
            break;
          }
          i = n.getPreviousSibling();
        }
        n !== null && (s = n.isInline() ? 0 : 2, l = i);
      }
      if (i = r.getTextContent(), i === "" && d.$isElementNode(r) && !r.isInline() && (i = `

`), n = i.length, !d.$isTextNode(r) || o >= n) i = r.getParent(), r.remove(), i == null || i.getChildrenSize() !== 0 || d.$isRootNode(i) || i.remove(), o -= n + s, r = l;
      else {
        let u = r.getKey();
        s = e.getEditorState().read(() => {
          const $ = d.$getNodeByKey(u);
          return d.$isTextNode($) && $.isSimpleText() ? $.getTextContent() : null;
        }), l = n - o;
        let g = i.slice(0, l);
        s !== null && s !== i ? (o = d.$getPreviousSelection(), n = r, r.isSimpleText() ? r.setTextContent(s) : (n = d.$createTextNode(s), r.replace(n)), d.$isRangeSelection(o) && o.isCollapsed() && (o = o.anchor.offset, n.select(o, o))) : r.isSimpleText() ? (s = t.key === u, i = t.offset, i < o && (i = n), o = s ? i - o : 0, n = s ? i : l, s && o === 0 ? ([o] = r.splitText(o, n), o.remove()) : ([, o] = r.splitText(o, n), o.remove())) : (o = d.$createTextNode(g), r.replace(o)), o = 0;
      }
    }
  }, _;
}
export {
  k as __require
};
