"use strict";var Bt=Object.defineProperty;var Gt=(a,e,n)=>e in a?Bt(a,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):a[e]=n;var H=(a,e,n)=>Gt(a,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ce=require("@univerjs/core"),Ie=require("rxjs"),Lt=500,qt=500,Kt=10,Tt=5*1024*1024,At=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"];var Ze={},et={},tt={},mt;function Ht(){if(mt)return tt;mt=1,Object.defineProperty(tt,"__esModule",{value:!0});function a(t,r){if(Array.isArray(r))return!1;for(let u in t)if(!n(t[u],r[u]))return!1;for(let u in r)if(t[u]===void 0)return!1;return!0}function e(t,r){if(!Array.isArray(r)||t.length!==r.length)return!1;for(let u=0;u_!=null&&typeof _=="object"&&!Array.isArray(_),t=(_,s)=>typeof _==typeof s?_>s:typeof _=="string"&&typeof s=="number";function r(_,s){for(let c in _){const h=c;s.write(h,_[h])}}a.isValidPathItem=_=>typeof _=="number"||typeof _=="string"&&_!=="__proto__";class u{constructor(s=null){this.parents=[],this.indexes=[],this.lcIdx=-1,this.idx=-1,this.container=s}ascend(){e(this.parents.length===this.indexes.length/2),this.idx===0?this.parents.length?(this.lcIdx=this.indexes.pop(),this.container=this.parents.pop(),this.idx=this.indexes.pop()):(this.lcIdx=0,this.idx=-1):(e(this.idx>0),this.idx--,n(this.container[this.idx])&&this.idx--)}getPath(){const s=[];let c=this.container,h=this.parents.length-1,U=this.idx;for(;U>=0;)s.unshift(c[U]),U===0?(U=this.indexes[h*2],c=this.parents[h--]):U-=n(c[U-1])?2:1;return s}}class o extends u{get(){return this.container?this.container.slice(this.idx+1):null}getKey(){return e(this.container!=null,"Invalid call to getKey before cursor descended"),this.container[this.idx]}getComponent(){let s;return this.container&&this.container.length>this.idx+1&&n(s=this.container[this.idx+1])?s:null}descendFirst(){let s=this.idx+1;if(!this.container||s>=this.container.length||n(this.container[s])&&s+1>=this.container.length)return!1;n(this.container[s])&&s++;const c=this.container[s];return Array.isArray(c)?(this.indexes.push(this.idx),this.parents.push(this.container),this.indexes.push(s),this.idx=0,this.container=c):this.idx=s,!0}nextSibling(){if(e(this.parents.length===this.indexes.length/2),this.idx>0||this.parents.length===0)return!1;const s=this.indexes[this.indexes.length-1]+1,c=this.parents[this.parents.length-1];return s>=c.length?!1:(e(!isNaN(s)),this.indexes[this.indexes.length-1]=s,this.container=c[s],!0)}_init(s,c,h,U){this.container=s,this.idx=c,this.parents=h.slice(),this.indexes=U.slice()}clone(){const s=new o;return s._init(this.container,this.idx,this.parents,this.indexes),s}*[Symbol.iterator](){if(this.descendFirst()){do yield this.getKey();while(this.nextSibling());this.ascend()}}traverse(s,c){const h=this.getComponent();h&&c(h,s);for(const U of this)s&&s.descend(U),this.traverse(s,c),s&&s.ascend()}eachPick(s,c){this.traverse(s,(h,U)=>{h.p!=null&&c(h.p,U)})}eachDrop(s,c){this.traverse(s,(h,U)=>{h.d!=null&&c(h.d,U)})}}a.ReadCursor=o;class l extends u{constructor(s=null){super(s),this.pendingDescent=[],this._op=s}flushDescent(){e(this.parents.length===this.indexes.length/2),this.container===null&&(this._op=this.container=[]);for(let s=0;s-1&&(this.lcIdx=h)}for(this.indexes.push(this.idx),this.parents.push(this.container),this.lcIdx!==-1&&(e(t(c,this.container[this.lcIdx][0])),h=this.lcIdx+1,this.lcIdx=-1);hthis.write(c,h)),this.reset(),this}writeMove(s,c,h=0){return this.writeAtPath(s,"p",h).writeAtPath(c,"d",h)}getPath(){const s=super.getPath();return s.push(...this.pendingDescent),s}}a.WriteCursor=l,a.writeCursor=()=>new l,a.readCursor=_=>new o(_);function E(_,s,c){let h,U;U=h=_?_.descendFirst():!1;function Y(pe){let Z;for(;U;){const Ce=Z=_.getKey();if(pe!=null){let Le=!1;if(s&&typeof Ce=="number"&&(Z=s(Ce,_.getComponent()),Z<0&&(Z=~Z,Le=!0)),t(Z,pe))return null;if(Z===pe&&!Le)return _}c&&typeof Z=="number"&&c(Z,_.getComponent()),U=_.nextSibling()}return null}return Y.end=()=>{h&&_.ascend()},Y}a.advancer=E;function W(_,s,c){let h,U,Y,pe;for(h=U=_&&_.descendFirst(),Y=pe=s&&s.descendFirst();h||Y;){let Z=h?_.getKey():null,Ce=Y?s.getKey():null;Z!==null&&Ce!==null&&(t(Ce,Z)?Ce=null:Z!==Ce&&(Z=null)),c(Z==null?Ce:Z,Z!=null?_:null,Ce!=null?s:null),Z!=null&&h&&(h=_.nextSibling()),Ce!=null&&Y&&(Y=s.nextSibling())}U&&_.ascend(),pe&&s.ascend()}a.eachChildOf=W}(at)),at}var lt={},wt;function jt(){return wt||(wt=1,function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.ConflictType=void 0,function(e){e[e.RM_UNEXPECTED_CONTENT=1]="RM_UNEXPECTED_CONTENT",e[e.DROP_COLLISION=2]="DROP_COLLISION",e[e.BLACKHOLE=3]="BLACKHOLE"}(a.ConflictType||(a.ConflictType={}))}(lt)),lt}var Fe={},Ve={},vt;function ht(){return vt||(vt=1,Object.defineProperty(Ve,"__esModule",{value:!0}),Ve.uniToStrPos=Ve.strPosToUni=void 0,Ve.strPosToUni=(a,e=a.length)=>{let n=0,t=0;for(;t=55296&&r<=57343&&(n++,t++)}if(t!==e)throw Error("Invalid offset - splits unicode bytes");return t-n},Ve.uniToStrPos=(a,e)=>{let n=0;for(;e>0;e--){const t=a.charCodeAt(n);n+=t>=55296&&t<=57343?2:1}return n}),Ve}var ut={},It;function ft(){return It||(It=1,function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.uniSlice=a.dlen=a.eachOp=void 0;const e=ht(),n=f=>{if(!Array.isArray(f))throw Error("Op must be an array of components");let y=null;for(let v=0;v0))throw Error("Inserts cannot be empty");break;case"number":if(!(L>0))throw Error("Skip components must be >0");if(typeof y=="number")throw Error("Adjacent skip components should be combined");break}y=L}if(typeof y=="number")throw Error("Op has a trailing skip")};function t(f,y){let v=0,L=0;for(let z=0;z{L(y(z,q,De))}),s(v)}const u=f=>f,o=f=>r(f,u);a.dlen=f=>typeof f=="number"?f:e.strPosToUni(f);const l=f=>y=>{if(!(!y||y.d===0||y.d===""))if(f.length===0)f.push(y);else if(typeof y==typeof f[f.length-1])if(typeof y=="object"){const v=f[f.length-1];v.d=typeof v.d=="string"&&typeof y.d=="string"?v.d+y.d:a.dlen(v.d)+a.dlen(y.d)}else f[f.length-1]+=y;else f.push(y)},E=f=>typeof f=="number"?f:typeof f=="string"?e.strPosToUni(f):typeof f.d=="number"?f.d:e.strPosToUni(f.d);a.uniSlice=(f,y,v)=>{const L=e.uniToStrPos(f,y),z=v==null?1/0:e.uniToStrPos(f,v);return f.slice(L,z)};const W=(f,y,v)=>typeof f=="number"?v==null?f-y:Math.min(f,v)-y:a.uniSlice(f,y,v),_=f=>{let y=0,v=0;return{take:(q,De)=>{if(y===f.length)return q===-1?null:q;const de=f[y];let ne;if(typeof de=="number")return q===-1||de-v<=q?(ne=de-v,++y,v=0,ne):(v+=q,q);if(typeof de=="string"){if(q===-1||De==="i"||e.strPosToUni(de.slice(v))<=q)return ne=de.slice(v),++y,v=0,ne;{const le=v+e.uniToStrPos(de.slice(v),q);return ne=de.slice(v,le),v=le,ne}}else{if(q===-1||De==="d"||a.dlen(de.d)-v<=q)return ne={d:W(de.d,v)},++y,v=0,ne;{let le=W(de.d,v,v+q);return v+=q,{d:le}}}},peek:()=>f[y]}},s=f=>(f.length>0&&typeof f[f.length-1]=="number"&&f.pop(),f);function c(f,y,v){if(v!=="left"&&v!=="right")throw Error("side ("+v+") must be 'left' or 'right'");n(f),n(y);const L=[],z=l(L),{take:q,peek:De}=_(f);for(let ne=0;ne0;)Se=q(ye,"i"),z(Se),typeof Se!="string"&&(ye-=E(Se));break;case"string":v==="left"&&typeof De()=="string"&&z(q(-1)),z(e.strPosToUni(le));break;case"object":for(ye=a.dlen(le.d);ye>0;)switch(Se=q(ye,"i"),typeof Se){case"number":ye-=Se;break;case"string":z(Se);break;case"object":ye-=a.dlen(Se.d)}break}}let de;for(;de=q(-1);)z(de);return s(L)}function h(f,y){n(f),n(y);const v=[],L=l(v),{take:z}=_(f);for(let De=0;De0;)le=z(ne,"d"),L(le),typeof le!="object"&&(ne-=E(le));break;case"string":L(de);break;case"object":ne=a.dlen(de.d);let ye=0;for(;ye{let v=0;for(let L=0;Lv;L++){const z=y[L];switch(typeof z){case"number":{v+=z;break}case"string":const q=e.strPosToUni(z);v+=q,f+=q;break;case"object":f-=Math.min(a.dlen(z.d),f-v);break}}return f},Y=(f,y)=>typeof f=="number"?U(f,y):f.map(v=>U(v,y));function pe(f,y,v){return r(f,(L,z)=>typeof L=="object"&&typeof L.d=="number"?{d:v.slice(y,z,z+L.d)}:L)}function Z(f){return r(f,y=>{switch(typeof y){case"object":if(typeof y.d=="number")throw Error("Cannot invert text op: Deleted characters missing from operation. makeInvertible must be called first.");return y.d;case"string":return{d:y};case"number":return y}})}function Ce(f){return r(f,y=>typeof y=="object"&&typeof y.d=="string"?{d:e.strPosToUni(y.d)}:y)}function Le(f){let y=!0;return t(f,v=>{typeof v=="object"&&typeof v.d=="number"&&(y=!1)}),y}function Oe(f){return{name:"text-unicode",uri:"http://sharejs.org/types/text-unicode",trim:s,normalize:o,checkOp:n,create(y=""){if(typeof y!="string")throw Error("Initial data must be a string");return f.create(y)},apply(y,v){n(v);const L=f.builder(y);for(let z=0;zy.length===0}}a.default=Oe}(ut)),ut}var rt={},Ct;function Vt(){if(Ct)return rt;Ct=1,Object.defineProperty(rt,"__esModule",{value:!0});const a=ft(),e=ht();function n(t,r){return{get:t,getLength(){return t().length},insert(u,o,l){const E=e.strPosToUni(t(),u);return r([E,o],l)},remove(u,o,l){const E=e.strPosToUni(t(),u);return r([E,{d:o}],l)},_onOp(u){a.eachOp(u,(o,l,E)=>{switch(typeof o){case"string":this.onInsert&&this.onInsert(E,o);break;case"object":const W=a.dlen(o.d);this.onRemove&&this.onRemove(E,W)}})},onInsert:null,onRemove:null}}return rt.default=n,n.provides={text:!0},rt}var Et;function zt(){return Et||(Et=1,function(a){var e=Fe.__createBinding||(Object.create?function(c,h,U,Y){Y===void 0&&(Y=U),Object.defineProperty(c,Y,{enumerable:!0,get:function(){return h[U]}})}:function(c,h,U,Y){Y===void 0&&(Y=U),c[Y]=h[U]}),n=Fe.__setModuleDefault||(Object.create?function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}:function(c,h){c.default=h}),t=Fe.__importStar||function(c){if(c&&c.__esModule)return c;var h={};if(c!=null)for(var U in c)Object.hasOwnProperty.call(c,U)&&e(h,c,U);return n(h,c),h},r=Fe.__importDefault||function(c){return c&&c.__esModule?c:{default:c}};Object.defineProperty(a,"__esModule",{value:!0}),a.type=a.remove=a.insert=void 0;const u=ht(),o=t(ft()),l=r(Vt()),E={create(c){return c},toString(c){return c},builder(c){if(typeof c!="string")throw Error("Invalid document snapshot: "+c);const h=[];return{skip(U){let Y=u.uniToStrPos(c,U);if(Y>c.length)throw Error("The op is too long for this document");h.push(c.slice(0,Y)),c=c.slice(Y)},append(U){h.push(U)},del(U){c=c.slice(u.uniToStrPos(c,U))},build(){return h.join("")+c}}},slice:o.uniSlice},W=o.default(E),_=Object.assign(Object.assign({},W),{api:l.default});a.type=_,a.insert=(c,h)=>h.length===0?[]:c===0?[h]:[c,h],a.remove=(c,h)=>o.dlen(h)===0?[]:c===0?[{d:h}]:[c,{d:h}];var s=ft();Object.defineProperty(a,"makeType",{enumerable:!0,get:function(){return s.default}})}(Fe)),Fe}var Dt;function Xt(){return Dt||(Dt=1,function(a){var e=et.__importDefault||function(i){return i&&i.__esModule?i:{default:i}};Object.defineProperty(a,"__esModule",{value:!0}),a.editOp=a.replaceOp=a.insertOp=a.moveOp=a.removeOp=a.type=void 0;const n=e(Ht()),t=e(Ft()),r=Mt(),u=jt();function o(i,d){if(!i)throw new Error(d)}a.type={name:"json1",uri:"http://sharejs.org/types/JSONv1",readCursor:r.readCursor,writeCursor:r.writeCursor,create:i=>i,isNoop:i=>i==null,setDebug(i){},registerSubtype:Z,checkValidOp:z,normalize:q,apply:De,transformPosition:de,compose:ne,tryTransform:st,transform:Wt,makeInvertible:Se,invert:le,invertWithDoc:Rt,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,O)=>yt(()=>!0,i,d,O),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},a.type),{transform:(d,O,D)=>yt(i,d,O,D)})};const l=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const W=i=>Array.isArray(i)?i.slice():i!==null&&typeof i=="object"?Object.assign({},i):i,_=i=>i&&(i.p!=null||i.r!==void 0),s=i=>i&&(i.d!=null||i.i!==void 0);function c(i,d){return o(i!=null),typeof d=="number"?(o(Array.isArray(i),"Invalid key - child is not an array"),(i=i.slice()).splice(d,1)):(o(E(i),"Invalid key - child is not an object"),delete(i=Object.assign({},i))[d]),i}function h(i,d,O){return typeof d=="number"?(o(i!=null,"Container is missing for key"),o(Array.isArray(i),"Cannot use numerical key for object container"),o(i.length>=d,"Cannot insert into out of bounds index"),i.splice(d,0,O)):(o(E(i),"Cannot insert into missing item"),o(i[d]===void 0,"Trying to overwrite value at key. Your op needs to remove it first"),i[d]=O),O}a.removeOp=(i,d=!0)=>r.writeCursor().writeAtPath(i,"r",d).get(),a.moveOp=(i,d)=>r.writeCursor().writeMove(i,d).get(),a.insertOp=(i,d)=>r.writeCursor().writeAtPath(i,"i",d).get(),a.replaceOp=(i,d,O)=>r.writeCursor().at(i,D=>{D.write("r",d),D.write("i",O)}).get(),a.editOp=(i,d,O,D=!1)=>r.writeCursor().at(i,b=>y(b,d,O,D)).get();const U=(i,d)=>i!=null&&(typeof d=="number"?Array.isArray(i):typeof i=="object"),Y=(i,d)=>U(i,d)?i[d]:void 0,pe={};function Z(i){let d=i.type?i.type:i;d.name&&(pe[d.name]=d),d.uri&&(pe[d.uri]=d)}const Ce=i=>{const d=pe[i];if(d)return d;throw Error("Missing type: "+i)};Z(zt());const Le=(i,d)=>i+d;Z({name:"number",apply:Le,compose:Le,invert:i=>-i,transform:i=>i});const Oe=i=>i==null?null:i.et?Ce(i.et):i.es?pe["text-unicode"]:i.ena!=null?pe.number:null,f=i=>i.es?i.es:i.ena!=null?i.ena:i.e,y=(i,d,O,D=!1)=>{const[b,C]=typeof d=="string"?[Ce(d),d]:[d,d.name];!D&&b.isNoop&&b.isNoop(O)||(C==="number"?i.write("ena",O):C==="text-unicode"?i.write("es",O):(i.write("et",C),i.write("e",O)))};function v(i){o(typeof i=="number"),o(i>=0),o(i===(0|i))}function L(i){typeof i=="number"?v(i):o(typeof i=="string")}function z(i){if(i===null)return;const d=new Set,O=new Set,D=C=>{let R=!0,N=!1;for(let g in C){const m=C[g];if(R=!1,o(g==="p"||g==="r"||g==="d"||g==="i"||g==="e"||g==="es"||g==="ena"||g==="et","Invalid component item '"+g+"'"),g==="p")v(m),o(!d.has(m)),d.add(m),o(C.r===void 0);else if(g==="d")v(m),o(!O.has(m)),O.add(m),o(C.i===void 0);else if(g==="e"||g==="es"||g==="ena"){o(!N),N=!0;const w=Oe(C);o(w,"Missing type in edit"),w.checkValidOp&&w.checkValidOp(f(C))}}o(!R)},b=(C,R,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");R||L(C[0]);let g=1,m=0,w=0;for(let I=0;I{const R=Oe(b);if(R){const g=f(b);y(C,R,R.normalize?R.normalize(g):g)}for(const g of["r","p","i","d"])if(b[g]!==void 0){const m=g==="p"||g==="d"?(N=b[g],O[N]==null&&(O[N]=d++),O[N]):b[g];C.write(g,m)}var N}),D.get()}function De(i,d){if(z(d),d===null)return i;const O=[];return function D(b,C){let R=b,N=0,g={root:b},m=0,w=g,I="root";function $(){for(;m=N;I--)b=D(b,C[I]);for(--N;N>=0;N--){const I=C[N];if(typeof I!="object"){const $=R.pop();b=b===Y($,I)?$:b===void 0?c($,I):(m=I,w=b,(g=W(g=$))[m]=w,g)}else _(I)&&(o(b!==void 0,"Cannot pick up or remove undefined"),I.p!=null&&(O[I.p]=b),b=void 0)}var g,m,w;return b}(i,d),d)}function de(i,d){i=i.slice(),z(d);const O=r.readCursor(d);let D,b,C=!1;const R=[];for(let g=0;;g++){const m=i[g],w=O.getComponent();if(w&&(w.r!==void 0?C=!0:w.p!=null&&(C=!1,D=w.p,b=g)),g>=i.length)break;let I=0;const $=r.advancer(O,void 0,(p,M)=>{_(M)&&I++});R.unshift($);const x=$(m);if(typeof m=="number"&&(i[g]-=I),!x)break}if(R.forEach(g=>g.end()),C)return null;const N=()=>{let g=0;if(D!=null){const m=O.getPath();g=m.length,i=m.concat(i.slice(b))}for(;gs(M)?~(p-$):p-$,(p,M)=>{s(M)&&$++})(m);if(typeof m=="number"&&(i[g]+=$),!x)break}};return D!=null?O.eachDrop(null,g=>{g===D&&N()}):N(),i}function ne(i,d){if(z(i),z(d),i==null)return d;if(d==null)return i;let O=0;const D=r.readCursor(i),b=r.readCursor(d),C=r.writeCursor(),R=[],N=[],g=[],m=[],w=[],I=[],$=new Set;D.traverse(null,p=>{p.p!=null&&(g[p.p]=D.clone())}),b.traverse(null,p=>{p.d!=null&&(m[p.d]=b.clone())});const x=r.writeCursor();return function p(M,re,te,K,se,$e,we,_e){o(re||te);const oe=l(re),Pe=l(te),Ae=!!Pe&&Pe.r!==void 0,qe=!!oe&&oe.i!==void 0,Te=oe?oe.d:null,Ee=Pe?Pe.p:null,Re=($e||Ae)&&Ee==null;if(Ee!=null)K=m[Ee],we=N[Ee]=new r.WriteCursor;else if(Pe&&Pe.r!==void 0)K=null;else{const P=l(K);P&&P.d!=null&&(K=null)}const Q=l(K);if(Te!=null)if(M=g[Te],_e=R[Te]=new r.WriteCursor,Re)$e&&!Ae&&_e.write("r",!0);else{const P=w[Te]=O++;we.write("d",P)}else if(oe&&oe.i!==void 0)M=null;else{const P=l(M);P&&P.p!=null&&(M=null)}let T;qe?(o(se===void 0),T=oe.i):T=se;const B=(Ee==null?!qe||$e||Ae:T===void 0)?null:we.getComponent();if(Ee!=null){if(!(se!==void 0||qe)){const P=Te!=null?w[Te]:O++;I[Ee]=P,_e.write("p",P)}}else Ae&&(qe||se!==void 0||(Pe.r,_e.write("r",Pe.r)));const A=Re?null:Oe(oe),S=Oe(Q);if((A||S)&&(A&&A.name,S&&S.name),A&&S){o(A===S);const P=f(oe),G=f(Q),ge=A.compose(P,G);y(we,A,ge),$.add(Q)}else A?y(we,A,f(oe)):S&&(y(we,S,f(Q)),$.add(Q));const j=typeof T=="object"&&T!=null;let J=!1,X=0,ee=0,he=0,fe=0,ae=0;const be=r.advancer(K,(P,G)=>s(G)?fe-P-1:P-fe,(P,G)=>{s(G)&&fe++}),V=r.advancer(M,(P,G)=>_(G)?X-P-1:P-X,(P,G)=>{_(G)&&X++});if(r.eachChildOf(re,te,(P,G,ge)=>{let ve,We,Ke=P,Me=P,Ye=P;if(typeof P=="number"){let me=P+he;We=be(me),Me=me+fe;let ue=P+ee;ve=V(ue),s(l(We))&&(ve=null),Ke=ue+X,Ye=P+ae,o(Ke>=0,"p1PickKey is negative"),o(Me>=0,"p2DropKey is negative");const je=s(l(G)),xe=_(l(ge));(je||xe&&!Re)&&ae--,je&&ee--,xe&&he--}else ve=V(P),We=be(P);_e.descend(Ke),we.descend(Me);const Xe=j&&!s(l(G))?T[Ye]:void 0,ke=p(ve,G,ge,We,Xe,Re,we,_e);var Ne,k,ie;j&&!Re?Xe!==ke&&(J||(T=Array.isArray(T)?T.slice():Object.assign({},T),J=!0),Ne=T,ie=ke,typeof(k=Ye)=="number"?(o(Array.isArray(Ne)),o(kp.get()),N.map(p=>p.get()),D.traverse(C,(p,M)=>{const re=p.p;if(re!=null){const te=w[re];te!=null&&M.write("p",te);const K=R[re];K&&K.get(),K&&M.mergeTree(K.get())}else p.r!==void 0&&M.write("r",p.r)}),C.reset(),C.get(),b.traverse(C,(p,M)=>{const re=p.d;if(re!=null){const K=I[re];K!=null&&M.write("d",K);const se=N[re];se&&M.mergeTree(se.get())}else p.i!==void 0&&M.write("i",p.i);const te=Oe(p);te&&!$.has(p)&&y(M,te,f(p))}),C.get()}function le(i){if(i==null)return null;const d=new r.ReadCursor(i),O=new r.WriteCursor;let D;const b=[],C=[];return function R(N,g,m){const w=N.getComponent();let I,$=!1;if(w){w.p!=null&&(g.write("d",w.p),b[w.p]=N.clone()),w.r!==void 0&&g.write("i",w.r),w.d!=null&&(g.write("p",w.d),m=void 0),w.i!==void 0&&(m=I=w.i);const p=Oe(w);p&&(m===void 0?(D||(D=new Set),D.add(w)):(f(w),m=p.apply(m,f(w)),$=!0))}let x=0;for(const p of N){g.descend(p);const M=typeof p=="number"?p-x:p,re=Y(m,M);s(N.getComponent())&&x++;const te=R(N,g,re);if(m!==void 0&&te!==void 0){if($||($=!0,m=W(m)),!U(m,M))throw Error("Cannot modify child - invalid operation");m[M]=te}g.ascend()}if(I===void 0)return $?m:void 0;g.write("r",m)}(d,O,void 0),D&&(O.reset(),function R(N,g,m){const w=g.getComponent();if(w){const p=w.d;if(p!=null&&(N=b[p],m=C[p]=r.writeCursor()),D.has(w)){const M=Oe(w);if(!M.invert)throw Error(`Cannot invert subtype ${M.name}`);y(m,M,M.invert(f(w)))}}let I=0,$=0;const x=r.advancer(N,(p,M)=>_(M)?I-p-1:p-I,(p,M)=>{_(M)&&I++});for(const p of g)if(typeof p=="number"){const M=p-$,re=x(M),te=M+I;m.descend(te),R(re,g,m),s(g.getComponent())&&$++,m.ascend()}else m.descend(p),R(x(p),g,m),m.ascend();x.end()}(d.clone(),d,O),C.length&&(O.reset(),d.traverse(O,(R,N)=>{const g=R.p;if(g!=null){const m=C[g];m&&m.get(),m&&N.mergeTree(m.get())}}))),O.get()}const ye=(i,d)=>i.some(O=>typeof O=="object"&&(Array.isArray(O)?ye(O,d):d(O)));function Se(i,d){if(i==null||!ye(i,g=>{var m;return g.r!==void 0||((m=Oe(g))===null||m===void 0?void 0:m.makeInvertible)!=null}))return i;const O=new r.ReadCursor(i),D=new r.WriteCursor;let b=!1;const C=[],R=[],N=(g,m,w)=>{const I=g.getComponent();let $=!1;if(I){I.d!=null&&m.write("d",I.d),I.i!==void 0&&m.write("i",I.i);const p=I.p;if(p!=null&&(C[p]=g.clone(),o(w!==void 0,"Operation picks up at an invalid key"),R[p]=w,m.write("p",I.p)),I.r!==void 0&&w===void 0)throw Error("Invalid doc / op in makeInvertible: removed item missing from doc");const M=Oe(I);M&&(M.makeInvertible?b=!0:y(m,M,f(I),!0))}let x=0;for(const p of g){m.descend(p);const M=typeof p=="number"?p-x:p,re=Y(w,M),te=N(g,m,re);re!==te&&($||($=!0,w=W(w)),te===void 0?(w=c(w,M),typeof p=="number"&&x++):w[M]=te),m.ascend()}return I&&(I.r!==void 0?(m.write("r",t.default(w)),w=void 0):I.p!=null&&(w=void 0)),w};return N(O,D,d),D.get(),b&&(D.reset(),function g(m,w,I,$,x){const p=w.getComponent();if(p){p.i!==void 0?($=p.i,x=!0):p.d!=null&&($=R[p.d],m=C[p.d],x=!1,p.d);let K=Oe(p);if(K&&K.makeInvertible){const se=f(p);y(I,K,K.makeInvertible(se,$),!0)}}let M=0,re=0;const te=r.advancer(m,(K,se)=>_(se)?M-K-1:K-M,(K,se)=>{_(se)&&M++});for(const K of w)if(typeof K=="number"){const se=K-re,$e=te(se),we=se+M,_e=Y($,x?se:we);I.descend(K),g($e,w,I,_e,x),s(w.getComponent())&&re++,I.ascend()}else{const se=Y($,K);I.descend(K),g(te(K),w,I,se,x),I.ascend()}te.end()}(O.clone(),O,D,d,!1)),D.get()}function Rt(i,d){return le(Se(i,d))}const it=i=>{if(i==null)return null;const d=i.slice();for(let O=0;O_(ge)?ae-G-1:G-ae,(G,ge)=>{_(ge)&&ae++}),P=r.advancer(T);for(const G of A)if(typeof G=="number"){const ge=G-be,ve=V(ge);be+=+Q(P(ge+ae),ve,A,S,j)}else{const ge=V(G);Q(P(G),ge,A,S,j)}return V.end(),P.end(),ee}(we,_e,_e.clone(),null,null),g.map(Q=>Q&&Q.get()),b)return{ok:!1,conflict:b};M.map(Q=>!!Q);const Pe=[];let Ae=null;(function Q(T,B,A,S,j){let J=!1;const X=l(B);if(_(X)){const V=X.p;V!=null?(A=g[V],S=re[V]=r.writeCursor(),J=!0,j=null):(A=null,j=B.clone())}else s(l(A))&&(A=null);const ee=T.getComponent();if(ee){const V=ee.p;V!=null?(j&&($[V]=j),Pe[V]=j||D===1&&J?null:S.getComponent(),C[V]=T.clone(),A&&(I[V]=A.clone())):ee.r!==void 0&&(j||S.write("r",!0),(j||J)&&(Ae==null&&(Ae=new Set),Ae.add(ee)))}let he=0,fe=0;const ae=r.advancer(B,void 0,(V,P)=>{_(P)&&he++}),be=r.advancer(A,(V,P)=>s(P)?~(V-fe):V-fe,(V,P)=>{s(P)&&fe++});if(T)for(const V of T)if(typeof V=="string"){const P=ae(V),G=be(V);S.descend(V),Q(T,P,G,S,j),S.ascend()}else{const P=ae(V),G=V-he,ge=_(l(P))?null:be(G),ve=G+fe;o(ve>=0),S.descend(ve),Q(T,P,ge,S,j),S.ascend()}ae.end(),be.end()})(we,_e,_e.clone(),oe,null),oe.reset();let qe=[];if(function Q(T,B,A,S,j,J){o(B);const X=B.getComponent();let ee=l(S),he=!1;const fe=(k,ie,me)=>k?a.moveOp(k.getPath(),ie.getPath()):a.insertOp(ie.getPath(),me.i);if(s(X)){const k=X.d;k!=null&&(R[k]=B.clone());const ie=k!=null?Pe[k]:null;let me=!1;if(X.i!==void 0||k!=null&&ie){let ue;ee&&(ee.i!==void 0||(ue=ee.d)!=null&&!p[ue])&&(me=ue!=null?k!=null&&k===K[ue]:n.default(ee.i,X.i),me||ue!=null&&D!==1&&K[ue]!=null||b==null&&(b={type:u.ConflictType.DROP_COLLISION,op1:fe(k!=null?C[k]:null,B,X),op2:fe(ue!=null?N[ue]:null,S,ee)})),me||(J?b==null&&(b={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:fe(k!=null?C[k]:null,B,X),op2:a.removeOp(J.getPath())}):(k!=null?(qe[$e]=k,j.write("d",ie.p=$e++)):j.write("i",t.default(X.i)),he=!0))}else if(k!=null&&!ie){const ue=$[k];ue&&(J=ue.clone())}k!=null?(T=C[k],A=w[k],S=I[k]):X.i!==void 0&&(T=A=null,me||(S=null))}else _(l(T))&&(T=A=S=null);const ae=l(T),be=l(A);if(_(be)){const k=be.p;be.r!==void 0&&(!ae||ae.r===void 0)||p[k]?(S=null,J=A.clone()):k!=null&&(S=g[k],D!==1&&K[k]!=null||((j=te[k])||(j=te[k]=r.writeCursor()),j.reset(),J=null))}else!s(X)&&s(ee)&&(S=null);ee=S!=null?S.getComponent():null;const V=Oe(X);if(V){const k=f(X);if(J)b==null&&(b={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.editOp(B.getPath(),V,k,!0),op2:a.removeOp(J.getPath())});else{const ie=Oe(ee);let me;if(ie){if(V!==ie)throw Error("Transforming incompatible types");const ue=f(ee);me=V.transform(k,ue,O)}else me=t.default(k);y(j,V,me)}}let P=0,G=0,ge=0,ve=0,We=0,Ke=0,Me=T!=null&&T.descendFirst(),Ye=Me;const Xe=r.advancer(A,void 0,(k,ie)=>{_(ie)&&ge++});let ke=S!=null&&S.descendFirst(),Ne=ke;for(const k of B)if(typeof k=="number"){let ie;const me=s(B.getComponent()),ue=k-G;{let Be;for(;Me&&typeof(Be=T.getKey())=="number";){Be+=P;const Ue=T.getComponent(),Je=_(Ue);if(Be>ue||Be===ue&&(!Je||D===0&&me))break;if(Je){P--;const He=Ue.p;K.includes(He),Ue.d,l(te[Ue.d]),_(l(te[Ue.d])),(Ue.r===void 0||Ae&&Ae.has(Ue))&&(He==null||!Pe[He]||D!==1&&K.includes(He))||We--}Me=T.nextSibling()}ie=Me&&Be===ue?T:null}const je=ue-P;let xe=Xe(je);const ot=je-ge;let Qe=null;{let Be,Ue;for(;ke&&typeof(Be=S.getKey())=="number";){Ue=Be-ve;const Je=S.getComponent(),He=s(Je);if(Ue>ot)break;if(Ue===ot){if(!He){Qe=S;break}{if(D===0&&me){Qe=S;break}const Ge=xe&&_(xe.getComponent());if(D===0&&Ge)break}}if(He){const Ge=Je.d;p[Ge],K[Ge],Je.i===void 0&&(p[Ge]||K[Ge]!=null&&D!==1)?(p[Ge]||K[Ge]!=null&&D===0)&&(ve++,Ke--):ve++}ke=S.nextSibling()}}const _t=ot+ve+We+Ke;o(_t>=0,"trying to descend to a negative index"),j.descend(_t),me&&(ie=xe=Qe=null,G++),Q(ie,B,xe,Qe,j,J)&&Ke++,j.ascend()}else{let ie;for(;Me&&(ie=T.getKey(),typeof ie!="string"||!(ie>k||ie===k));)Me=T.nextSibling();const me=Me&&ie===k?T:null,ue=Xe(k);let je;for(;ke&&(je=S.getKey(),typeof je!="string"||!(je>k||je===k));)ke=S.nextSibling();const xe=ke&&je===k?S:null;j.descend(k),Q(me,B,ue,xe,j,J),j.ascend()}return Xe.end(),Ye&&T.ascend(),Ne&&S.ascend(),he}(we,we.clone(),_e,_e.clone(),oe,null),b)return{ok:!1,conflict:b};oe.reset();const Te=(Q,T,B)=>Q.traverse(T,(A,S)=>{A.d!=null&&B(A.d,Q,S)});(p.length||re.length)&&(Te(_e,oe,(Q,T,B)=>{p[Q]&&!M[Q]&&B.write("r",!0),re[Q]&&B.mergeTree(re[Q].get())}),oe.reset());const Ee=[],Re=[];if((te.length||p.length)&&!b){const Q=r.readCursor(it(oe.get()));if(Te(Q,null,(T,B)=>{Ee[T]=B.clone()}),te.forEach(T=>{T&&Te(r.readCursor(T.get()),null,(B,A)=>{Ee[B]=A.clone()})}),function T(B,A,S,j,J,X){const ee=l(A);if(ee&&_(ee))if(ee.p!=null){const P=ee.p;Ee[P].getPath(),S=Ee[P],j=Re[P]=r.writeCursor()}else ee.r!==void 0&&(S=null);else s(l(S))&&(S=null);const he=B.getComponent();if(he){let P;if((P=he.d)!=null){const G=te[P];G&&(G.get(),j.mergeTree(G.get()),S=r.readCursor(G.get()))}}let fe=0,ae=0;const be=r.advancer(A,void 0,(P,G)=>{_(G)&&fe--}),V=r.advancer(S,(P,G)=>s(G)?-(P-ae)-1:P-ae,(P,G)=>{s(G)&&ae++});for(const P of B)if(typeof P=="number"){const G=be(P),ge=P+fe,ve=V(ge),We=ge+ae;j.descend(We),T(B,G,ve,j),j.ascend()}else j.descend(P),T(B,be(P),V(P),j),j.ascend();be.end(),V.end()}(_e,Q,Q.clone(),oe),oe.reset(),b)return{ok:!1,conflict:b};if(oe.get(),Re.length){const T=Re.map(A=>A?A.get():null),B=r.readCursor(it(oe.get()));if(Te(B,oe,(A,S,j)=>{const J=T[A];J&&(j.mergeTree(J),T[A]=null)}),T.find(A=>A)){const A=r.writeCursor(),S=r.writeCursor();let j=0,J=0;T.forEach(X=>{X!=null&&Te(r.readCursor(X),null,ee=>{const he=qe[ee];A.writeMove(C[he].getPath(),R[he].getPath(),j++);const fe=se[he];fe&&fe.forEach(ae=>{p[ae]||D!==1&&K[ae]!=null||S.writeMove(N[ae].getPath(),g[ae].getPath(),J++)})})}),b={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:S.get()}}}}return b?{ok:!1,conflict:b}:{ok:!0,result:oe.get()}}const pt=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Wt(i,d,O){const D=st(i,d,O);if(D.ok)return D.result;pt(D.conflict)}const ze=i=>{const d=r.writeCursor();return r.readCursor(i).traverse(d,(O,D)=>{(s(O)||Oe(O))&&D.write("r",!0)}),d.get()},xt=(i,d)=>{const{type:O,op1:D,op2:b}=i;switch(O){case u.ConflictType.DROP_COLLISION:return d==="left"?[null,ze(b)]:[ze(D),null];case u.ConflictType.RM_UNEXPECTED_CONTENT:let C=!1;return r.readCursor(D).traverse(null,R=>{R.r!==void 0&&(C=!0)}),C?[null,ze(b)]:[ze(D),null];case u.ConflictType.BLACKHOLE:return[ze(D),ze(b)];default:throw Error("Unrecognised conflict: "+O)}};function yt(i,d,O,D){let b=null;for(;;){const C=st(d,O,D);if(C.ok)return ne(b,C.result);{const{conflict:R}=C;i(R)||pt(R);const[N,g]=xt(R,D);d=ne(q(d),N),O=ne(q(O),g),b=ne(b,g)}}}}(et)),et}var St;function Jt(){return St||(St=1,function(a){var e=Ze.__createBinding||(Object.create?function(u,o,l,E){E===void 0&&(E=l),Object.defineProperty(u,E,{enumerable:!0,get:function(){return o[l]}})}:function(u,o,l,E){E===void 0&&(E=l),u[E]=o[l]}),n=Ze.__exportStar||function(u,o){for(var l in u)l!=="default"&&!o.hasOwnProperty(l)&&e(o,u,l)};Object.defineProperty(a,"__esModule",{value:!0}),n(Xt(),a);var t=Mt();Object.defineProperty(a,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(a,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var r=jt();Object.defineProperty(a,"ConflictType",{enumerable:!0,get:function(){return r.ConflictType}})}(Ze)),Ze}var F=Jt();class Ut{constructor(){H(this,"drawingManagerData",{});H(this,"_oldDrawingManagerData",{});H(this,"_focusDrawings",[]);H(this,"_remove$",new Ie.Subject);H(this,"remove$",this._remove$.asObservable());H(this,"_add$",new Ie.Subject);H(this,"add$",this._add$.asObservable());H(this,"_update$",new Ie.Subject);H(this,"update$",this._update$.asObservable());H(this,"_order$",new Ie.Subject);H(this,"order$",this._order$.asObservable());H(this,"_group$",new Ie.Subject);H(this,"group$",this._group$.asObservable());H(this,"_ungroup$",new Ie.Subject);H(this,"ungroup$",this._ungroup$.asObservable());H(this,"_refreshTransform$",new Ie.Subject);H(this,"refreshTransform$",this._refreshTransform$.asObservable());H(this,"_visible$",new Ie.Subject);H(this,"visible$",this._visible$.asObservable());H(this,"_focus$",new Ie.Subject);H(this,"focus$",this._focus$.asObservable());H(this,"_featurePluginUpdate$",new Ie.Subject);H(this,"featurePluginUpdate$",this._featurePluginUpdate$.asObservable());H(this,"_featurePluginAdd$",new Ie.Subject);H(this,"featurePluginAdd$",this._featurePluginAdd$.asObservable());H(this,"_featurePluginRemove$",new Ie.Subject);H(this,"featurePluginRemove$",this._featurePluginRemove$.asObservable());H(this,"_featurePluginOrderUpdate$",new Ie.Subject);H(this,"featurePluginOrderUpdate$",this._featurePluginOrderUpdate$.asObservable());H(this,"_featurePluginGroupUpdate$",new Ie.Subject);H(this,"featurePluginGroupUpdate$",this._featurePluginGroupUpdate$.asObservable());H(this,"_featurePluginUngroupUpdate$",new Ie.Subject);H(this,"featurePluginUngroupUpdate$",this._featurePluginUngroupUpdate$.asObservable());H(this,"_visible",!0);H(this,"_editable",!0)}dispose(){this._remove$.complete(),this._add$.complete(),this._update$.complete(),this._order$.complete(),this._focus$.complete(),this._featurePluginUpdate$.complete(),this._featurePluginAdd$.complete(),this._featurePluginRemove$.complete(),this._featurePluginOrderUpdate$.complete(),this.drawingManagerData={},this._oldDrawingManagerData={}}visibleNotification(e){this._visible$.next(e)}refreshTransform(e){e.forEach(n=>{const t=this._getCurrentBySearch(n);t!=null&&(t.transform=n.transform,t.transforms=n.transforms,t.isMultiTransform=n.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){const n=this.drawingManagerData[e];if(n==null)return;delete this.drawingManagerData[e];const t=[];Object.keys(n).forEach(r=>{const u=n[r];(u==null?void 0:u.data)!=null&&Object.keys(u.data).forEach(o=>{t.push({unitId:e,subUnitId:r,drawingId:o})})}),t.length>0&&this.removeNotification(t)}registerDrawingData(e,n){this.drawingManagerData[e]=n}initializeNotification(e){const n=[],t=this.drawingManagerData[e];t!=null&&(Object.keys(t).forEach(r=>{this._establishDrawingMap(e,r);const u=t[r];Object.keys(u.data).forEach(o=>{const l=u.data[o];l.unitId=e,l.subUnitId=r,n.push(l)})}),n.length>0&&this.addNotification(n))}getDrawingData(e,n){return this._getDrawingData(e,n)}setDrawingData(e,n,t){this.drawingManagerData[e][n].data=t}getBatchAddOp(e){const n=[],t=[],r=[];e.forEach(W=>{const{op:_,invertOp:s}=this._addByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),r.push(s)});const u=t.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:E,objects:n}}getBatchRemoveOp(e){const n=[],t=[];e.forEach(E=>{const{op:W,invertOp:_}=this._removeByParam(E);n.unshift(W),t.push(_)});const r=n.reduce(F.type.compose,null),u=t.reduce(F.type.compose,null),{unitId:o,subUnitId:l}=e[0];return{undo:u,redo:r,unitId:o,subUnitId:l,objects:e}}getBatchUpdateOp(e){const n=[],t=[],r=[];e.forEach(W=>{const{op:_,invertOp:s}=this._updateByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),r.push(s)});const u=t.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:E,objects:n}}removeNotification(e){this._remove$.next(e)}addNotification(e){this._add$.next(e)}updateNotification(e){this._update$.next(e)}orderNotification(e){this._order$.next(e)}groupUpdateNotification(e){this._group$.next(e)}ungroupUpdateNotification(e){this._ungroup$.next(e)}refreshTransformNotification(e){this._refreshTransform$.next(e)}getGroupDrawingOp(e){const n=[],{unitId:t,subUnitId:r}=e[0].parent;e.forEach(l=>{n.push(this._getGroupDrawingOp(l))});const u=n.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:r,objects:e}}getUngroupDrawingOp(e){const n=[],{unitId:t,subUnitId:r}=e[0].parent;e.forEach(l=>{n.push(this._getUngroupDrawingOp(l))});const u=n.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:r,objects:e}}getDrawingsByGroup(e){const{unitId:n,subUnitId:t,drawingId:r}=e;if(this.getDrawingByParam({unitId:n,subUnitId:t,drawingId:r})==null)return[];const o=this._getDrawingData(n,t),l=[];return Object.keys(o).forEach(E=>{const W=o[E];W.groupId===r&&l.push(W)}),l}_getGroupDrawingOp(e){const{parent:n,children:t}=e,{unitId:r,subUnitId:u,drawingId:o}=n,l=[];l.push(F.insertOp([r,u,"data",o],n));let E=Number.NEGATIVE_INFINITY;return t.forEach(W=>{const{unitId:_,subUnitId:s,drawingId:c}=W,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),l.push(...this._getUpdateParamCompareOp(W,this.getDrawingByParam({unitId:_,subUnitId:s,drawingId:c})))}),E===Number.NEGATIVE_INFINITY&&(E=this._getDrawingOrder(r,u).length),l.push(F.insertOp([r,u,"order",E],o)),l.reduce(F.type.compose,null)}_getUngroupDrawingOp(e){const{parent:n,children:t}=e,{unitId:r,subUnitId:u,drawingId:o}=n,l=[];return t.forEach(E=>{const{unitId:W,subUnitId:_,drawingId:s}=E;l.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:W,subUnitId:_,drawingId:s})))}),l.push(F.removeOp([r,u,"data",o],!0)),l.push(F.removeOp([r,u,"order",this._getDrawingOrder(r,u).indexOf(o)],!0)),l.reduce(F.type.compose,null)}applyJson1(e,n,t){this._establishDrawingMap(e,n),this._oldDrawingManagerData={...this.drawingManagerData},this.drawingManagerData=F.type.apply(this.drawingManagerData,t)}featurePluginUpdateNotification(e){this._featurePluginUpdate$.next(e)}featurePluginOrderUpdateNotification(e){this._featurePluginOrderUpdate$.next(e)}featurePluginAddNotification(e){this._featurePluginAdd$.next(e)}featurePluginRemoveNotification(e){this._featurePluginRemove$.next(e)}featurePluginGroupUpdateNotification(e){this._featurePluginGroupUpdate$.next(e)}featurePluginUngroupUpdateNotification(e){this._featurePluginUngroupUpdate$.next(e)}getDrawingByParam(e){return this._getCurrentBySearch(e)}getOldDrawingByParam(e){return this._getOldBySearch(e)}getDrawingOKey(e){const[n,t,r]=e.split("#-#");return this._getCurrentBySearch({unitId:n,subUnitId:t,drawingId:r})}focusDrawing(e){if(e==null||e.length===0){this._focusDrawings=[],this._focus$.next([]);return}const n=[];e.forEach(t=>{var E;const{unitId:r,subUnitId:u,drawingId:o}=t,l=(E=this._getDrawingData(r,u))==null?void 0:E[o];l!=null&&n.push(l)}),n.length>0&&(this._focusDrawings=n,this._focus$.next(n))}getFocusDrawings(){const e=[];return this._focusDrawings.forEach(n=>{var l;const{unitId:t,subUnitId:r,drawingId:u}=n,o=(l=this._getDrawingData(t,r))==null?void 0:l[u];o!=null&&e.push(o)}),e}getDrawingOrder(e,n){return this._getDrawingOrder(e,n)}setDrawingOrder(e,n,t){this.drawingManagerData[e][n].order=t}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=[],o=this.getDrawingOrder(n,t),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:n,subUnitId:t,drawingId:_});if(s===-1||s===o.length-1)return;const c=F.moveOp([n,t,"order",s],[n,t,"order",s+1]);u.push(c),l.includes(o[s+1])||l.push(o[s+1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:n,subUnitId:t,objects:{...e,drawingIds:l}}}getBackwardDrawingOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=[],o=this.getDrawingOrder(n,t),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:n,subUnitId:t,drawingId:_});if(s===-1||s===0)return;const c=F.moveOp([n,t,"order",s],[n,t,"order",s-1]);u.push(c),l.includes(o[s-1])||l.push(o[s-1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:n,subUnitId:t,objects:{...e,drawingIds:l}}}getFrontDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=this._getOrderFromSearchParams(n,t,r),o=[...r],l=this.getDrawingOrder(n,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=this._getDrawingCount(n,t)-1,U=F.moveOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(c)],[n,t,"order",h]);E.push(U),o.includes(l[h])||o.push(l[h])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:n,subUnitId:t,objects:{...e,drawingIds:o}}}getBackDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=this._getOrderFromSearchParams(n,t,r,!0),o=[...r],l=this.getDrawingOrder(n,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=F.moveOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(c)],[n,t,"order",0]);E.push(h),o.includes(l[0])||o.push(l[0])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:n,subUnitId:t,objects:{...e,drawingIds:o}}}_getDrawingCount(e,n){return this.getDrawingOrder(e,n).length||0}_getOrderFromSearchParams(e,n,t,r=!1){return t.map(u=>{const o=this._hasDrawingOrder({unitId:e,subUnitId:n,drawingId:u});return{drawingId:u,zIndex:o}}).sort(r===!1?ce.sortRules:ce.sortRulesByDesc)}_hasDrawingOrder(e){if(e==null)return-1;const{unitId:n,subUnitId:t,drawingId:r}=e;return this._establishDrawingMap(n,t),this._getDrawingOrder(n,t).indexOf(r)}_getCurrentBySearch(e){var u,o,l;if(e==null)return;const{unitId:n,subUnitId:t,drawingId:r}=e;return(l=(o=(u=this.drawingManagerData[n])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:l[r]}_getOldBySearch(e){var u,o,l;if(e==null)return;const{unitId:n,subUnitId:t,drawingId:r}=e;return(l=(o=(u=this._oldDrawingManagerData[n])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:l[r]}_establishDrawingMap(e,n,t){var r;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][n]||(this.drawingManagerData[e][n]={data:{},order:[]}),t==null?null:(r=this.drawingManagerData[e][n].data)==null?void 0:r[t]}_addByParam(e){const{unitId:n,subUnitId:t,drawingId:r}=e;this._establishDrawingMap(n,t,r);const u=F.insertOp([n,t,"data",r],e),o=F.insertOp([n,t,"order",this._getDrawingOrder(n,t).length],r),l=[u,o].reduce(F.type.compose,null),E=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:E}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};const{unitId:n,subUnitId:t,drawingId:r}=e;if(this._establishDrawingMap(n,t,r)==null)return{op:[],invertOp:[]};const o=F.removeOp([n,t,"data",r],!0),l=F.removeOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(r)],!0),E=[o,l].reduce(F.type.compose,null),W=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:W}}_updateByParam(e){const{unitId:n,subUnitId:t,drawingId:r}=e,u=this._establishDrawingMap(n,t,r);if(u==null)return{op:[],invertOp:[]};const l=this._getUpdateParamCompareOp(e,u).reduce(F.type.compose,null),E=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:E}}_getUpdateParamCompareOp(e,n){const{unitId:t,subUnitId:r,drawingId:u}=e,o=[];return Object.keys(e).forEach(l=>{const E=e[l],W=n[l];W!==E&&o.push(F.replaceOp([t,r,"data",u,l],W,E))}),o}_getDrawingData(e,n){var t,r;return((r=(t=this.drawingManagerData[e])==null?void 0:t[n])==null?void 0:r.data)||{}}_getDrawingOrder(e,n){var t,r;return((r=(t=this.drawingManagerData[e])==null?void 0:t[n])==null?void 0:r.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}}class kt extends Ut{}function Yt({unitId:a,subUnitId:e,drawingId:n},t){return typeof t=="number"?`${a}#-#${e}#-#${n}#-#${t}`:`${a}#-#${e}#-#${n}`}const Zt=async a=>new Promise((e,n)=>{const t=new Image;t.src=a,t.onload=()=>{e({width:t.width,height:t.height,image:t})},t.onerror=r=>{n(r)}}),gt=ce.createIdentifier("univer.drawing-manager.service"),Nt={id:"drawing.operation.set-drawing-selected",type:ce.CommandType.OPERATION,handler:(a,e)=>{const n=a.get(gt);return e==null?!1:(n.focusDrawing(e),!0)}},Qt="drawing.config",Pt={};class $t{constructor(){H(this,"_waitCount",0);H(this,"_change$",new Ie.Subject);H(this,"change$",this._change$);H(this,"_imageSourceCache",new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,n){if(n===ce.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,n,t){n===ce.ImageSourceType.BASE64||t==null||this._imageSourceCache.set(e,t)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((n,t)=>{if(!At.includes(e.type)){t(new Error(ce.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>Tt){t(new Error(ce.ImageUploadStatusType.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}const r=new FileReader;r.readAsDataURL(e),r.onload=u=>{var E;const o=(E=u.target)==null?void 0:E.result;if(o==null){t(new Error(ce.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const l=ce.Tools.generateRandomId(6);n({imageId:l,imageSourceType:ce.ImageSourceType.BASE64,source:o,base64Cache:o,status:ce.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var en=Object.defineProperty,tn=Object.getOwnPropertyDescriptor,nn=(a,e,n,t)=>{for(var r=t>1?void 0:t?tn(e,n):e,u=a.length-1,o;u>=0;u--)(o=a[u])&&(r=(t?o(e,n,r):o(r))||r);return t&&r&&en(e,n,r),r},ct=(a,e)=>(n,t)=>e(n,t,a);const rn="UNIVER_DRAWING_PLUGIN";var dt;exports.UniverDrawingPlugin=(dt=class extends ce.Plugin{constructor(e=Pt,n,t,r){super(),this._config=e,this._injector=n,this._configService=t,this._commandService=r;const{...u}=ce.merge({},Pt,this._config);this._configService.setConfig(Qt,u)}onStarting(){this._initCommands(),this._initDependencies()}_initDependencies(){var t;const e=[[ce.IImageIoService,{useClass:$t}],[gt,{useClass:kt}]];ce.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(r=>this._injector.add(r))}_initCommands(){[Nt].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}},H(dt,"pluginName",rn),dt);exports.UniverDrawingPlugin=nn([ct(1,ce.Inject(ce.Injector)),ct(2,ce.IConfigService),ct(3,ce.ICommandService)],exports.UniverDrawingPlugin);Object.defineProperty(exports,"IImageIoService",{enumerable:!0,get:()=>ce.IImageIoService});Object.defineProperty(exports,"ImageSourceType",{enumerable:!0,get:()=>ce.ImageSourceType});Object.defineProperty(exports,"ImageUploadStatusType",{enumerable:!0,get:()=>ce.ImageUploadStatusType});exports.DRAWING_IMAGE_ALLOW_IMAGE_LIST=At;exports.DRAWING_IMAGE_ALLOW_SIZE=Tt;exports.DRAWING_IMAGE_COUNT_LIMIT=Kt;exports.DRAWING_IMAGE_HEIGHT_LIMIT=qt;exports.DRAWING_IMAGE_WIDTH_LIMIT=Lt;exports.DrawingManagerService=kt;exports.IDrawingManagerService=gt;exports.ImageIoService=$t;exports.SetDrawingSelectedOperation=Nt;exports.UnitDrawingService=Ut;exports.getDrawingShapeKeyByDrawingSearch=Yt;exports.getImageSize=Zt;