import{g as R,Y as E,q as M,aJ as Y,l as w,aK as q,h as B,R as z,x as Q,aL as W,W as A,N as H,_ as b,F as K,aM as D,aN as P,aO as F,aP as V,aQ as $,aR as j,ac as G,o as T}from"./rich-text-theme-Bkhh9rAZ.js";import{r as J}from"./iframe-B8FSoBNY.js";function X(t,r,s,a,c){if(t===null||s.size===0&&a.size===0&&!c)return 0;const e=r._selection,n=t._selection;if(c)return 1;if(!(H(e)&&H(n)&&n.isCollapsed()&&e.isCollapsed()))return 0;const i=(function(g,d,S){const C=g._nodeMap,_=[];for(const k of d){const h=C.get(k);h!==void 0&&_.push(h)}for(const[k,h]of S){if(!h)continue;const N=C.get(k);N===void 0||K(N)||_.push(N)}return _})(r,s,a);if(i.length===0)return 0;if(i.length>1){const g=r._nodeMap,d=g.get(e.anchor.key),S=g.get(n.anchor.key);return d&&S&&!t._nodeMap.has(d.__key)&&b(d)&&d.__text.length===1&&e.anchor.offset===1?2:0}const u=i[0],l=t._nodeMap.get(u.__key);if(!b(l)||!b(u)||l.__mode!==u.__mode)return 0;const o=l.__text,p=u.__text;if(o===p)return 0;const m=e.anchor,y=n.anchor;if(m.key!==y.key||m.type!=="text")return 0;const f=m.offset,x=y.offset,v=p.length-o.length;return v===1&&x===f-1?2:v===-1&&x===f+1?3:v===-1&&x===f?4:0}function Z(t,r){let s=Date.now(),a=0;return(c,e,n,i,u,l)=>{const o=Date.now();if(l.has(z))return a=0,s=o,2;const p=X(c,e,i,u,t.isComposing()),m=(()=>{const y=n===null||n.editor===t,f=l.has(W);if(!f&&y&&l.has(A))return 0;if(c===null)return 1;const x=e._selection;if(!(i.size>0||u.size>0))return x!==null?0:2;const v=typeof r=="number"?r:r.peek();return f===!1&&p!==0&&p===a&&o<s+v&&y||i.size===1&&(function(g,d,S){const C=d._nodeMap.get(g),_=S._nodeMap.get(g),k=d._selection,h=S._selection;return!(H(k)&&H(h)&&k.anchor.type==="element"&&k.focus.type==="element"&&h.anchor.type==="text"&&h.focus.type==="text"||!b(C)||!b(_)||C.__parent!==_.__parent)&&JSON.stringify(d.read(()=>C.exportJSON()))===JSON.stringify(S.read(()=>_.exportJSON()))})(Array.from(i)[0],c,e)?0:1})();return s=o,a=p,m}}function U(t){t.undoStack=[],t.redoStack=[],t.current=null}function I(t,r,s){const a=Z(t,s);return R(t.registerCommand(Q,()=>((function(e,n){const i=n.redoStack,u=n.undoStack;if(u.length!==0){const l=n.current,o=u.pop();l!==null&&(i.push(l),e.dispatchCommand(E,!0)),u.length===0&&e.dispatchCommand(M,!1),n.current=o||null,o&&o.editor.setEditorState(o.editorState,{tag:z})}})(t,r),!0),w),t.registerCommand(B,()=>((function(e,n){const i=n.redoStack,u=n.undoStack;if(i.length!==0){const l=n.current;l!==null&&(u.push(l),e.dispatchCommand(M,!0));const o=i.pop();i.length===0&&e.dispatchCommand(E,!1),n.current=o||null,o&&o.editor.setEditorState(o.editorState,{tag:z})}})(t,r),!0),w),t.registerCommand(q,()=>(U(r),!1),w),t.registerCommand(Y,()=>(U(r),t.dispatchCommand(E,!1),t.dispatchCommand(M,!1),!0),w),t.registerUpdateListener(({editorState:e,prevEditorState:n,dirtyLeaves:i,dirtyElements:u,tags:l})=>{const o=r.current,p=r.redoStack,m=r.undoStack,y=o===null?null:o.editorState;if(o!==null&&e===y)return;const f=a(n,e,o,i,u,l);if(f===1)p.length!==0&&(r.redoStack=[],t.dispatchCommand(E,!1)),o!==null&&(m.push({...o}),t.dispatchCommand(M,!0));else if(f===2)return;r.current={editor:t,editorState:e}}))}function L(){return{current:null,redoStack:[],undoStack:[]}}const O=D({build:(t,{delay:r,createInitialHistoryState:s,disabled:a})=>G({delay:r,disabled:a,historyState:s(t)}),config:j({createInitialHistoryState:L,delay:300,disabled:typeof window>"u"}),name:"@lexical/history/History",register:(t,r,s)=>{const a=s.getOutput();return F(()=>a.disabled.value?void 0:I(t,a.historyState.value,a.delay))}});D({dependencies:[$(O,{createInitialHistoryState:()=>{throw new Error("SharedHistory did not inherit parent history")},disabled:!0})],name:"@lexical/history/SharedHistory",register(t,r,s){const{output:a}=s.getDependency(O),c=(function(n){return n?P(n,O.name):null})(t._parentEditor);if(!c)return()=>{};const e=c.output;return F(()=>V(()=>{a.delay.value=e.delay.value,a.historyState.value=e.historyState.value,a.disabled.value=e.disabled.value}))}});function nt({delay:t,externalHistoryState:r}){const[s]=T();return(function(a,c,e=1e3){const n=J.useMemo(()=>c||L(),[c]);J.useEffect(()=>I(a,n,e),[e,a,n])})(s,r,t),null}export{nt as a};
