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