/*!
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{Slice as t,Fragment as e,Mark as r,ReplaceError as n,MarkType as i}from"../../prosemirror-model/dist/index.js";const s=Math.pow(2,16);function o(t,e){return t+e*s}function l(t){return 65535&t}class a{constructor(t,e,r){this.pos=t,this.delInfo=e,this.recover=r}get deleted(){return(8&this.delInfo)>0}get deletedBefore(){return(5&this.delInfo)>0}get deletedAfter(){return(6&this.delInfo)>0}get deletedAcross(){return(4&this.delInfo)>0}}class p{constructor(t,e=!1){if(this.ranges=t,this.inverted=e,!t.length&&p.empty)return p.empty}recover(t){let e=0,r=l(t);if(!this.inverted)for(let n=0;n<r;n++)e+=this.ranges[3*n+2]-this.ranges[3*n+1];return this.ranges[3*r]+e+function(t){return(t-(65535&t))/s}(t)}mapResult(t,e=1){return this._map(t,e,!1)}map(t,e=1){return this._map(t,e,!0)}_map(t,e,r){let n=0,i=this.inverted?2:1,s=this.inverted?1:2;for(let l=0;l<this.ranges.length;l+=3){let p=this.ranges[l]-(this.inverted?n:0);if(p>t)break;let h=this.ranges[l+i],c=this.ranges[l+s],f=p+h;if(t<=f){let i=p+n+((h?t==p?-1:t==f?1:e:e)<0?0:c);if(r)return i;let s=t==(e<0?p:f)?null:o(l/3,t-p),d=t==p?2:t==f?1:4;return(e<0?t!=p:t!=f)&&(d|=8),new a(i,d,s)}n+=c-h}return r?t+n:new a(t+n,0,null)}touches(t,e){let r=0,n=l(e),i=this.inverted?2:1,s=this.inverted?1:2;for(let o=0;o<this.ranges.length;o+=3){let e=this.ranges[o]-(this.inverted?r:0);if(e>t)break;let l=this.ranges[o+i];if(t<=e+l&&o==3*n)return!0;r+=this.ranges[o+s]-l}return!1}forEach(t){let e=this.inverted?2:1,r=this.inverted?1:2;for(let n=0,i=0;n<this.ranges.length;n+=3){let s=this.ranges[n],o=s-(this.inverted?i:0),l=s+(this.inverted?0:i),a=this.ranges[n+e],p=this.ranges[n+r];t(o,o+a,l,l+p),i+=p-a}}invert(){return new p(this.ranges,!this.inverted)}toString(){return(this.inverted?"-":"")+JSON.stringify(this.ranges)}static offset(t){return 0==t?p.empty:new p(t<0?[0,-t,0]:[0,0,t])}}p.empty=new p([]);class h{constructor(t,e,r=0,n=(t?t.length:0)){this.mirror=e,this.from=r,this.to=n,this._maps=t||[],this.ownData=!(t||e)}get maps(){return this._maps}slice(t=0,e=this.maps.length){return new h(this._maps,this.mirror,t,e)}appendMap(t,e){this.ownData||(this._maps=this._maps.slice(),this.mirror=this.mirror&&this.mirror.slice(),this.ownData=!0),this.to=this._maps.push(t),null!=e&&this.setMirror(this._maps.length-1,e)}appendMapping(t){for(let e=0,r=this._maps.length;e<t._maps.length;e++){let n=t.getMirror(e);this.appendMap(t._maps[e],null!=n&&n<e?r+n:void 0)}}getMirror(t){if(this.mirror)for(let e=0;e<this.mirror.length;e++)if(this.mirror[e]==t)return this.mirror[e+(e%2?-1:1)]}setMirror(t,e){this.mirror||(this.mirror=[]),this.mirror.push(t,e)}appendMappingInverted(t){for(let e=t.maps.length-1,r=this._maps.length+t._maps.length;e>=0;e--){let n=t.getMirror(e);this.appendMap(t._maps[e].invert(),null!=n&&n>e?r-n-1:void 0)}}invert(){let t=new h;return t.appendMappingInverted(this),t}map(t,e=1){if(this.mirror)return this._map(t,e,!0);for(let r=this.from;r<this.to;r++)t=this._maps[r].map(t,e);return t}mapResult(t,e=1){return this._map(t,e,!1)}_map(t,e,r){let n=0;for(let i=this.from;i<this.to;i++){let r=this._maps[i].mapResult(t,e);if(null!=r.recover){let e=this.getMirror(i);if(null!=e&&e>i&&e<this.to){i=e,t=this._maps[e].recover(r.recover);continue}}n|=r.delInfo,t=r.pos}return r?t:new a(t,n,null)}}const c=/* @__PURE__ */Object.create(null);class f{getMap(){return p.empty}merge(t){return null}static fromJSON(t,e){if(!e||!e.stepType)throw new RangeError("Invalid input for Step.fromJSON");let r=c[e.stepType];if(!r)throw new RangeError(`No step type ${e.stepType} defined`);return r.fromJSON(t,e)}static jsonID(t,e){if(t in c)throw new RangeError("Duplicate use of step JSON ID "+t);return c[t]=e,e.prototype.jsonID=t,e}}class d{constructor(t,e){this.doc=t,this.failed=e}static ok(t){return new d(t,null)}static fail(t){return new d(null,t)}static fromReplace(t,e,r,i){try{return d.ok(t.replace(e,r,i))}catch(s){if(s instanceof n)return d.fail(s.message);throw s}}}function u(t,r,n){let i=[];for(let e=0;e<t.childCount;e++){let s=t.child(e);s.content.size&&(s=s.copy(u(s.content,r,s))),s.isInline&&(s=r(s,n,e)),i.push(s)}return e.fromArray(i)}class m extends f{constructor(t,e,r){super(),this.from=t,this.to=e,this.mark=r}apply(e){let r=e.slice(this.from,this.to),n=e.resolve(this.from),i=n.node(n.sharedDepth(this.to)),s=new t(u(r.content,(t,e)=>t.isAtom&&e.type.allowsMarkType(this.mark.type)?t.mark(this.mark.addToSet(t.marks)):t,i),r.openStart,r.openEnd);return d.fromReplace(e,this.from,this.to,s)}invert(){return new g(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deleted&&r.deleted||e.pos>=r.pos?null:new m(e.pos,r.pos,this.mark)}merge(t){return t instanceof m&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new m(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:"addMark",mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to)throw new RangeError("Invalid input for AddMarkStep.fromJSON");return new m(e.from,e.to,t.markFromJSON(e.mark))}}f.jsonID("addMark",m);class g extends f{constructor(t,e,r){super(),this.from=t,this.to=e,this.mark=r}apply(e){let r=e.slice(this.from,this.to),n=new t(u(r.content,t=>t.mark(this.mark.removeFromSet(t.marks)),e),r.openStart,r.openEnd);return d.fromReplace(e,this.from,this.to,n)}invert(){return new m(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deleted&&r.deleted||e.pos>=r.pos?null:new g(e.pos,r.pos,this.mark)}merge(t){return t instanceof g&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new g(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:"removeMark",mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to)throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");return new g(e.from,e.to,t.markFromJSON(e.mark))}}f.jsonID("removeMark",g);class y extends f{constructor(t,e){super(),this.pos=t,this.mark=e}apply(r){let n=r.nodeAt(this.pos);if(!n)return d.fail("No node at mark step's position");let i=n.type.create(n.attrs,null,this.mark.addToSet(n.marks));return d.fromReplace(r,this.pos,this.pos+1,new t(e.from(i),0,n.isLeaf?0:1))}invert(t){let e=t.nodeAt(this.pos);if(e){let t=this.mark.addToSet(e.marks);if(t.length==e.marks.length){for(let r=0;r<e.marks.length;r++)if(!e.marks[r].isInSet(t))return new y(this.pos,e.marks[r]);return new y(this.pos,this.mark)}}return new w(this.pos,this.mark)}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new y(e.pos,this.mark)}toJSON(){return{stepType:"addNodeMark",pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,e){if("number"!=typeof e.pos)throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");return new y(e.pos,t.markFromJSON(e.mark))}}f.jsonID("addNodeMark",y);class w extends f{constructor(t,e){super(),this.pos=t,this.mark=e}apply(r){let n=r.nodeAt(this.pos);if(!n)return d.fail("No node at mark step's position");let i=n.type.create(n.attrs,null,this.mark.removeFromSet(n.marks));return d.fromReplace(r,this.pos,this.pos+1,new t(e.from(i),0,n.isLeaf?0:1))}invert(t){let e=t.nodeAt(this.pos);return e&&this.mark.isInSet(e.marks)?new y(this.pos,this.mark):this}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new w(e.pos,this.mark)}toJSON(){return{stepType:"removeNodeMark",pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,e){if("number"!=typeof e.pos)throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");return new w(e.pos,t.markFromJSON(e.mark))}}f.jsonID("removeNodeMark",w);class k extends f{constructor(t,e,r,n=!1){super(),this.from=t,this.to=e,this.slice=r,this.structure=n}apply(t){return this.structure&&S(t,this.from,this.to)?d.fail("Structure replace would overwrite content"):d.fromReplace(t,this.from,this.to,this.slice)}getMap(){return new p([this.from,this.to-this.from,this.slice.size])}invert(t){return new k(this.from,this.from+this.slice.size,t.slice(this.from,this.to))}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deletedAcross&&r.deletedAcross?null:new k(e.pos,Math.max(e.pos,r.pos),this.slice,this.structure)}merge(e){if(!(e instanceof k)||e.structure||this.structure)return null;if(this.from+this.slice.size!=e.from||this.slice.openEnd||e.slice.openStart){if(e.to!=this.from||this.slice.openStart||e.slice.openEnd)return null;{let r=this.slice.size+e.slice.size==0?t.empty:new t(e.slice.content.append(this.slice.content),e.slice.openStart,this.slice.openEnd);return new k(e.from,this.to,r,this.structure)}}{let r=this.slice.size+e.slice.size==0?t.empty:new t(this.slice.content.append(e.slice.content),this.slice.openStart,e.slice.openEnd);return new k(this.from,this.to+(e.to-e.from),r,this.structure)}}toJSON(){let t={stepType:"replace",from:this.from,to:this.to};return this.slice.size&&(t.slice=this.slice.toJSON()),this.structure&&(t.structure=!0),t}static fromJSON(e,r){if("number"!=typeof r.from||"number"!=typeof r.to)throw new RangeError("Invalid input for ReplaceStep.fromJSON");return new k(r.from,r.to,t.fromJSON(e,r.slice),!!r.structure)}}f.jsonID("replace",k);class v extends f{constructor(t,e,r,n,i,s,o=!1){super(),this.from=t,this.to=e,this.gapFrom=r,this.gapTo=n,this.slice=i,this.insert=s,this.structure=o}apply(t){if(this.structure&&(S(t,this.from,this.gapFrom)||S(t,this.gapTo,this.to)))return d.fail("Structure gap-replace would overwrite content");let e=t.slice(this.gapFrom,this.gapTo);if(e.openStart||e.openEnd)return d.fail("Gap is not a flat range");let r=this.slice.insertAt(this.insert,e.content);return r?d.fromReplace(t,this.from,this.to,r):d.fail("Content does not fit in gap")}getMap(){return new p([this.from,this.gapFrom-this.from,this.insert,this.gapTo,this.to-this.gapTo,this.slice.size-this.insert])}invert(t){let e=this.gapTo-this.gapFrom;return new v(this.from,this.from+this.slice.size+e,this.from+this.insert,this.from+this.insert+e,t.slice(this.from,this.to).removeBetween(this.gapFrom-this.from,this.gapTo-this.from),this.gapFrom-this.from,this.structure)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1),n=this.from==this.gapFrom?e.pos:t.map(this.gapFrom,-1),i=this.to==this.gapTo?r.pos:t.map(this.gapTo,1);return e.deletedAcross&&r.deletedAcross||n<e.pos||i>r.pos?null:new v(e.pos,r.pos,n,i,this.slice,this.insert,this.structure)}toJSON(){let t={stepType:"replaceAround",from:this.from,to:this.to,gapFrom:this.gapFrom,gapTo:this.gapTo,insert:this.insert};return this.slice.size&&(t.slice=this.slice.toJSON()),this.structure&&(t.structure=!0),t}static fromJSON(e,r){if("number"!=typeof r.from||"number"!=typeof r.to||"number"!=typeof r.gapFrom||"number"!=typeof r.gapTo||"number"!=typeof r.insert)throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");return new v(r.from,r.to,r.gapFrom,r.gapTo,t.fromJSON(e,r.slice),r.insert,!!r.structure)}}function S(t,e,r){let n=t.resolve(e),i=r-e,s=n.depth;for(;i>0&&s>0&&n.indexAfter(s)==n.node(s).childCount;)s--,i--;if(i>0){let t=n.node(s).maybeChild(n.indexAfter(s));for(;i>0;){if(!t||t.isLeaf)return!0;t=t.firstChild,i--}}return!1}function M(r,n,i,s=i.contentMatch,o=!0){let l=r.doc.nodeAt(n),a=[],p=n+1;for(let h=0;h<l.childCount;h++){let n=l.child(h),c=p+n.nodeSize,f=s.matchType(n.type);if(f){s=f;for(let t=0;t<n.marks.length;t++)i.allowsMarkType(n.marks[t].type)||r.step(new g(p,c,n.marks[t]));if(o&&n.isText&&"pre"!=i.whitespace){let r,s,o=/\r?\n|\r/g;for(;r=o.exec(n.text);)s||(s=new t(e.from(i.schema.text(" ",i.allowedMarks(n.marks))),0,0)),a.push(new k(p+r.index,p+r.index+r[0].length,s))}}else a.push(new k(p,c,t.empty));p=c}if(!s.validEnd){let n=s.fillBefore(e.empty,!0);r.replace(p,p,new t(n,0,0))}for(let t=a.length-1;t>=0;t--)r.step(a[t])}function x(t,e,r){return(0==e||t.canReplace(e,t.childCount))&&(r==t.childCount||t.canReplace(0,r))}function R(t){let e=t.parent.content.cutByIndex(t.startIndex,t.endIndex);for(let r=t.depth,n=0,i=0;;--r){let s=t.$from.node(r),o=t.$from.index(r)+n,l=t.$to.indexAfter(r)-i;if(r<t.depth&&s.canReplace(o,l,e))return r;if(0==r||s.type.spec.isolating||!x(s,o,l))break;o&&(n=1),l<s.childCount&&(i=1)}return null}function C(t,e,r=null,n=t){let i=function(t,e){let{parent:r,startIndex:n,endIndex:i}=t,s=r.contentMatchAt(n).findWrapping(e);if(!s)return null;let o=s.length?s[0]:e;return r.canReplaceWith(n,i,o)?s:null}(t,e),s=i&&function(t,e){let{parent:r,startIndex:n,endIndex:i}=t,s=r.child(n),o=e.contentMatch.findWrapping(s.type);if(!o)return null;let l=(o.length?o[o.length-1]:e).contentMatch;for(let a=n;l&&a<i;a++)l=l.matchType(r.child(a).type);return l&&l.validEnd?o:null}(n,e);return s?i.map(b).concat({type:e,attrs:r}).concat(s.map(b)):null}function b(t){return{type:t,attrs:null}}function N(t,e,r,n){e.forEach((i,s)=>{if(i.isText){let o,l=/\r?\n|\r/g;for(;o=l.exec(i.text);){let i=t.mapping.slice(n).map(r+1+s+o.index);t.replaceWith(i,i+1,e.type.schema.linebreakReplacement.create())}}})}function I(t,e,r,n){e.forEach((i,s)=>{if(i.type==i.type.schema.linebreakReplacement){let i=t.mapping.slice(n).map(r+1+s);t.replaceWith(i,i+1,e.type.schema.text("\n"))}})}function T(t,e,r=1,n){let i=t.resolve(e),s=i.depth-r,o=n&&n[n.length-1]||i.parent;if(s<0||i.parent.type.spec.isolating||!i.parent.canReplace(i.index(),i.parent.childCount)||!o.type.validContent(i.parent.content.cutByIndex(i.index(),i.parent.childCount)))return!1;for(let p=i.depth-1,h=r-2;p>s;p--,h--){let t=i.node(p),e=i.index(p);if(t.type.spec.isolating)return!1;let r=t.content.cutByIndex(e,t.childCount),s=n&&n[h+1];s&&(r=r.replaceChild(0,s.type.create(s.attrs)));let o=n&&n[h]||t;if(!t.canReplace(e+1,t.childCount)||!o.type.validContent(r))return!1}let l=i.indexAfter(s),a=n&&n[0];return i.node(s).canReplaceWith(l,l,a?a.type:i.node(s+1).type)}function A(t,e){let r=t.resolve(e),n=r.index();return O(r.nodeBefore,r.nodeAfter)&&r.parent.canReplace(n,n+1)}function O(t,e){return!(!t||!e||t.isLeaf||!function(t,e){e.content.size||t.type.compatibleContent(e.type);let r=t.contentMatchAt(t.childCount),{linebreakReplacement:n}=t.type.schema;for(let i=0;i<e.childCount;i++){let s=e.child(i),o=s.type==n?t.type.schema.nodes.text:s.type;if(r=r.matchType(o),!r)return!1;if(!t.type.allowsMarks(s.marks))return!1}return r.validEnd}(t,e))}function E(t,e,r=-1){let n=t.resolve(e);for(let i=n.depth;;i--){let t,s,o=n.index(i);if(i==n.depth?(t=n.nodeBefore,s=n.nodeAfter):r>0?(t=n.node(i+1),o++,s=n.node(i).maybeChild(o)):(t=n.node(i).maybeChild(o-1),s=n.node(i+1)),t&&!t.isTextblock&&O(t,s)&&n.node(i).canReplace(o,o+1))return e;if(0==i)break;e=r<0?n.before(i):n.after(i)}}function J(t,e,r){let n=t.resolve(e);if(n.parent.canReplaceWith(n.index(),n.index(),r))return e;if(0==n.parentOffset)for(let i=n.depth-1;i>=0;i--){let t=n.index(i);if(n.node(i).canReplaceWith(t,t,r))return n.before(i+1);if(t>0)return null}if(n.parentOffset==n.parent.content.size)for(let i=n.depth-1;i>=0;i--){let t=n.indexAfter(i);if(n.node(i).canReplaceWith(t,t,r))return n.after(i+1);if(t<n.node(i).childCount)return null}return null}function z(t,e,r){let n=t.resolve(e);if(!r.content.size)return e;let i=r.content;for(let s=0;s<r.openStart;s++)i=i.firstChild.content;for(let s=1;s<=(0==r.openStart&&r.size?2:1);s++)for(let t=n.depth;t>=0;t--){let e=t==n.depth?0:n.pos<=(n.start(t+1)+n.end(t+1))/2?-1:1,r=n.index(t)+(e>0?1:0),o=n.node(t),l=!1;if(1==s)l=o.canReplace(r,r,i);else{let t=o.contentMatchAt(r).findWrapping(i.firstChild.type);l=t&&o.canReplaceWith(r,r,t[0])}if(l)return 0==e?n.pos:e<0?n.before(t+1):n.after(t+1)}return null}function F(e,r,n=r,i=t.empty){if(r==n&&!i.size)return null;let s=e.resolve(r),o=e.resolve(n);return _(s,o,i)?new k(r,n,i):new D(s,o,i).fit()}function _(t,e,r){return!r.openStart&&!r.openEnd&&t.start()==e.start()&&t.parent.canReplace(t.index(),e.index(),r.content)}f.jsonID("replaceAround",v);class D{constructor(t,r,n){this.$from=t,this.$to=r,this.unplaced=n,this.frontier=[],this.placed=e.empty;for(let e=0;e<=t.depth;e++){let r=t.node(e);this.frontier.push({type:r.type,match:r.contentMatchAt(t.indexAfter(e))})}for(let i=t.depth;i>0;i--)this.placed=e.from(t.node(i).copy(this.placed))}get depth(){return this.frontier.length-1}fit(){for(;this.unplaced.size;){let t=this.findFittable();t?this.placeNodes(t):this.openMore()||this.dropNode()}let e=this.mustMoveInline(),r=this.placed.size-this.depth-this.$from.depth,n=this.$from,i=this.close(e<0?this.$to:n.doc.resolve(e));if(!i)return null;let s=this.placed,o=n.depth,l=i.depth;for(;o&&l&&1==s.childCount;)s=s.firstChild.content,o--,l--;let a=new t(s,o,l);return e>-1?new v(n.pos,e,this.$to.pos,this.$to.end(),a,r):a.size||n.pos!=this.$to.pos?new k(n.pos,i.pos,a):null}findFittable(){let t=this.unplaced.openStart;for(let e=this.unplaced.content,r=0,n=this.unplaced.openEnd;r<t;r++){let i=e.firstChild;if(e.childCount>1&&(n=0),i.type.spec.isolating&&n<=r){t=r;break}e=i.content}for(let r=1;r<=2;r++)for(let n=1==r?t:this.unplaced.openStart;n>=0;n--){let t,i=null;n?(i=W(this.unplaced.content,n-1).firstChild,t=i.content):t=this.unplaced.content;let s=t.firstChild;for(let o=this.depth;o>=0;o--){let t,{type:l,match:a}=this.frontier[o],p=null;if(1==r&&(s?a.matchType(s.type)||(p=a.fillBefore(e.from(s),!1)):i&&l.compatibleContent(i.type)))return{sliceDepth:n,frontierDepth:o,parent:i,inject:p};if(2==r&&s&&(t=a.findWrapping(s.type)))return{sliceDepth:n,frontierDepth:o,parent:i,wrap:t};if(i&&a.matchType(i.type))break}}}openMore(){let{content:e,openStart:r,openEnd:n}=this.unplaced,i=W(e,r);return!(!i.childCount||i.firstChild.isLeaf)&&(this.unplaced=new t(e,r+1,Math.max(n,i.size+r>=e.size-n?r+1:0)),!0)}dropNode(){let{content:e,openStart:r,openEnd:n}=this.unplaced,i=W(e,r);if(i.childCount<=1&&r>0){let s=e.size-r<=r+i.size;this.unplaced=new t(B(e,r-1,1),r-1,s?r-1:n)}else this.unplaced=new t(B(e,r,1),r,n)}placeNodes({sliceDepth:r,frontierDepth:n,parent:i,inject:s,wrap:o}){for(;this.depth>n;)this.closeFrontierNode();if(o)for(let t=0;t<o.length;t++)this.openFrontierNode(o[t]);let l=this.unplaced,a=i?i.content:l.content,p=l.openStart-r,h=0,c=[],{match:f,type:d}=this.frontier[n];if(s){for(let t=0;t<s.childCount;t++)c.push(s.child(t));f=f.matchFragment(s)}let u=a.size+r-(l.content.size-l.openEnd);for(;h<a.childCount;){let t=a.child(h),e=f.matchType(t.type);if(!e)break;h++,(h>1||0==p||t.content.size)&&(f=e,c.push(j(t.mark(d.allowedMarks(t.marks)),1==h?p:0,h==a.childCount?u:-1)))}let m=h==a.childCount;m||(u=-1),this.placed=$(this.placed,n,e.from(c)),this.frontier[n].match=f,m&&u<0&&i&&i.type==this.frontier[this.depth].type&&this.frontier.length>1&&this.closeFrontierNode();for(let t=0,e=a;t<u;t++){let t=e.lastChild;this.frontier.push({type:t.type,match:t.contentMatchAt(t.childCount)}),e=t.content}this.unplaced=m?0==r?t.empty:new t(B(l.content,r-1,1),r-1,u<0?l.openEnd:r-1):new t(B(l.content,r,h),l.openStart,l.openEnd)}mustMoveInline(){if(!this.$to.parent.isTextblock)return-1;let t,e=this.frontier[this.depth];if(!e.type.isTextblock||!L(this.$to,this.$to.depth,e.type,e.match,!1)||this.$to.depth==this.depth&&(t=this.findCloseLevel(this.$to))&&t.depth==this.depth)return-1;let{depth:r}=this.$to,n=this.$to.after(r);for(;r>1&&n==this.$to.end(--r);)++n;return n}findCloseLevel(t){t:for(let e=Math.min(this.depth,t.depth);e>=0;e--){let{match:r,type:n}=this.frontier[e],i=e<t.depth&&t.end(e+1)==t.pos+(t.depth-(e+1)),s=L(t,e,n,r,i);if(s){for(let r=e-1;r>=0;r--){let{match:e,type:n}=this.frontier[r],i=L(t,r,n,e,!0);if(!i||i.childCount)continue t}return{depth:e,fit:s,move:i?t.doc.resolve(t.after(e+1)):t}}}}close(t){let e=this.findCloseLevel(t);if(!e)return null;for(;this.depth>e.depth;)this.closeFrontierNode();e.fit.childCount&&(this.placed=$(this.placed,e.depth,e.fit)),t=e.move;for(let r=e.depth+1;r<=t.depth;r++){let e=t.node(r),n=e.type.contentMatch.fillBefore(e.content,!0,t.index(r));this.openFrontierNode(e.type,e.attrs,n)}return t}openFrontierNode(t,r=null,n){let i=this.frontier[this.depth];i.match=i.match.matchType(t),this.placed=$(this.placed,this.depth,e.from(t.create(r,n))),this.frontier.push({type:t,match:t.contentMatch})}closeFrontierNode(){let t=this.frontier.pop().match.fillBefore(e.empty,!0);t.childCount&&(this.placed=$(this.placed,this.frontier.length,t))}}function B(t,e,r){return 0==e?t.cutByIndex(r,t.childCount):t.replaceChild(0,t.firstChild.copy(B(t.firstChild.content,e-1,r)))}function $(t,e,r){return 0==e?t.append(r):t.replaceChild(t.childCount-1,t.lastChild.copy($(t.lastChild.content,e-1,r)))}function W(t,e){for(let r=0;r<e;r++)t=t.firstChild.content;return t}function j(t,r,n){if(r<=0)return t;let i=t.content;return r>1&&(i=i.replaceChild(0,j(i.firstChild,r-1,1==i.childCount?n-1:0))),r>0&&(i=t.type.contentMatch.fillBefore(i).append(i),n<=0&&(i=i.append(t.type.contentMatch.matchFragment(i).fillBefore(e.empty,!0)))),t.copy(i)}function L(t,e,r,n,i){let s=t.node(e),o=i?t.indexAfter(e):t.index(e);if(o==s.childCount&&!r.compatibleContent(s.type))return null;let l=n.fillBefore(s.content,!0,o);return l&&!function(t,e,r){for(let n=r;n<e.childCount;n++)if(!t.allowsMarks(e.child(n).marks))return!0;return!1}(r,s.content,o)?l:null}function q(t){return t.spec.defining||t.spec.definingForContent}function G(t,r,n,i,s){if(r<n){let e=t.firstChild;t=t.replaceChild(0,e.copy(G(e.content,r+1,n,i,e)))}if(r>i){let r=s.contentMatchAt(0),n=r.fillBefore(t).append(t);t=n.append(r.matchFragment(n).fillBefore(e.empty,!0))}return t}function H(t,e){let r=[];for(let n=Math.min(t.depth,e.depth);n>=0;n--){let i=t.start(n);if(i<t.pos-(t.depth-n)||e.end(n)>e.pos+(e.depth-n)||t.node(n).type.spec.isolating||e.node(n).type.spec.isolating)break;(i==e.start(n)||n==t.depth&&n==e.depth&&t.parent.inlineContent&&e.parent.inlineContent&&n&&e.start(n-1)==i-1)&&r.push(n)}return r}class K extends f{constructor(t,e,r){super(),this.pos=t,this.attr=e,this.value=r}apply(r){let n=r.nodeAt(this.pos);if(!n)return d.fail("No node at attribute step's position");let i=/* @__PURE__ */Object.create(null);for(let t in n.attrs)i[t]=n.attrs[t];i[this.attr]=this.value;let s=n.type.create(i,null,n.marks);return d.fromReplace(r,this.pos,this.pos+1,new t(e.from(s),0,n.isLeaf?0:1))}getMap(){return p.empty}invert(t){return new K(this.pos,this.attr,t.nodeAt(this.pos).attrs[this.attr])}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new K(e.pos,this.attr,this.value)}toJSON(){return{stepType:"attr",pos:this.pos,attr:this.attr,value:this.value}}static fromJSON(t,e){if("number"!=typeof e.pos||"string"!=typeof e.attr)throw new RangeError("Invalid input for AttrStep.fromJSON");return new K(e.pos,e.attr,e.value)}}f.jsonID("attr",K);class P extends f{constructor(t,e){super(),this.attr=t,this.value=e}apply(t){let e=/* @__PURE__ */Object.create(null);for(let n in t.attrs)e[n]=t.attrs[n];e[this.attr]=this.value;let r=t.type.create(e,t.content,t.marks);return d.ok(r)}getMap(){return p.empty}invert(t){return new P(this.attr,t.attrs[this.attr])}map(t){return this}toJSON(){return{stepType:"docAttr",attr:this.attr,value:this.value}}static fromJSON(t,e){if("string"!=typeof e.attr)throw new RangeError("Invalid input for DocAttrStep.fromJSON");return new P(e.attr,e.value)}}f.jsonID("docAttr",P);let Q=class extends Error{};Q=function t(e){let r=Error.call(this,e);return r.__proto__=t.prototype,r},(Q.prototype=Object.create(Error.prototype)).constructor=Q,Q.prototype.name="TransformError";class U{constructor(t){this.doc=t,this.steps=[],this.docs=[],this.mapping=new h}get before(){return this.docs.length?this.docs[0]:this.doc}step(t){let e=this.maybeStep(t);if(e.failed)throw new Q(e.failed);return this}maybeStep(t){let e=t.apply(this.doc);return e.failed||this.addStep(t,e.doc),e}get docChanged(){return this.steps.length>0}changedRange(){let t=1e9,e=-1e9;for(let r=0;r<this.mapping.maps.length;r++){let n=this.mapping.maps[r];r&&(t=n.map(t,1),e=n.map(e,-1)),n.forEach((r,n,i,s)=>{t=Math.min(t,i),e=Math.max(e,s)})}return 1e9==t?null:{from:t,to:e}}addStep(t,e){this.docs.push(this.doc),this.steps.push(t),this.mapping.appendMap(t.getMap()),this.doc=e}replace(e,r=e,n=t.empty){let i=F(this.doc,e,r,n);return i&&this.step(i),this}replaceWith(r,n,i){return this.replace(r,n,new t(e.from(i),0,0))}delete(e,r){return this.replace(e,r,t.empty)}insert(t,e){return this.replaceWith(t,t,e)}replaceRange(e,r,n){return function(e,r,n,i){if(!i.size)return e.deleteRange(r,n);let s=e.doc.resolve(r),o=e.doc.resolve(n);if(_(s,o,i))return e.step(new k(r,n,i));let l=H(s,o);0==l[l.length-1]&&l.pop();let a=-(s.depth+1);l.unshift(a);for(let t=s.depth,d=s.pos-1;t>0;t--,d--){let e=s.node(t).type.spec;if(e.defining||e.definingAsContext||e.isolating)break;l.indexOf(t)>-1?a=t:s.before(t)==d&&l.splice(1,0,-t)}let p=l.indexOf(a),h=[],c=i.openStart;for(let t=i.content,d=0;;d++){let e=t.firstChild;if(h.push(e),d==i.openStart)break;t=e.content}for(let t=c-1;t>=0;t--){let e=h[t],r=q(e.type);if(r&&!e.sameMarkup(s.node(Math.abs(a)-1)))c=t;else if(r||!e.type.isTextblock)break}for(let d=i.openStart;d>=0;d--){let r=(d+c+1)%(i.openStart+1),a=h[r];if(a)for(let h=0;h<l.length;h++){let c=l[(h+p)%l.length],f=!0;c<0&&(f=!1,c=-c);let d=s.node(c-1),u=s.index(c-1);if(d.canReplaceWith(u,u,a.type,a.marks))return e.replace(s.before(c),f?o.after(c):n,new t(G(i.content,0,i.openStart,r),r,i.openEnd))}}let f=e.steps.length;for(let t=l.length-1;t>=0&&(e.replace(r,n,i),!(e.steps.length>f));t--){let e=l[t];e<0||(r=s.before(e),n=o.after(e))}}(this,e,r,n),this}replaceRangeWith(r,n,i){return function(r,n,i,s){if(!s.isInline&&n==i&&r.doc.resolve(n).parent.content.size){let t=J(r.doc,n,s.type);null!=t&&(n=i=t)}r.replaceRange(n,i,new t(e.from(s),0,0))}(this,r,n,i),this}deleteRange(t,e){return function(t,e,r){let n=t.doc.resolve(e),i=t.doc.resolve(r),s=H(n,i);for(let o=0;o<s.length;o++){let e=s[o],r=o==s.length-1;if(r&&0==e||n.node(e).type.contentMatch.validEnd)return t.delete(n.start(e),i.end(e));if(e>0&&(r||n.node(e-1).canReplace(n.index(e-1),i.indexAfter(e-1))))return t.delete(n.before(e),i.after(e))}for(let o=1;o<=n.depth&&o<=i.depth;o++)if(e-n.start(o)==n.depth-o&&r>n.end(o)&&i.end(o)-r!=i.depth-o&&n.start(o-1)==i.start(o-1)&&n.node(o-1).canReplace(n.index(o-1),i.index(o-1)))return t.delete(n.before(o),r);t.delete(e,r)}(this,t,e),this}lift(r,n){return function(r,n,i){let{$from:s,$to:o,depth:l}=n,a=s.before(l+1),p=o.after(l+1),h=a,c=p,f=e.empty,d=0;for(let t=l,g=!1;t>i;t--)g||s.index(t)>0?(g=!0,f=e.from(s.node(t).copy(f)),d++):h--;let u=e.empty,m=0;for(let t=l,g=!1;t>i;t--)g||o.after(t+1)<o.end(t)?(g=!0,u=e.from(o.node(t).copy(u)),m++):c++;r.step(new v(h,c,a,p,new t(f.append(u),d,m),f.size-d,!0))}(this,r,n),this}join(e,r=1){return function(e,r,n){let i=null,{linebreakReplacement:s}=e.doc.type.schema,o=e.doc.resolve(r-n),l=o.node().type;if(s&&l.inlineContent){let t="pre"==l.whitespace,e=!!l.contentMatch.matchType(s);t&&!e?i=!1:!t&&e&&(i=!0)}let a=e.steps.length;if(!1===i){let t=e.doc.resolve(r+n);I(e,t.node(),t.before(),a)}l.inlineContent&&M(e,r+n-1,l,o.node().contentMatchAt(o.index()),null==i);let p=e.mapping.slice(a),h=p.map(r-n);if(e.step(new k(h,p.map(r+n,-1),t.empty,!0)),!0===i){let t=e.doc.resolve(h);N(e,t.node(),t.before(),e.steps.length)}}(this,e,r),this}wrap(r,n){return function(r,n,i){let s=e.empty;for(let t=i.length-1;t>=0;t--){if(s.size){let e=i[t].type.contentMatch.matchFragment(s);if(!e||!e.validEnd)throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper")}s=e.from(i[t].type.create(i[t].attrs,s))}let o=n.start,l=n.end;r.step(new v(o,l,o,l,new t(s,0,0),i.length,!0))}(this,r,n),this}setBlockType(r,n=r,i,s=null){return function(r,n,i,s,o){if(!s.isTextblock)throw new RangeError("Type given to setBlockType should be a textblock");let l=r.steps.length;r.doc.nodesBetween(n,i,(n,i)=>{let a="function"==typeof o?o(n):o;if(n.isTextblock&&!n.hasMarkup(s,a)&&function(t,e,r){let n=t.resolve(e),i=n.index();return n.parent.canReplaceWith(i,i+1,r)}(r.doc,r.mapping.slice(l).map(i),s)){let o=null;if(s.schema.linebreakReplacement){let t="pre"==s.whitespace,e=!!s.contentMatch.matchType(s.schema.linebreakReplacement);t&&!e?o=!1:!t&&e&&(o=!0)}!1===o&&I(r,n,i,l),M(r,r.mapping.slice(l).map(i,1),s,void 0,null===o);let p=r.mapping.slice(l),h=p.map(i,1),c=p.map(i+n.nodeSize,1);return r.step(new v(h,c,h+1,c-1,new t(e.from(s.create(a,null,n.marks)),0,0),1,!0)),!0===o&&N(r,n,i,l),!1}})}(this,r,n,i,s),this}setNodeMarkup(r,n,i=null,s){return function(r,n,i,s,o){let l=r.doc.nodeAt(n);if(!l)throw new RangeError("No node at given position");i||(i=l.type);let a=i.create(s,null,o||l.marks);if(l.isLeaf)return r.replaceWith(n,n+l.nodeSize,a);if(!i.validContent(l.content))throw new RangeError("Invalid content for node type "+i.name);r.step(new v(n,n+l.nodeSize,n+1,n+l.nodeSize-1,new t(e.from(a),0,0),1,!0))}(this,r,n,i,s),this}setNodeAttribute(t,e,r){return this.step(new K(t,e,r)),this}setDocAttribute(t,e){return this.step(new P(t,e)),this}addNodeMark(t,e){return this.step(new y(t,e)),this}removeNodeMark(t,e){let n=this.doc.nodeAt(t);if(!n)throw new RangeError("No node at position "+t);if(e instanceof r)e.isInSet(n.marks)&&this.step(new w(t,e));else{let r,i=n.marks,s=[];for(;r=e.isInSet(i);)s.push(new w(t,r)),i=r.removeFromSet(i);for(let t=s.length-1;t>=0;t--)this.step(s[t])}return this}split(r,n=1,i){return function(r,n,i=1,s){let o=r.doc.resolve(n),l=e.empty,a=e.empty;for(let t=o.depth,p=o.depth-i,h=i-1;t>p;t--,h--){l=e.from(o.node(t).copy(l));let r=s&&s[h];a=e.from(r?r.type.create(r.attrs,a):o.node(t).copy(a))}r.step(new k(n,n,new t(l.append(a),i,i),!0))}(this,r,n,i),this}addMark(t,e,r){return function(t,e,r,n){let i,s,o=[],l=[];t.doc.nodesBetween(e,r,(t,a,p)=>{if(!t.isInline)return;let h=t.marks;if(!n.isInSet(h)&&p.type.allowsMarkType(n.type)){let p=Math.max(a,e),c=Math.min(a+t.nodeSize,r),f=n.addToSet(h);for(let t=0;t<h.length;t++)h[t].isInSet(f)||(i&&i.to==p&&i.mark.eq(h[t])?i.to=c:o.push(i=new g(p,c,h[t])));s&&s.to==p?s.to=c:l.push(s=new m(p,c,n))}}),o.forEach(e=>t.step(e)),l.forEach(e=>t.step(e))}(this,t,e,r),this}removeMark(t,e,r){return function(t,e,r,n){let s=[],o=0;t.doc.nodesBetween(e,r,(t,l)=>{if(!t.isInline)return;o++;let a=null;if(n instanceof i){let e,r=t.marks;for(;e=n.isInSet(r);)(a||(a=[])).push(e),r=e.removeFromSet(r)}else n?n.isInSet(t.marks)&&(a=[n]):a=t.marks;if(a&&a.length){let n=Math.min(l+t.nodeSize,r);for(let t=0;t<a.length;t++){let r,i=a[t];for(let t=0;t<s.length;t++){let e=s[t];e.step==o-1&&i.eq(s[t].style)&&(r=e)}r?(r.to=n,r.step=o):s.push({style:i,from:Math.max(l,e),to:n,step:o})}}}),s.forEach(e=>t.step(new g(e.from,e.to,e.style)))}(this,t,e,r),this}clearIncompatible(t,e,r){return M(this,t,e,r),this}}export{m as AddMarkStep,y as AddNodeMarkStep,K as AttrStep,P as DocAttrStep,a as MapResult,h as Mapping,g as RemoveMarkStep,w as RemoveNodeMarkStep,v as ReplaceAroundStep,k as ReplaceStep,f as Step,p as StepMap,d as StepResult,U as Transform,Q as TransformError,A as canJoin,T as canSplit,z as dropPoint,C as findWrapping,J as insertPoint,E as joinPoint,R as liftTarget,F as replaceStep};