/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import e from"../../rope-sequence/dist/index.js";import{Mapping as t}from"../../prosemirror-transform/dist/index.js";import{Plugin as n,PluginKey as s}from"../../prosemirror-state/dist/index.js";class i{constructor(e,t){this.items=e,this.eventCount=t}popEvent(e,t){if(0==this.eventCount)return null;let n,s,o=this.items.length;for(;;o--){if(this.items.get(o-1).selection){--o;break}}t&&(n=this.remapping(o,this.items.length),s=n.maps.length);let p,l,a=e.tr,m=[],h=[];return this.items.forEach((e,t)=>{if(!e.step)return n||(n=this.remapping(o,t+1),s=n.maps.length),s--,void h.push(e);if(n){h.push(new r(e.map));let t,i=e.step.map(n.slice(s));i&&a.maybeStep(i).doc&&(t=a.mapping.maps[a.mapping.maps.length-1],m.push(new r(t,void 0,void 0,m.length+h.length))),s--,t&&n.appendMap(t,s)}else a.maybeStep(e.step);return e.selection?(p=n?e.selection.map(n.slice(s)):e.selection,l=new i(this.items.slice(0,o).append(h.reverse().concat(m)),this.eventCount-1),!1):void 0},this.items.length,0),{remaining:l,transform:a,selection:p}}addTransform(e,t,n,s){let o=[],l=this.eventCount,a=this.items,m=!s&&a.length?a.get(a.length-1):null;for(let i=0;i<e.steps.length;i++){let n,p=e.steps[i].invert(e.docs[i]),h=new r(e.mapping.maps[i],p,t);(n=m&&m.merge(h))&&(h=n,i?o.pop():a=a.slice(0,a.length-1)),o.push(h),t&&(l++,t=void 0),s||(m=h)}let h=l-n.depth;return h>p&&(a=function(e,t){let n;return e.forEach((e,s)=>{if(e.selection&&0==t--)return n=s,!1}),e.slice(n)}(a,h),l-=h),new i(a.append(o),l)}remapping(e,n){let s=new t;return this.items.forEach((t,n)=>{let i=null!=t.mirrorOffset&&n-t.mirrorOffset>=e?s.maps.length-t.mirrorOffset:void 0;s.appendMap(t.map,i)},e,n),s}addMaps(e){return 0==this.eventCount?this:new i(this.items.append(e.map(e=>new r(e))),this.eventCount)}rebased(e,t){if(!this.eventCount)return this;let n=[],s=Math.max(0,this.items.length-t),o=e.mapping,p=e.steps.length,l=this.eventCount;this.items.forEach(e=>{e.selection&&l--},s);let a=t;this.items.forEach(t=>{let s=o.getMirror(--a);if(null==s)return;p=Math.min(p,s);let i=o.maps[s];if(t.step){let p=e.steps[s].invert(e.docs[s]),m=t.selection&&t.selection.map(o.slice(a+1,s));m&&l++,n.push(new r(i,p,m))}else n.push(new r(i))},s);let m=[];for(let i=t;i<p;i++)m.push(new r(o.maps[i]));let h=this.items.slice(0,s).append(m).append(n),u=new i(h,l);return u.emptyItemCount()>500&&(u=u.compress(this.items.length-n.length)),u}emptyItemCount(){let e=0;return this.items.forEach(t=>{t.step||e++}),e}compress(t=this.items.length){let n=this.remapping(0,t),s=n.maps.length,o=[],p=0;return this.items.forEach((e,i)=>{if(i>=t)o.push(e),e.selection&&p++;else if(e.step){let t=e.step.map(n.slice(s)),i=t&&t.getMap();if(s--,i&&n.appendMap(i,s),t){let l=e.selection&&e.selection.map(n.slice(s));l&&p++;let a,m=new r(i.invert(),t,l),h=o.length-1;(a=o.length&&o[h].merge(m))?o[h]=a:o.push(m)}}else e.map&&s--},this.items.length,0),new i(e.from(o.reverse()),p)}}i.empty=new i(e.empty,0);class r{constructor(e,t,n,s){this.map=e,this.step=t,this.selection=n,this.mirrorOffset=s}merge(e){if(this.step&&e.step&&!e.selection){let t=e.step.merge(this.step);if(t)return new r(t.getMap().invert(),t,this.selection)}}}class o{constructor(e,t,n,s,i){this.done=e,this.undone=t,this.prevRanges=n,this.prevTime=s,this.prevComposition=i}}const p=20;function l(e){let t=[];for(let n=e.length-1;n>=0&&0==t.length;n--)e[n].forEach((e,n,s,i)=>t.push(s,i));return t}function a(e,t){if(!e)return null;let n=[];for(let s=0;s<e.length;s+=2){let i=t.map(e[s],1),r=t.map(e[s+1],-1);i<=r&&n.push(i,r)}return n}let m=!1,h=null;function u(e){let t=e.plugins;if(h!=t){m=!1,h=t;for(let e=0;e<t.length;e++)if(t[e].spec.historyPreserveItems){m=!0;break}}return m}const d=new s("history"),g=new s("closeHistory");function c(e={}){return e={depth:e.depth||100,newGroupDelay:e.newGroupDelay||500},new n({key:d,state:{init:()=>new o(i.empty,i.empty,null,0,-1),apply:(t,n,s)=>function(e,t,n,s){let r,p=n.getMeta(d);if(p)return p.historyState;n.getMeta(g)&&(e=new o(e.done,e.undone,null,0,-1));let m=n.getMeta("appendedTransaction");if(0==n.steps.length)return e;if(m&&m.getMeta(d))return m.getMeta(d).redo?new o(e.done.addTransform(n,void 0,s,u(t)),e.undone,l(n.mapping.maps),e.prevTime,e.prevComposition):new o(e.done,e.undone.addTransform(n,void 0,s,u(t)),null,e.prevTime,e.prevComposition);if(!1===n.getMeta("addToHistory")||m&&!1===m.getMeta("addToHistory"))return(r=n.getMeta("rebased"))?new o(e.done.rebased(n,r),e.undone.rebased(n,r),a(e.prevRanges,n.mapping),e.prevTime,e.prevComposition):new o(e.done.addMaps(n.mapping.maps),e.undone.addMaps(n.mapping.maps),a(e.prevRanges,n.mapping),e.prevTime,e.prevComposition);{let r=n.getMeta("composition"),p=0==e.prevTime||!m&&e.prevComposition!=r&&(e.prevTime<(n.time||0)-s.newGroupDelay||!function(e,t){if(!t)return!1;if(!e.docChanged)return!0;let n=!1;return e.mapping.maps[0].forEach((e,s)=>{for(let i=0;i<t.length;i+=2)e<=t[i+1]&&s>=t[i]&&(n=!0)}),n}(n,e.prevRanges)),h=m?a(e.prevRanges,n.mapping):l(n.mapping.maps);return new o(e.done.addTransform(n,p?t.selection.getBookmark():void 0,s,u(t)),i.empty,h,n.time,null==r?e.prevComposition:r)}}(n,s,t,e)},config:e,props:{handleDOMEvents:{beforeinput(e,t){let n=t.inputType,s="historyUndo"==n?v:"historyRedo"==n?w:null;return!(!s||!e.editable)&&(t.preventDefault(),s(e.state,e.dispatch))}}}})}function f(e,t){return(n,s)=>{let i=d.getState(n);if(!i||0==(e?i.undone:i.done).eventCount)return!1;if(s){let r=function(e,t,n){let s=u(t),i=d.get(t).spec.config,r=(n?e.undone:e.done).popEvent(t,s);if(!r)return null;let p=r.selection.resolve(r.transform.doc),l=(n?e.done:e.undone).addTransform(r.transform,t.selection.getBookmark(),i,s),a=new o(n?l:r.remaining,n?r.remaining:l,null,0,-1);return r.transform.setSelection(p).setMeta(d,{redo:n,historyState:a})}(i,n,e);r&&s(t?r.scrollIntoView():r)}return!0}}const v=f(!1,!0),w=f(!0,!0);export{c as history,w as redo,v as undo};