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