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