/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

"use strict";var t=require("@lexical/utils"),e=require("lexical");const n=0,r=1,o=2,i=0,s=1,c=2,a=3,u=4;function d(t,n,r,o,d){if(null===t||0===r.size&&0===o.size&&!d)return i;const _=n._selection,l=t._selection;if(d)return s;if(!(e.$isRangeSelection(_)&&e.$isRangeSelection(l)&&l.isCollapsed()&&_.isCollapsed()))return i;const f=function(t,n,r){const o=t._nodeMap,i=[];for(const t of n){const e=o.get(t);void 0!==e&&i.push(e)}for(const[t,n]of r){if(!n)continue;const r=o.get(t);void 0===r||e.$isRootNode(r)||i.push(r)}return i}(n,r,o);if(0===f.length)return i;if(f.length>1){const r=n._nodeMap,o=r.get(_.anchor.key),s=r.get(l.anchor.key);return o&&s&&!t._nodeMap.has(o.__key)&&e.$isTextNode(o)&&1===o.__text.length&&1===_.anchor.offset?c:i}const O=f[0],C=t._nodeMap.get(O.__key);if(!e.$isTextNode(C)||!e.$isTextNode(O)||C.__mode!==O.__mode)return i;const p=C.__text,M=O.__text;if(p===M)return i;const N=_.anchor,g=l.anchor;if(N.key!==g.key||"text"!==N.type)return i;const S=N.offset,h=g.offset,m=M.length-p.length;return 1===m&&h===S-1?c:-1===m&&h===S+1?a:-1===m&&h===S?u:i}function _(t,s){let c=Date.now(),a=i;return(u,_,l,f,O,C)=>{const p=Date.now();if(C.has(e.HISTORIC_TAG))return a=i,c=p,o;const M=d(u,_,f,O,t.isComposing()),N=(()=>{const d=null===l||l.editor===t,N=C.has(e.HISTORY_PUSH_TAG);if(!N&&d&&C.has(e.HISTORY_MERGE_TAG))return n;if(null===u)return r;const g=_._selection;if(!(f.size>0||O.size>0))return null!==g?n:o;if(!1===N&&M!==i&&M===a&&p<c+s&&d)return n;if(1===f.size){if(function(t,n,r){const o=n._nodeMap.get(t),i=r._nodeMap.get(t),s=n._selection,c=r._selection;return!(e.$isRangeSelection(s)&&e.$isRangeSelection(c)&&"element"===s.anchor.type&&"element"===s.focus.type&&"text"===c.anchor.type&&"text"===c.focus.type||!e.$isTextNode(o)||!e.$isTextNode(i)||o.__parent!==i.__parent)&&JSON.stringify(n.read((()=>o.exportJSON())))===JSON.stringify(r.read((()=>i.exportJSON())))}(Array.from(f)[0],u,_))return n}return r})();return c=p,a=M,N}}function l(t){t.undoStack=[],t.redoStack=[],t.current=null}exports.createEmptyHistoryState=function(){return{current:null,redoStack:[],undoStack:[]}},exports.registerHistory=function(n,i,s){const c=_(n,s);return t.mergeRegister(n.registerCommand(e.UNDO_COMMAND,(()=>(function(t,n){const r=n.redoStack,o=n.undoStack;if(0!==o.length){const i=n.current,s=o.pop();null!==i&&(r.push(i),t.dispatchCommand(e.CAN_REDO_COMMAND,!0)),0===o.length&&t.dispatchCommand(e.CAN_UNDO_COMMAND,!1),n.current=s||null,s&&s.editor.setEditorState(s.editorState,{tag:e.HISTORIC_TAG})}}(n,i),!0)),e.COMMAND_PRIORITY_EDITOR),n.registerCommand(e.REDO_COMMAND,(()=>(function(t,n){const r=n.redoStack,o=n.undoStack;if(0!==r.length){const i=n.current;null!==i&&(o.push(i),t.dispatchCommand(e.CAN_UNDO_COMMAND,!0));const s=r.pop();0===r.length&&t.dispatchCommand(e.CAN_REDO_COMMAND,!1),n.current=s||null,s&&s.editor.setEditorState(s.editorState,{tag:e.HISTORIC_TAG})}}(n,i),!0)),e.COMMAND_PRIORITY_EDITOR),n.registerCommand(e.CLEAR_EDITOR_COMMAND,(()=>(l(i),!1)),e.COMMAND_PRIORITY_EDITOR),n.registerCommand(e.CLEAR_HISTORY_COMMAND,(()=>(l(i),n.dispatchCommand(e.CAN_REDO_COMMAND,!1),n.dispatchCommand(e.CAN_UNDO_COMMAND,!1),!0)),e.COMMAND_PRIORITY_EDITOR),n.registerUpdateListener((({editorState:t,prevEditorState:s,dirtyLeaves:a,dirtyElements:u,tags:d})=>{const _=i.current,l=i.redoStack,f=i.undoStack,O=null===_?null:_.editorState;if(null!==_&&t===O)return;const C=c(s,t,_,a,u,d);if(C===r)0!==l.length&&(i.redoStack=[],n.dispatchCommand(e.CAN_REDO_COMMAND,!1)),null!==_&&(f.push({..._}),n.dispatchCommand(e.CAN_UNDO_COMMAND,!0));else if(C===o)return;i.current={editor:n,editorState:t}})))};
