/**
 * 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 e=require("@lexical/extension"),t=require("@lexical/utils"),n=require("lexical");function o(e,t,o,r,i){if(null===e||0===o.size&&0===r.size&&!i)return 0;const s=t._selection,a=e._selection;if(i)return 1;if(!(n.$isRangeSelection(s)&&n.$isRangeSelection(a)&&a.isCollapsed()&&s.isCollapsed()))return 0;const d=function(e,t,o){const r=e._nodeMap,i=[];for(const e of t){const t=r.get(e);void 0!==t&&i.push(t)}for(const[e,t]of o){if(!t)continue;const o=r.get(e);void 0===o||n.$isRootNode(o)||i.push(o)}return i}(t,o,r);if(0===d.length)return 0;if(d.length>1){const o=t._nodeMap,r=o.get(s.anchor.key),i=o.get(a.anchor.key);return r&&i&&!e._nodeMap.has(r.__key)&&n.$isTextNode(r)&&1===r.__text.length&&1===s.anchor.offset?2:0}const l=d[0],c=e._nodeMap.get(l.__key);if(!n.$isTextNode(c)||!n.$isTextNode(l)||c.__mode!==l.__mode)return 0;const u=c.__text,_=l.__text;if(u===_)return 0;const f=s.anchor,p=a.anchor;if(f.key!==p.key||"text"!==f.type)return 0;const O=f.offset,C=p.offset,S=_.length-u.length;return 1===S&&C===O-1?2:-1===S&&C===O+1?3:-1===S&&C===O?4:0}function r(e,t,r){let i=r(),s=0,a=i,d=0,l=null;return(c,u,_,f,p,O)=>{const C=r();if(O.has(n.COMPOSITION_START_TAG)&&(a=i,d=s,l=c),O.has(n.HISTORIC_TAG))return s=0,i=C,2;O.has(n.COMPOSITION_END_TAG)&&l&&(i=a,s=d,c=l);const S=o(c,u,f,p,e.isComposing()),g=(()=>{const o=null===_||_.editor===e,r=O.has(n.HISTORY_PUSH_TAG);if(!r&&o&&O.has(n.HISTORY_MERGE_TAG))return 0;if(1===S)return 2;if(null===c)return 1;const a=u._selection;if(!(f.size>0||p.size>0))return null!==a?0:2;const d="number"==typeof t?t:t.peek();if(!1===r&&0!==S&&S===s&&C<i+d&&o)return 0;if(1===f.size){if(function(e,t,o){const r=t._nodeMap.get(e),i=o._nodeMap.get(e),s=t._selection,a=o._selection;return!(n.$isRangeSelection(s)&&n.$isRangeSelection(a)&&"element"===s.anchor.type&&"element"===s.focus.type&&"text"===a.anchor.type&&"text"===a.focus.type||!n.$isTextNode(r)||!n.$isTextNode(i)||r.__parent!==i.__parent)&&JSON.stringify(t.read(()=>r.exportJSON()))===JSON.stringify(o.read(()=>i.exportJSON()))}(Array.from(f)[0],c,u))return 0}return 1})();return i=C,s=S,g}}function i(e){e.undoStack=[],e.redoStack=[],e.current=null}function s(e,o,s,a=Date.now){const d=r(e,s,a);return t.mergeRegister(e.registerCommand(n.UNDO_COMMAND,()=>(function(e,t){const o=t.redoStack,r=t.undoStack;if(0!==r.length){const i=t.current,s=r.pop();null!==i&&(o.push(i),e.dispatchCommand(n.CAN_REDO_COMMAND,!0)),0===r.length&&e.dispatchCommand(n.CAN_UNDO_COMMAND,!1),t.current=s||null,s&&s.editor.setEditorState(s.editorState,{tag:n.HISTORIC_TAG})}}(e,o),!0),n.COMMAND_PRIORITY_EDITOR),e.registerCommand(n.REDO_COMMAND,()=>(function(e,t){const o=t.redoStack,r=t.undoStack;if(0!==o.length){const i=t.current;null!==i&&(r.push(i),e.dispatchCommand(n.CAN_UNDO_COMMAND,!0));const s=o.pop();0===o.length&&e.dispatchCommand(n.CAN_REDO_COMMAND,!1),t.current=s||null,s&&s.editor.setEditorState(s.editorState,{tag:n.HISTORIC_TAG})}}(e,o),!0),n.COMMAND_PRIORITY_EDITOR),e.registerCommand(n.CLEAR_EDITOR_COMMAND,()=>(i(o),!1),n.COMMAND_PRIORITY_EDITOR),e.registerCommand(n.CLEAR_HISTORY_COMMAND,()=>(i(o),e.dispatchCommand(n.CAN_REDO_COMMAND,!1),e.dispatchCommand(n.CAN_UNDO_COMMAND,!1),!0),n.COMMAND_PRIORITY_EDITOR),e.registerUpdateListener(({editorState:t,prevEditorState:r,dirtyLeaves:i,dirtyElements:s,tags:a})=>{const l=o.current,c=o.redoStack,u=o.undoStack,_=null===l?null:l.editorState;if(null!==l&&t===_)return;const f=d(r,t,l,i,s,a);if(1===f)0!==c.length&&(o.redoStack=[],e.dispatchCommand(n.CAN_REDO_COMMAND,!1)),null!==l&&(u.push({...l}),e.dispatchCommand(n.CAN_UNDO_COMMAND,!0));else if(2===f)return;o.current={editor:e,editorState:t}}))}function a(){return{current:null,redoStack:[],undoStack:[]}}const d=n.defineExtension({build:(t,{delay:n,createInitialHistoryState:o,disabled:r,now:i})=>e.namedSignals({delay:n,disabled:r,historyState:o(t),now:i}),config:n.safeCast({createInitialHistoryState:a,delay:300,disabled:"undefined"==typeof window,now:Date.now}),name:"@lexical/history/History",register:(t,n,o)=>{const r=o.getOutput();return e.effect(()=>r.disabled.value?void 0:s(t,r.historyState.value,r.delay,()=>r.now.peek()()))}});const l=n.defineExtension({build:(t,{disabled:n,parentEditor:o})=>e.namedSignals({disabled:n,parentEditor:o||t._parentEditor}),config:n.safeCast({disabled:!1,parentEditor:null}),dependencies:[n.configExtension(d,{disabled:!0})],name:"@lexical/history/SharedHistory",register:(t,n,o)=>e.effect(()=>{const{disabled:t,parentEditor:n}=o.getOutput();if(!t.value){const{output:t}=o.getDependency(d),r=function(t){return t?e.getPeerDependencyFromEditor(t,d.name):null}(n.value);if(!r)return;const i=r.output;e.batch(()=>{t.delay.value=i.delay.value,t.historyState.value=i.historyState.value,t.now.value=i.now.value,t.disabled.value=i.disabled.value})}})});exports.HistoryExtension=d,exports.SharedHistoryExtension=l,exports.createEmptyHistoryState=a,exports.registerHistory=s;
