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