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