"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),f=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),m=require("../blots/block.js"),q=require("../blots/break.js"),B=require("../blots/cursor.js"),O=require("../blots/text.js"),F=require("./selection.js"),k=require("../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.js"),A=require("../../../../lodash-es@4.17.21/node_modules/lodash-es/cloneDeep.js"),M=require("../../../../lodash-es@4.17.21/node_modules/lodash-es/isEqual.js"),v=/^[ -~]*$/;class S{constructor(e){this.scroll=e,this.delta=this.getDelta()}applyDelta(e){this.scroll.update();let t=this.scroll.length();this.scroll.batchStart();const n=j(e),s=new f.default;return R(n.ops.slice()).reduce((r,l)=>{const c=f.DeltaExports.Op.length(l);let o=l.attributes||{},u=!1,g=!1;if(l.insert!=null){if(s.retain(c),typeof l.insert=="string"){const h=l.insert;g=!h.endsWith(`
`)&&(t<=r||!!this.scroll.descendant(m.BlockEmbed,r)[0]),this.scroll.insertAt(r,h);const[w,p]=this.scroll.line(r);let L=k.default({},m.bubbleFormats(w));if(w instanceof m.default){const[y]=w.descendant(b.LeafBlot,p);y&&(L=k.default(L,m.bubbleFormats(y)))}o=f.DeltaExports.AttributeMap.diff(L,o)||{}}else if(typeof l.insert=="object"){const h=Object.keys(l.insert)[0];if(h==null)return r;const w=this.scroll.query(h,b.Scope.INLINE)!=null;if(w)(t<=r||this.scroll.descendant(m.BlockEmbed,r)[0])&&(g=!0);else if(r>0){const[p,L]=this.scroll.descendant(b.LeafBlot,r-1);p instanceof O.default?p.value()[L]!==`
`&&(u=!0):p instanceof b.EmbedBlot&&p.statics.scope===b.Scope.INLINE_BLOT&&(u=!0)}if(this.scroll.insertAt(r,h,l.insert[h]),w){const[p]=this.scroll.descendant(b.LeafBlot,r);if(p){const L=k.default({},m.bubbleFormats(p));o=f.DeltaExports.AttributeMap.diff(L,o)||{}}}}t+=c}else if(s.push(l),l.retain!==null&&typeof l.retain=="object"){const h=Object.keys(l.retain)[0];if(h==null)return r;this.scroll.updateEmbedAt(r,h,l.retain[h])}Object.keys(o).forEach(h=>{this.scroll.formatAt(r,c,h,o[h])});const E=u?1:0,d=g?1:0;return t+=E+d,s.retain(E),s.delete(d),r+c+E+d},0),s.reduce((r,l)=>typeof l.delete=="number"?(this.scroll.deleteAt(r,l.delete),r):r+f.DeltaExports.Op.length(l),0),this.scroll.batchEnd(),this.scroll.optimize(),this.update(n)}deleteText(e,t){return this.scroll.deleteAt(e,t),this.update(new f.default().retain(e).delete(t))}formatLine(e,t){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};this.scroll.update(),Object.keys(n).forEach(a=>{this.scroll.lines(e,Math.max(t,1)).forEach(r=>{r.format(a,n[a])})}),this.scroll.optimize();const s=new f.default().retain(e).retain(t,A.default(n));return this.update(s)}formatText(e,t){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};Object.keys(n).forEach(a=>{this.scroll.formatAt(e,t,a,n[a])});const s=new f.default().retain(e).retain(t,A.default(n));return this.update(s)}getContents(e,t){return this.delta.slice(e,e+t)}getDelta(){return this.scroll.lines().reduce((e,t)=>e.concat(t.delta()),new f.default)}getFormat(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0,n=[],s=[];t===0?this.scroll.path(e).forEach(l=>{const[c]=l;c instanceof m.default?n.push(c):c instanceof b.LeafBlot&&s.push(c)}):(n=this.scroll.lines(e,t),s=this.scroll.descendants(b.LeafBlot,e,t));const[a,r]=[n,s].map(l=>{const c=l.shift();if(c==null)return{};let o=m.bubbleFormats(c);for(;Object.keys(o).length>0;){const u=l.shift();if(u==null)return o;o=C(m.bubbleFormats(u),o)}return o});return{...a,...r}}getHTML(e,t){const[n,s]=this.scroll.line(e);if(n){const a=n.length();return n.length()>=s+t&&!(s===0&&t===a)?$(n,s,t,!0):$(this.scroll,e,t,!0)}return""}getText(e,t){return this.getContents(e,t).filter(n=>typeof n.insert=="string").map(n=>n.insert).join("")}insertContents(e,t){const n=j(t),s=new f.default().retain(e).concat(n);return this.scroll.insertContents(e,n),this.update(s)}insertEmbed(e,t,n){return this.scroll.insertAt(e,t,n),this.update(new f.default().retain(e).insert({[t]:n}))}insertText(e,t){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};return t=t.replace(/\r\n/g,`
`).replace(/\r/g,`
`),this.scroll.insertAt(e,t),Object.keys(n).forEach(s=>{this.scroll.formatAt(e,t.length,s,n[s])}),this.update(new f.default().retain(e).insert(t,A.default(n)))}isBlank(){if(this.scroll.children.length===0)return!0;if(this.scroll.children.length>1)return!1;const e=this.scroll.children.head;if((e==null?void 0:e.statics.blotName)!==m.default.blotName)return!1;const t=e;return t.children.length>1?!1:t.children.head instanceof q.default}removeFormat(e,t){const n=this.getText(e,t),[s,a]=this.scroll.line(e+t);let r=0,l=new f.default;s!=null&&(r=s.length()-a,l=s.delta().slice(a,a+r-1).insert(`
`));const o=this.getContents(e,t+r).diff(new f.default().insert(n).concat(l)),u=new f.default().retain(e).concat(o);return this.applyDelta(u)}update(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:[],n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:void 0;const s=this.delta;if(t.length===1&&t[0].type==="characterData"&&t[0].target.data.match(v)&&this.scroll.find(t[0].target)){const a=this.scroll.find(t[0].target),r=m.bubbleFormats(a),l=a.offset(this.scroll),c=t[0].oldValue.replace(B.default.CONTENTS,""),o=new f.default().insert(c),u=new f.default().insert(a.value()),g=n&&{oldRange:N(n.oldRange,-l),newRange:N(n.newRange,-l)};e=new f.default().retain(l).concat(o.diff(u,g)).reduce((d,h)=>h.insert?d.insert(h.insert,r):d.push(h),new f.default),this.delta=s.compose(e)}else this.delta=this.getDelta(),(!e||!M.default(s.compose(e),this.delta))&&(e=s.diff(this.delta,n));return e}}function T(i,e,t){if(i.length===0){const[d]=D(t.pop());return e<=0?`</li></${d}>`:`</li></${d}>${T([],e-1,t)}`}const[{child:n,offset:s,length:a,indent:r,type:l},...c]=i,[o,u]=D(l);if(r>e)return t.push(l),r===e+1?`<${o}><li${u}>${$(n,s,a)}${T(c,r,t)}`:`<${o}><li>${T(i,e+1,t)}`;const g=t[t.length-1];if(r===e&&l===g)return`</li><li${u}>${$(n,s,a)}${T(c,r,t)}`;const[E]=D(t.pop());return`</li></${E}>${T(i,e-1,t)}`}function $(i,e,t){let n=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1;if("html"in i&&typeof i.html=="function")return i.html(e,t);if(i instanceof O.default)return O.escapeText(i.value().slice(e,e+t));if(i instanceof b.ParentBlot){if(i.statics.blotName==="list-container"){const o=[];return i.children.forEachAt(e,t,(u,g,E)=>{const d="formats"in u&&typeof u.formats=="function"?u.formats():{};o.push({child:u,offset:g,length:E,indent:d.indent||0,type:d.list})}),T(o,-1,[])}const s=[];if(i.children.forEachAt(e,t,(o,u,g)=>{s.push($(o,u,g))}),n||i.statics.blotName==="list")return s.join("");const{outerHTML:a,innerHTML:r}=i.domNode,[l,c]=a.split(`>${r}<`);return l==="<table"?`<table style="border: 1px solid #000;">${s.join("")}<${c}`:`${l}>${s.join("")}<${c}`}return i.domNode instanceof Element?i.domNode.outerHTML:""}function C(i,e){return Object.keys(e).reduce((t,n)=>{if(i[n]==null)return t;const s=e[n];return s===i[n]?t[n]=s:Array.isArray(s)?s.indexOf(i[n])<0?t[n]=s.concat([i[n]]):t[n]=s:t[n]=[s,i[n]],t},{})}function D(i){const e=i==="ordered"?"ol":"ul";switch(i){case"checked":return[e,' data-list="checked"'];case"unchecked":return[e,' data-list="unchecked"'];default:return[e,""]}}function j(i){return i.reduce((e,t)=>{if(typeof t.insert=="string"){const n=t.insert.replace(/\r\n/g,`
`).replace(/\r/g,`
`);return e.insert(n,t.attributes)}return e.push(t)},new f.default)}function N(i,e){let{index:t,length:n}=i;return new F.Range(t+e,n)}function R(i){const e=[];return i.forEach(t=>{typeof t.insert=="string"?t.insert.split(`
`).forEach((s,a)=>{a&&e.push({insert:`
`,attributes:t.attributes}),s&&e.push({insert:s,attributes:t.attributes})}):e.push(t)}),e}exports.default=S;
