import { __exports as O } from "./index-LexicalHistory.dev2.js";
import { __require as F } from "./index-LexicalUtils.js";
import { __require as G } from "./index-Lexical.js";
var y;
function b() {
  if (y) return O;
  y = 1;
  var x = F(), r = G();
  const h = 0, D = 1, M = 2, a = 0, k = 1, T = 2, I = 3, H = 4;
  function L(e, n, i) {
    const o = e._nodeMap, c = [];
    for (const t of n) {
      const s = o.get(t);
      s !== void 0 && c.push(s);
    }
    for (const [t, s] of i) {
      if (!s)
        continue;
      const u = o.get(t);
      u !== void 0 && !r.$isRootNode(u) && c.push(u);
    }
    return c;
  }
  function S(e, n, i, o, c) {
    if (e === null || i.size === 0 && o.size === 0 && !c)
      return a;
    const t = n._selection, s = e._selection;
    if (c)
      return k;
    if (!r.$isRangeSelection(t) || !r.$isRangeSelection(s) || !s.isCollapsed() || !t.isCollapsed())
      return a;
    const u = L(n, i, o);
    if (u.length === 0)
      return a;
    if (u.length > 1) {
      const R = n._nodeMap, m = R.get(t.anchor.key), z = R.get(s.anchor.key);
      return m && z && !e._nodeMap.has(m.__key) && r.$isTextNode(m) && m.__text.length === 1 && t.anchor.offset === 1 ? T : a;
    }
    const l = u[0], f = e._nodeMap.get(l.__key);
    if (!r.$isTextNode(f) || !r.$isTextNode(l) || f.__mode !== l.__mode)
      return a;
    const d = f.__text, C = l.__text;
    if (d === C)
      return a;
    const _ = t.anchor, N = s.anchor;
    if (_.key !== N.key || _.type !== "text")
      return a;
    const p = _.offset, A = N.offset, g = C.length - d.length;
    return g === 1 && A === p - 1 ? T : g === -1 && A === p + 1 ? I : g === -1 && A === p ? H : a;
  }
  function U(e, n, i) {
    const o = n._nodeMap.get(e), c = i._nodeMap.get(e), t = n._selection, s = i._selection;
    let u = !1;
    return r.$isRangeSelection(t) && r.$isRangeSelection(s) && (u = t.anchor.type === "element" && t.focus.type === "element" && s.anchor.type === "text" && s.focus.type === "text"), !u && r.$isTextNode(o) && r.$isTextNode(c) ? o.__type === c.__type && o.__text === c.__text && o.__mode === c.__mode && o.__detail === c.__detail && o.__style === c.__style && o.__format === c.__format && o.__parent === c.__parent : !1;
  }
  function $(e, n) {
    let i = Date.now(), o = a;
    return (c, t, s, u, l, f) => {
      const d = Date.now();
      if (f.has("historic"))
        return o = a, i = d, M;
      const C = S(c, t, u, l, e.isComposing()), _ = (() => {
        const N = s === null || s.editor === e, p = f.has("history-push");
        if (!p && N && f.has("history-merge"))
          return h;
        if (c === null)
          return D;
        const g = t._selection;
        if (!(u.size > 0 || l.size > 0))
          return g !== null ? h : M;
        if (p === !1 && C !== a && C === o && d < i + n && N)
          return h;
        if (u.size === 1) {
          const m = Array.from(u)[0];
          if (U(m, c, t))
            return h;
        }
        return D;
      })();
      return i = d, o = C, _;
    };
  }
  function Y(e, n) {
    const i = n.redoStack, o = n.undoStack;
    if (i.length !== 0) {
      const c = n.current;
      c !== null && (o.push(c), e.dispatchCommand(r.CAN_UNDO_COMMAND, !0));
      const t = i.pop();
      i.length === 0 && e.dispatchCommand(r.CAN_REDO_COMMAND, !1), n.current = t || null, t && t.editor.setEditorState(t.editorState, {
        tag: "historic"
      });
    }
  }
  function q(e, n) {
    const i = n.redoStack, o = n.undoStack;
    if (o.length !== 0) {
      const t = n.current, s = o.pop();
      t !== null && (i.push(t), e.dispatchCommand(r.CAN_REDO_COMMAND, !0)), o.length === 0 && e.dispatchCommand(r.CAN_UNDO_COMMAND, !1), n.current = s || null, s && s.editor.setEditorState(s.editorState, {
        tag: "historic"
      });
    }
  }
  function E(e) {
    e.undoStack = [], e.redoStack = [], e.current = null;
  }
  function P(e, n, i) {
    const o = $(e, i), c = ({
      editorState: u,
      prevEditorState: l,
      dirtyLeaves: f,
      dirtyElements: d,
      tags: C
    }) => {
      const _ = n.current, N = n.redoStack, p = n.undoStack, A = _ === null ? null : _.editorState;
      if (_ !== null && u === A)
        return;
      const g = o(l, u, _, f, d, C);
      if (g === D)
        N.length !== 0 && (n.redoStack = [], e.dispatchCommand(r.CAN_REDO_COMMAND, !1)), _ !== null && (p.push({
          ..._
        }), e.dispatchCommand(r.CAN_UNDO_COMMAND, !0));
      else if (g === M)
        return;
      n.current = {
        editor: e,
        editorState: u
      };
    }, t = x.mergeRegister(e.registerCommand(r.UNDO_COMMAND, () => (q(e, n), !0), r.COMMAND_PRIORITY_EDITOR), e.registerCommand(r.REDO_COMMAND, () => (Y(e, n), !0), r.COMMAND_PRIORITY_EDITOR), e.registerCommand(r.CLEAR_EDITOR_COMMAND, () => (E(n), !1), r.COMMAND_PRIORITY_EDITOR), e.registerCommand(r.CLEAR_HISTORY_COMMAND, () => (E(n), e.dispatchCommand(r.CAN_REDO_COMMAND, !1), e.dispatchCommand(r.CAN_UNDO_COMMAND, !1), !0), r.COMMAND_PRIORITY_EDITOR), e.registerUpdateListener(c)), s = e.registerUpdateListener(c);
    return () => {
      t(), s();
    };
  }
  function v() {
    return {
      current: null,
      redoStack: [],
      undoStack: []
    };
  }
  return O.createEmptyHistoryState = v, O.registerHistory = P, O;
}
export {
  b as __require
};
