@annotorious/annotorious 3.0.0-rc.4 → 3.0.0-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- (function(O,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(O=typeof globalThis<"u"?globalThis:O||self,F(O.Annotorious={}))})(this,function(O){"use strict";function F(){}function kn(e,t){for(const n in t)e[n]=t[n];return e}function gt(e){return e()}function pt(){return Object.create(null)}function le(e){e.forEach(gt)}function q(e){return typeof e=="function"}function x(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}function Pn(e){return Object.keys(e).length===0}function _t(e,...t){if(e==null)return F;const n=e.subscribe(...t);return n.unsubscribe?()=>n.unsubscribe():n}function yt(e,t,n){e.$$.on_destroy.push(_t(t,n))}function Rn(e,t,n,o){if(e){const i=wt(e,t,n,o);return e[0](i)}}function wt(e,t,n,o){return e[1]&&o?kn(n.ctx.slice(),e[1](o(t))):n.ctx}function Yn(e,t,n,o){if(e[2]&&o){const i=e[2](o(n));if(t.dirty===void 0)return i;if(typeof i=="object"){const s=[],r=Math.max(t.dirty.length,i.length);for(let a=0;a<r;a+=1)s[a]=t.dirty[a]|i[a];return s}return t.dirty|i}return t.dirty}function Dn(e,t,n,o,i,s){if(i){const r=wt(t,n,o,s);e.p(r,i)}}function Un(e){if(e.ctx.length>32){const t=[],n=e.ctx.length/32;for(let o=0;o<n;o++)t[o]=-1;return t}return-1}function ue(e,t){e.appendChild(t)}function P(e,t,n){e.insertBefore(t,n||null)}function B(e){e.parentNode&&e.parentNode.removeChild(e)}function Ke(e,t){for(let n=0;n<e.length;n+=1)e[n]&&e[n].d(t)}function U(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function bt(e){return document.createTextNode(e)}function oe(){return bt(" ")}function ae(){return bt("")}function K(e,t,n,o){return e.addEventListener(t,n,o),()=>e.removeEventListener(t,n,o)}function u(e,t,n){n==null?e.removeAttribute(t):e.getAttribute(t)!==n&&e.setAttribute(t,n)}function Xn(e){return Array.from(e.childNodes)}function Et(e,t,n){e.classList[n?"add":"remove"](t)}function Nn(e,t,{bubbles:n=!1,cancelable:o=!1}={}){const i=document.createEvent("CustomEvent");return i.initCustomEvent(e,n,o,t),i}let Le;function Oe(e){Le=e}function At(){if(!Le)throw new Error("Function called outside component initialization");return Le}function ve(e){At().$$.on_mount.push(e)}function ye(){const e=At();return(t,n,{cancelable:o=!1}={})=>{const i=e.$$.callbacks[t];if(i){const s=Nn(t,n,{cancelable:o});return i.slice().forEach(r=>{r.call(e,s)}),!s.defaultPrevented}return!0}}function we(e,t){const n=e.$$.callbacks[t.type];n&&n.slice().forEach(o=>o.call(this,t))}const be=[],Re=[];let Ee=[];const St=[],Gn=Promise.resolve();let Je=!1;function Fn(){Je||(Je=!0,Gn.then(Tt))}function Ze(e){Ee.push(e)}const Qe=new Set;let Ae=0;function Tt(){if(Ae!==0)return;const e=Le;do{try{for(;Ae<be.length;){const t=be[Ae];Ae++,Oe(t),Hn(t.$$)}}catch(t){throw be.length=0,Ae=0,t}for(Oe(null),be.length=0,Ae=0;Re.length;)Re.pop()();for(let t=0;t<Ee.length;t+=1){const n=Ee[t];Qe.has(n)||(Qe.add(n),n())}Ee.length=0}while(be.length);for(;St.length;)St.pop()();Je=!1,Qe.clear(),Oe(e)}function Hn(e){if(e.fragment!==null){e.update(),le(e.before_update);const t=e.dirty;e.dirty=[-1],e.fragment&&e.fragment.p(e.ctx,t),e.after_update.forEach(Ze)}}function zn(e){const t=[],n=[];Ee.forEach(o=>e.indexOf(o)===-1?t.push(o):n.push(o)),n.forEach(o=>o()),Ee=t}const Ye=new Set;let me;function ce(){me={r:0,c:[],p:me}}function fe(){me.r||le(me.c),me=me.p}function X(e,t){e&&e.i&&(Ye.delete(e),e.i(t))}function N(e,t,n,o){if(e&&e.o){if(Ye.has(e))return;Ye.add(e),me.c.push(()=>{Ye.delete(e),o&&(n&&e.d(1),o())}),e.o(t)}else o&&o()}function ge(e){e&&e.c()}function de(e,t,n,o){const{fragment:i,after_update:s}=e.$$;i&&i.m(t,n),o||Ze(()=>{const r=e.$$.on_mount.map(gt).filter(q);e.$$.on_destroy?e.$$.on_destroy.push(...r):le(r),e.$$.on_mount=[]}),s.forEach(Ze)}function he(e,t){const n=e.$$;n.fragment!==null&&(zn(n.after_update),le(n.on_destroy),n.fragment&&n.fragment.d(t),n.on_destroy=n.fragment=null,n.ctx=[])}function Vn(e,t){e.$$.dirty[0]===-1&&(be.push(e),Fn(),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<<t%31}function ie(e,t,n,o,i,s,r,a=[-1]){const l=Le;Oe(e);const f=e.$$={fragment:null,ctx:[],props:s,update:F,not_equal:i,bound:pt(),on_mount:[],on_destroy:[],on_disconnect:[],before_update:[],after_update:[],context:new Map(t.context||(l?l.$$.context:[])),callbacks:pt(),dirty:a,skip_bound:!1,root:t.target||l.$$.root};r&&r(f.root);let h=!1;if(f.ctx=n?n(e,t.props||{},(c,d,...m)=>{const g=m.length?m[0]:d;return f.ctx&&i(f.ctx[c],f.ctx[c]=g)&&(!f.skip_bound&&f.bound[c]&&f.bound[c](g),h&&Vn(e,c)),d}):[],f.update(),h=!0,le(f.before_update),f.fragment=o?o(f.ctx):!1,t.target){if(t.hydrate){const c=Xn(t.target);f.fragment&&f.fragment.l(c),c.forEach(B)}else f.fragment&&f.fragment.c();t.intro&&X(e.$$.fragment),de(e,t.target,t.anchor,t.customElement),Tt()}Oe(l)}class se{$destroy(){he(this,1),this.$destroy=F}$on(t,n){if(!q(n))return F;const o=this.$$.callbacks[t]||(this.$$.callbacks[t]=[]);return o.push(n),()=>{const i=o.indexOf(n);i!==-1&&o.splice(i,1)}}$set(t){this.$$set&&!Pn(t)&&(this.$$.skip_bound=!0,this.$$set(t),this.$$.skip_bound=!1)}}var V=(e=>(e.ELLIPSE="ELLIPSE",e.POLYGON="POLYGON",e.RECTANGLE="RECTANGLE",e))(V||{});const xe={},De=(e,t)=>xe[e]=t,Ue=e=>xe[e.type].area(e),Mt=(e,t,n)=>xe[e.type].intersects(e,t,n),Ce=e=>{let t=1/0,n=1/0,o=-1/0,i=-1/0;return e.forEach(([s,r])=>{t=Math.min(t,s),n=Math.min(n,r),o=Math.max(o,s),i=Math.max(i,r)}),{minX:t,minY:n,maxX:o,maxY:i}},jn={area:e=>Math.PI*e.geometry.rx*e.geometry.ry,intersects:(e,t,n)=>{const{cx:o,cy:i,rx:s,ry:r}=e.geometry,a=0,l=Math.cos(a),f=Math.sin(a),h=t-o,c=n-i,d=l*h+f*c,m=f*h-l*c;return d*d/(s*s)+m*m/(r*r)<=1}};De(V.ELLIPSE,jn);const Wn={area:e=>{const{points:t}=e.geometry;let n=0,o=t.length-1;for(let i=0;i<t.length;i++)n+=(t[o][0]+t[i][0])*(t[o][1]-t[i][1]),o=i;return Math.abs(.5*n)},intersects:(e,t,n)=>{const{points:o}=e.geometry;let i=!1;for(let s=0,r=o.length-1;s<o.length;r=s++){const a=o[s][0],l=o[s][1],f=o[r][0],h=o[r][1];l>n!=h>n&&t<(f-a)*(n-l)/(h-l)+a&&(i=!i)}return i}};De(V.POLYGON,Wn);const Lt={area:e=>e.geometry.w*e.geometry.h,intersects:(e,t,n)=>t>=e.geometry.x&&t<=e.geometry.x+e.geometry.w&&n>=e.geometry.y&&n<=e.geometry.y+e.geometry.h};De(V.RECTANGLE,Lt);const Ot=(e,t=!1)=>{const n=typeof e=="string"?e:e.value,o=/^(xywh)=(pixel|percent)?:?(.+?),(.+?),(.+?),(.+)*/g,i=[...n.matchAll(o)][0],[s,r,a,l,f,h,c]=i;if(r!=="xywh")throw new Error("Unsupported MediaFragment: "+n);if(a&&a!=="pixel")throw new Error(`Unsupported MediaFragment unit: ${a}`);const[d,m,g,y]=[l,f,h,c].map(parseFloat);return{type:V.RECTANGLE,geometry:{x:d,y:m,w:g,h:y,bounds:{minX:d,minY:t?m-y:m,maxX:d+g,maxY:t?m:m+y}}}},vt=e=>{const{x:t,y:n,w:o,h:i}=e;return{type:"FragmentSelector",conformsTo:"http://www.w3.org/TR/media-frags/",value:`xywh=pixel:${t},${n},${o},${i}`}},Ct="http://www.w3.org/2000/svg",It=e=>{const t=o=>{Array.from(o.attributes).forEach(i=>{i.name.startsWith("on")&&o.removeAttribute(i.name)})},n=e.getElementsByTagName("script");return Array.from(n).reverse().forEach(o=>o.parentNode.removeChild(o)),Array.from(e.querySelectorAll("*")).forEach(t),e},qn=e=>{const o=new XMLSerializer().serializeToString(e.documentElement).replace("<svg>",`<svg xmlns="${Ct}">`);return new DOMParser().parseFromString(o,"image/svg+xml").documentElement},Kn=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml"),o=n.lookupPrefix(Ct),i=n.lookupNamespaceURI(null);return o||i?It(n).firstChild:It(qn(n)).firstChild},Jn=e=>{const[t,n,o]=e.match(/(<polygon points=["|'])([^("|')]*)/)||[];if(!o)return;const i=o.split(" ").map(s=>s.split(",").map(parseFloat));return{type:V.POLYGON,geometry:{points:i,bounds:Ce(i)}}},Zn=e=>{const t=Kn(e),n=parseFloat(t.getAttribute("cx")),o=parseFloat(t.getAttribute("cy")),i=parseFloat(t.getAttribute("rx")),s=parseFloat(t.getAttribute("ry")),r={minX:n-i,minY:o-s,maxX:n+i,maxY:o+s};return{type:V.ELLIPSE,geometry:{cx:n,cy:o,rx:i,ry:s,bounds:r}}},Bt=e=>{const t=typeof e=="string"?e:e.value;if(t.includes("<polygon points="))return Jn(t);if(t.includes("<ellipse "))return Zn(t)},kt=e=>{let t;if(e.type===V.POLYGON){const n=e.geometry,{points:o}=n;t=`<svg><polygon points="${o.map(i=>i.join(",")).join(" ")}" /></svg>`}else if(e.type===V.ELLIPSE){const n=e.geometry;t=`<svg><ellipse cx="${n.cx}" cy="${n.cy}" rx="${n.rx}" ry="${n.ry}" /></svg>`}if(t)return{type:"SvgSelector",value:t};throw`Unsupported shape type: ${e.type}`};let Xe;const Qn=new Uint8Array(16);function xn(){if(!Xe&&(Xe=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!Xe))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return Xe(Qn)}const J=[];for(let e=0;e<256;++e)J.push((e+256).toString(16).slice(1));function $n(e,t=0){return J[e[t+0]]+J[e[t+1]]+J[e[t+2]]+J[e[t+3]]+"-"+J[e[t+4]]+J[e[t+5]]+"-"+J[e[t+6]]+J[e[t+7]]+"-"+J[e[t+8]]+J[e[t+9]]+"-"+J[e[t+10]]+J[e[t+11]]+J[e[t+12]]+J[e[t+13]]+J[e[t+14]]+J[e[t+15]]}const Pt={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function $e(e,t,n){if(Pt.randomUUID&&!t&&!e)return Pt.randomUUID();e=e||{};const o=e.random||(e.rng||xn)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(let i=0;i<16;++i)t[n+i]=o[i];return t}return $n(o)}var Rt=Object.prototype.hasOwnProperty;function pe(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&pe(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(Rt.call(e,n)&&++o&&!Rt.call(t,n)||!(n in t)||!pe(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}const Se=[];function Ne(e,t=F){let n;const o=new Set;function i(a){if(x(e,a)&&(e=a,n)){const l=!Se.length;for(const f of o)f[1](),Se.push(f,e);if(l){for(let f=0;f<Se.length;f+=2)Se[f][0](Se[f+1]);Se.length=0}}}function s(a){i(a(e))}function r(a,l=F){const f=[a,l];return o.add(f),o.size===1&&(n=t(i)||F),a(e),()=>{o.delete(f),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:r}}const eo=e=>{const{subscribe:t,set:n}=Ne(null);let o=null;return t(i=>o=i),e.observe(({changes:i})=>{if(o){i.deleted.some(a=>a.id===o)&&n(null);const r=i.updated.find(({oldValue:a})=>a.id===o);r&&n(r.newValue.id)}}),{get current(){return o},subscribe:t,set:n}};var et=(e=>(e.EDIT="EDIT",e.SELECT="SELECT",e.NONE="NONE",e))(et||{});const tt={selected:[]},to=(e,t="EDIT")=>{const{subscribe:n,set:o}=Ne(tt);let i=tt;n(c=>i=c);const s=()=>o(tt),r=()=>{var c;return((c=i.selected)==null?void 0:c.length)===0},a=c=>{if(i.selected.length===0)return!1;const d=typeof c=="string"?c:c.id;return i.selected.some(m=>m.id===d)},l=(c,d)=>{const m=e.getAnnotation(c);if(m){const g=no(m,t);o(g==="EDIT"?{selected:[{id:c,editable:!0}],pointerEvent:d}:g==="SELECT"?{selected:[{id:c}],pointerEvent:d}:{selected:[],pointerEvent:d})}else console.warn("Invalid selection: "+c)},f=(c,d=!0)=>{const m=Array.isArray(c)?c:[c],g=m.map(y=>e.getAnnotation(y)).filter(y=>y);o({selected:g.map(({id:y})=>({id:y,editable:d}))}),g.length!==m.length&&console.warn("Invalid selection",c)},h=c=>{if(i.selected.length===0)return!1;const{selected:d}=i;d.filter(({id:g})=>c.includes(g)).length>0&&o({selected:d.filter(({id:g})=>!c.includes(g))})};return e.observe(({changes:c})=>h(c.deleted.map(d=>d.id))),{clear:s,clickSelect:l,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:r,isSelected:a,setSelected:f,subscribe:n}},no=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT",oo=e=>{const{creator:t,updatedBy:n}=e.target,o=e.bodies.reduce((i,s)=>[...i,s.creator,s.updatedBy],[]);return[t,n,...o].filter(i=>i)},io=(e,t,n,o)=>({id:$e(),annotation:e.id,created:n||new Date,creator:o,...t}),so=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},ro=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},lo=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!pe(o,n)?o:void 0}}).filter(({oldBody:n})=>n),ao=(e,t)=>!pe(e.target,t.target),nt=(e,t)=>{const n=so(e,t),o=ro(e,t),i=lo(e,t);return{oldValue:e,newValue:t,bodiesCreated:n.length>0?n:void 0,bodiesDeleted:o.length>0?o:void 0,bodiesUpdated:i.length>0?i:void 0,targetUpdated:ao(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var H=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(H||{});const co=(e,t)=>{var s,r;const{changes:n,origin:o}=t;if(!(!e.options.origin||e.options.origin===o))return!1;if(e.options.ignore){const{ignore:a}=e.options,l=h=>(h==null?void 0:h.length)>0;if(!(l(n.created)||l(n.deleted))){const h=(s=n.updated)==null?void 0:s.some(d=>l(d.bodiesCreated)||l(d.bodiesDeleted)||l(d.bodiesUpdated)),c=(r=n.updated)==null?void 0:r.some(d=>d.targetUpdated);if(a==="BODY_ONLY"&&h&&!c||a==="TARGET_ONLY"&&c&&!h)return!1}}if(e.options.annotations){const a=new Set([...n.created.map(f=>f.id),...n.deleted.map(f=>f.id),...n.updated.map(({oldValue:f})=>f.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(f=>a.has(f))}else return!0},fo=(e,t)=>{const n=new Set((e.created||[]).map(c=>c.id)),o=new Set((e.updated||[]).map(({newValue:c})=>c.id)),i=new Set((t.created||[]).map(c=>c.id)),s=new Set((t.deleted||[]).map(c=>c.id)),r=new Set((t.updated||[]).map(({oldValue:c})=>c.id)),a=new Set((t.updated||[]).filter(({oldValue:c})=>n.has(c.id)||o.has(c.id)).map(({oldValue:c})=>c.id)),l=[...(e.created||[]).filter(c=>!s.has(c.id)).map(c=>r.has(c.id)?t.updated.find(({oldValue:d})=>d.id===c.id).newValue:c),...t.created||[]],f=[...(e.deleted||[]).filter(c=>!i.has(c.id)),...(t.deleted||[]).filter(c=>!n.has(c.id))],h=[...(e.updated||[]).filter(({newValue:c})=>!s.has(c.id)).map(c=>{const{oldValue:d,newValue:m}=c;if(r.has(m.id)){const g=t.updated.find(y=>y.oldValue.id===m.id).newValue;return nt(d,g)}else return c}),...(t.updated||[]).filter(({oldValue:c})=>!a.has(c.id))];return{created:l,deleted:f,updated:h}},uo=e=>e.id!==void 0,ho=()=>{const e=new Map,t=new Map,n=[],o=(w,A={})=>n.push({onChange:w,options:A}),i=w=>{const A=n.findIndex(S=>S.onChange==w);A>-1&&n.splice(A,1)},s=(w,A)=>{const S={origin:w,changes:{created:A.created||[],updated:A.updated||[],deleted:A.deleted||[]},state:[...e.values()]};n.forEach(M=>{co(M,S)&&M.onChange(S)})},r=(w,A=H.LOCAL)=>{if(e.get(w.id))throw Error(`Cannot add annotation ${w.id} - exists already`);e.set(w.id,w),w.bodies.forEach(M=>t.set(M.id,w.id)),s(A,{created:[w]})},a=(w,A)=>{const S=typeof w=="string"?A:w,M=typeof w=="string"?w:w.id,R=e.get(M);if(R){const G=nt(R,S);return M===S.id?e.set(M,S):(e.delete(M),e.set(S.id,S)),R.bodies.forEach($=>t.delete($.id)),S.bodies.forEach($=>t.set($.id,S.id)),G}else console.warn(`Cannot update annotation ${M} - does not exist`)},l=(w,A=H.LOCAL,S=H.LOCAL)=>{const M=uo(A)?S:A,R=a(w,A);R&&s(M,{updated:[R]})},f=(w,A=H.LOCAL)=>{const S=w.reduce((M,R)=>{const G=a(R);return G?[...M,G]:M},[]);S.length>0&&s(A,{updated:S})},h=(w,A=H.LOCAL)=>{const S=e.get(w.annotation);if(S){const M={...S,bodies:[...S.bodies,w]};e.set(S.id,M),t.set(w.id,M.id),s(A,{updated:[{oldValue:S,newValue:M,bodiesCreated:[w]}]})}else console.warn(`Attempt to add body to missing annotation: ${w.annotation}`)},c=()=>[...e.values()],d=(w=H.LOCAL)=>{const A=[...e.values()];e.clear(),t.clear(),s(w,{deleted:A})},m=(w,A=!0,S=H.LOCAL)=>{if(A){const M=[...e.values()];e.clear(),t.clear(),w.forEach(R=>{e.set(R.id,R),R.bodies.forEach(G=>t.set(G.id,R.id))}),s(S,{created:w,deleted:M})}else{const M=w.reduce((R,G)=>{const $=e.get(G.id);return $?[...R,$]:R},[]);if(M.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${M.map(R=>R.id).join(", ")}`);w.forEach(R=>{e.set(R.id,R),R.bodies.forEach(G=>t.set(G.id,R.id))}),s(S,{created:w})}},g=w=>{const A=typeof w=="string"?w:w.id,S=e.get(A);if(S)return e.delete(A),S.bodies.forEach(M=>t.delete(M.id)),S;console.warn(`Attempt to delete missing annotation: ${A}`)},y=(w,A=H.LOCAL)=>{const S=g(w);S&&s(A,{deleted:[S]})},E=(w,A=H.LOCAL)=>{const S=w.reduce((M,R)=>{const G=g(R);return G?[...M,G]:M},[]);S.length>0&&s(A,{deleted:S})},b=(w,A=H.LOCAL)=>{const S=e.get(w.annotation);if(S){const M=S.bodies.find(R=>R.id===w.id);if(M){t.delete(M.id);const R={...S,bodies:S.bodies.filter($=>$.id!==w.id)};e.set(S.id,R),s(A,{updated:[{oldValue:S,newValue:R,bodiesDeleted:[M]}]})}else console.warn(`Attempt to delete missing body ${w.id} from annotation ${w.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${w.annotation}`)},_=w=>{const A=e.get(w);return A?{...A}:void 0},p=w=>{const A=t.get(w);if(A){const M=_(A).bodies.find(R=>R.id===w);if(M)return M;console.error(`Store integrity error: body ${w} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${w}`)},L=(w,A)=>{if(w.annotation!==A.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const S=e.get(w.annotation);if(S){const M=S.bodies.find(G=>G.id===w.id),R={...S,bodies:S.bodies.map(G=>G.id===M.id?A:G)};return e.set(S.id,R),M.id!==A.id&&(t.delete(M.id),t.set(A.id,R.id)),{oldValue:S,newValue:R,bodiesUpdated:[{oldBody:M,newBody:A}]}}else console.warn(`Attempt to add body to missing annotation ${w.annotation}`)},T=(w,A,S=H.LOCAL)=>{const M=L(w,A);s(S,{updated:[M]})},k=(w,A=H.LOCAL)=>{const S=w.map(M=>L({id:M.id,annotation:M.annotation},M));s(A,{updated:S})},j=w=>{const A=e.get(w.annotation);if(A){const S={...A,target:{...A.target,...w}};return e.set(A.id,S),{oldValue:A,newValue:S,targetUpdated:{oldTarget:A.target,newTarget:w}}}else console.warn(`Attempt to update target on missing annotation: ${w.annotation}`)};return{addAnnotation:r,addBody:h,all:c,bulkAddAnnotation:m,bulkDeleteAnnotation:E,bulkUpdateAnnotation:f,bulkUpdateBodies:k,bulkUpdateTargets:(w,A=H.LOCAL)=>{const S=w.map(j).filter(M=>M);S.length>0&&s(A,{updated:S})},clear:d,deleteAnnotation:y,deleteBody:b,getAnnotation:_,getBody:p,observe:o,unobserve:i,updateAnnotation:l,updateBody:T,updateTarget:(w,A=H.LOCAL)=>{const S=j(w);S&&s(A,{updated:[S]})}}},mo=e=>({...e,subscribe:n=>{const o=i=>n(i.state);return e.observe(o),n(e.all()),()=>e.unobserve(o)}});let Yt=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const go=250,po=e=>{const t=Yt(),n=[];let o=-1,i=!1,s=0;const r=p=>{if(!i){const{changes:L}=p,T=performance.now();if(T-s>go)n.splice(o+1),n.push(L),o=n.length-1;else{const k=n.length-1;n[k]=fo(n[k],L)}s=T}i=!1};e.observe(r,{origin:H.LOCAL});const a=p=>(p==null?void 0:p.length)>0&&e.bulkDeleteAnnotation(p),l=p=>(p==null?void 0:p.length)>0&&e.bulkAddAnnotation(p,!1),f=p=>(p==null?void 0:p.length)>0&&e.bulkUpdateAnnotation(p.map(({oldValue:L})=>L)),h=p=>(p==null?void 0:p.length)>0&&e.bulkUpdateAnnotation(p.map(({newValue:L})=>L)),c=p=>(p==null?void 0:p.length)>0&&e.bulkAddAnnotation(p,!1),d=p=>(p==null?void 0:p.length)>0&&e.bulkDeleteAnnotation(p);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(r),on:(p,L)=>t.on(p,L),redo:()=>{if(n.length-1>o){i=!0;const{created:p,updated:L,deleted:T}=n[o+1];l(p),h(L),d(T),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:p,updated:L,deleted:T}=n[o];a(p),f(L),c(T),t.emit("undo",n[o]),o-=1}}}},_o=()=>{const{subscribe:e,set:t}=Ne([]);return{subscribe:e,set:t}},yo=(e,t,n,o)=>{const{store:i,selection:s,hover:r,viewport:a}=e,l=new Map;let f=[],h,c;const d=(b,_)=>{l.has(b)?l.get(b).push(_):l.set(b,[_])},m=(b,_)=>{const p=l.get(b);p&&p.indexOf(_)>0&&p.splice(p.indexOf(_),1)},g=(b,_,p)=>{l.has(b)&&setTimeout(()=>{l.get(b).forEach(L=>{if(n){const T=Array.isArray(_)?_.map(j=>n.serialize(j)):n.serialize(_),k=p?p instanceof PointerEvent?p:n.serialize(p):void 0;L(T,k)}else L(_,p)})},1)},y=()=>{const{selected:b}=s,_=b.map(({id:p})=>i.getAnnotation(p));_.forEach(p=>{const L=f.find(T=>T.id===p.id);(!L||!pe(L,p))&&g("updateAnnotation",p,L)}),f=f.map(p=>{const L=_.find(({id:T})=>T===p.id);return L||p})};s.subscribe(({selected:b})=>{if(!(f.length===0&&b.length===0)){if(f.length===0&&b.length>0)f=b.map(({id:_})=>i.getAnnotation(_));else if(f.length>0&&b.length===0)f.forEach(_=>{const p=i.getAnnotation(_.id);p&&!pe(p,_)&&g("updateAnnotation",p,_)}),f=[];else{const _=new Set(f.map(T=>T.id)),p=new Set(b.map(({id:T})=>T));f.filter(T=>!p.has(T.id)).forEach(T=>{const k=i.getAnnotation(T.id);k&&!pe(k,T)&&g("updateAnnotation",k,T)}),f=[...f.filter(T=>p.has(T.id)),...b.filter(({id:T})=>!_.has(T)).map(({id:T})=>i.getAnnotation(T))]}g("selectionChanged",f)}}),r.subscribe(b=>{!h&&b?g("mouseEnterAnnotation",i.getAnnotation(b)):h&&!b?g("mouseLeaveAnnotation",i.getAnnotation(h)):h&&b&&(g("mouseLeaveAnnotation",i.getAnnotation(h)),g("mouseEnterAnnotation",i.getAnnotation(b))),h=b}),a==null||a.subscribe(b=>g("viewportIntersect",b.map(i.getAnnotation))),i.observe(b=>{o&&(c&&clearTimeout(c),c=setTimeout(y,1e3));const{created:_,deleted:p}=b.changes;_.forEach(T=>g("createAnnotation",T)),p.forEach(T=>g("deleteAnnotation",T)),b.changes.updated.filter(T=>[...T.bodiesCreated||[],...T.bodiesDeleted||[],...T.bodiesUpdated||[]].length>0).forEach(({oldValue:T,newValue:k})=>{const j=f.find(I=>I.id===T.id)||T;f=f.map(I=>I.id===T.id?k:I),g("updateAnnotation",k,j)})},{origin:H.LOCAL}),i.observe(b=>{if(f){const _=new Set(f.map(L=>L.id)),p=b.changes.updated.filter(({newValue:L})=>_.has(L.id)).map(({newValue:L})=>L);p.length>0&&(f=f.map(L=>{const T=p.find(k=>k.id===L.id);return T||L}))}},{origin:H.REMOTE});const E=b=>_=>{const{created:p,deleted:L,updated:T}=_;p.forEach(k=>g("createAnnotation",k)),L.forEach(k=>g("deleteAnnotation",k)),b?T.forEach(k=>g("updateAnnotation",k.oldValue,k.newValue)):T.forEach(k=>g("updateAnnotation",k.newValue,k.oldValue))};return t.on("undo",E(!0)),t.on("redo",E(!1)),{on:d,off:m,emit:g}},wo=e=>t=>t.map(n=>e.serialize(n)),Dt=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),Ut=(e,t,n)=>{const{store:o,selection:i}=e,s=E=>{if(n){const{parsed:b,error:_}=n.parse(E);b?o.addAnnotation(b,H.REMOTE):console.error(_)}else o.addAnnotation(E,H.REMOTE)},r=()=>i.clear(),a=()=>o.clear(),l=E=>{const b=o.getAnnotation(E);return n&&b?n.serialize(b):b},f=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var _;const b=(((_=i.selected)==null?void 0:_.map(p=>p.id))||[]).map(p=>o.getAnnotation(p));return n?b.map(n.serialize):b},c=E=>fetch(E).then(b=>b.json()).then(b=>(m(b),b)),d=E=>{if(typeof E=="string"){const b=o.getAnnotation(E);return o.deleteAnnotation(E),n?n.serialize(b):b}else{const b=n?n.parse(E).parsed:E;return o.deleteAnnotation(b),E}},m=E=>{if(n){const{parsed:b,failed:_}=Dt(n)(E);_.length>0&&console.warn(`Discarded ${_.length} invalid annotations`,_),o.bulkAddAnnotation(b,!0,H.REMOTE)}else o.bulkAddAnnotation(E,!0,H.REMOTE)},g=E=>{E?i.setSelected(E):i.clear()},y=E=>{if(n){const b=n.parse(E).parsed,_=n.serialize(o.getAnnotation(b.id));return o.updateAnnotation(b),_}else{const b=o.getAnnotation(E.id);return o.updateAnnotation(E),b}};return{addAnnotation:s,cancelSelected:r,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:a,getAnnotationById:l,getAnnotations:f,getSelected:h,loadAnnotations:c,redo:t.redo,removeAnnotation:d,setAnnotations:m,setSelected:g,undo:t.undo,updateAnnotation:y}};let bo=e=>crypto.getRandomValues(new Uint8Array(e)),Eo=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let r="";for(;;){let a=n(i),l=i;for(;l--;)if(r+=e[a[l]&o]||"",r.length===s)return r}}},Ao=(e,t=21)=>Eo(e,t,bo),So=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Xt=()=>({isGuest:!0,id:Ao("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),To=e=>{const t=JSON.stringify(e);let n=0;for(let o=0,i=t.length;o<i;o++){let s=t.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},Nt=(e,t)=>(Array.isArray(e)?e:[e]).map(n=>{const{id:o,type:i,purpose:s,value:r,created:a,creator:l,...f}=n;return{id:o||`temp-${To(n)}`,annotation:t,type:i,purpose:s,value:r,created:a,creator:l?typeof l=="object"?{...l}:l:void 0,...f}}),Gt=e=>e.map(t=>{var o;const n={...t};return delete n.annotation,(o=n.id)!=null&&o.startsWith("temp-")&&delete n.id,n}),Mo=["#ff7c00","#1ac938","#e8000b","#8b2be2","#9f4800","#f14cc1","#ffc400","#00d7ff","#023eff"],Ft=()=>{const e=[...Mo];return{assignRandomColor:()=>{const o=Math.floor(Math.random()*e.length),i=e[o];return e.splice(o,1),i},releaseColor:o=>e.push(o)}},Ht=()=>{const e=Ft();return{addUser:(o,i)=>{const s=e.assignRandomColor();return{label:i.name||i.id,avatar:i.avatar,color:s}},removeUser:o=>e.releaseColor(o.appearance.color)}},Lo=(e,t)=>e.every(n=>e.includes(n))&&t.every(n=>e.includes(n)),Oo=So(),vo=(e=Ht())=>{const t=Yt(),n=new Map,o=new Map,i=(c,d)=>{if(n.has(c)){console.warn("Attempt to add user that is already present",c,d);return}const m=e.addUser(c,d);n.set(c,{...d,presenceKey:c,appearance:m})},s=c=>{const d=n.get(c);if(!d){console.warn("Attempt to remove user that is not present",c);return}e.removeUser(d),n.delete(c)},r=c=>{const d=new Set(c.map(y=>y.presenceKey)),m=c.filter(({presenceKey:y})=>!n.has(y)),g=Array.from(n.values()).filter(y=>!d.has(y.presenceKey));m.forEach(({presenceKey:y,user:E})=>i(y,E)),g.forEach(y=>{const{presenceKey:E}=y;o.has(E)&&t.emit("selectionChange",y,null),s(E)}),(m.length>0||g.length>0)&&t.emit("presence",f())},a=(c,d)=>{const m=n.get(c);if(!m){console.warn("Activity notification from user that is not present");return}const g=o.get(c);(!g||!Lo(g,d))&&(o.set(c,d),t.emit("selectionChange",m,d))},l=(c,d)=>{const m=n.get(c);if(!m){console.warn("Selection change for user that is not present",c);return}d?o.set(c,d):o.delete(c),t.emit("selectionChange",m,d)},f=()=>[...Array.from(n.values())];return{getPresentUsers:f,notifyActivity:a,on:(c,d)=>t.on(c,d),syncUsers:r,updateSelection:l}},Co=(e,t=!1)=>({parse:i=>zt(i,t),serialize:i=>Vt(i,e)}),zt=(e,t=!1)=>{const n=e.id||$e(),{body:o,...i}=e,s=Nt(o,n),r=Array.isArray(e.target)?e.target[0]:e.target,a=Array.isArray(r.selector)?r.selector[0]:r.selector,l=a.type==="FragmentSelector"?Ot(a,t):a.type==="SvgSelector"?Bt(a):void 0;return l?{parsed:{...i,id:n,bodies:s,target:{annotation:n,selector:l}}}:{error:Error(`Unknown selector type: ${a.type}`)}},Vt=(e,t)=>{const n=e.target.selector,o=n.type==V.RECTANGLE?vt(n.geometry):kt(n);return{...e,"@context":"http://www.w3.org/ns/anno.jsonld",id:e.id,type:"Annotation",body:Gt(e.bodies),target:{source:t,selector:o}}};function jt(e,t,n){const o=e.slice();return o[11]=t[n],o[13]=n,o}function Wt(e){let t,n,o,i,s;return{c(){t=U("rect"),u(t,"class","a9s-corner-handle"),u(t,"x",n=e[11][0]-e[3]/2),u(t,"y",o=e[11][1]-e[3]/2),u(t,"height",e[3]),u(t,"width",e[3])},m(r,a){P(r,t,a),i||(s=K(t,"pointerdown",function(){q(e[10](C(e[13])))&&e[10](C(e[13])).apply(this,arguments)}),i=!0)},p(r,a){e=r,a&24&&n!==(n=e[11][0]-e[3]/2)&&u(t,"x",n),a&24&&o!==(o=e[11][1]-e[3]/2)&&u(t,"y",o),a&8&&u(t,"height",e[3]),a&8&&u(t,"width",e[3])},d(r){r&&B(t),i=!1,s()}}}function Io(e){let t,n,o,i,s,r,a,l,f,h,c=e[4].points,d=[];for(let m=0;m<c.length;m+=1)d[m]=Wt(jt(e,c,m));return{c(){t=U("polygon"),i=oe(),s=U("polygon"),a=oe();for(let m=0;m<d.length;m+=1)d[m].c();l=ae(),u(t,"class","a9s-outer"),u(t,"style",n=e[1]?"display:none;":void 0),u(t,"points",o=e[4].points.map(qt).join(" ")),u(s,"class","a9s-inner a9s-shape-handle"),u(s,"style",e[1]),u(s,"points",r=e[4].points.map(Kt).join(" "))},m(m,g){P(m,t,g),P(m,i,g),P(m,s,g),P(m,a,g);for(let y=0;y<d.length;y+=1)d[y]&&d[y].m(m,g);P(m,l,g),f||(h=[K(t,"pointerdown",function(){q(e[10](C.SHAPE))&&e[10](C.SHAPE).apply(this,arguments)}),K(s,"pointerdown",function(){q(e[10](C.SHAPE))&&e[10](C.SHAPE).apply(this,arguments)})],f=!0)},p(m,g){if(e=m,g&2&&n!==(n=e[1]?"display:none;":void 0)&&u(t,"style",n),g&16&&o!==(o=e[4].points.map(qt).join(" "))&&u(t,"points",o),g&2&&u(s,"style",e[1]),g&16&&r!==(r=e[4].points.map(Kt).join(" "))&&u(s,"points",r),g&1048){c=e[4].points;let y;for(y=0;y<c.length;y+=1){const E=jt(e,c,y);d[y]?d[y].p(E,g):(d[y]=Wt(E),d[y].c(),d[y].m(l.parentNode,l))}for(;y<d.length;y+=1)d[y].d(1);d.length=c.length}},d(m){m&&B(t),m&&B(i),m&&B(s),m&&B(a),Ke(d,m),m&&B(l),f=!1,le(h)}}}function Bo(e){let t,n;return t=new it({props:{shape:e[0],transform:e[2],editor:e[5],$$slots:{default:[Io,({grab:o})=>({10:o}),({grab:o})=>o?1024:0]},$$scope:{ctx:e}}}),t.$on("change",e[7]),t.$on("grab",e[8]),t.$on("release",e[9]),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,[i]){const s={};i&1&&(s.shape=o[0]),i&4&&(s.transform=o[2]),i&17434&&(s.$$scope={dirty:i,ctx:o}),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}const qt=e=>e.join(","),Kt=e=>e.join(",");function ko(e,t,n){let o,i,{shape:s}=t,{computedStyle:r=void 0}=t,{transform:a}=t,{viewportScale:l=1}=t;const f=(m,g,y)=>{let E;g===C.SHAPE?E=m.geometry.points.map(([_,p])=>[_+y[0],p+y[1]]):E=m.geometry.points.map(([_,p],L)=>g===C(L)?[_+y[0],p+y[1]]:[_,p]);const b=Ce(E);return{...m,geometry:{points:E,bounds:b}}};function h(m){we.call(this,e,m)}function c(m){we.call(this,e,m)}function d(m){we.call(this,e,m)}return e.$$set=m=>{"shape"in m&&n(0,s=m.shape),"computedStyle"in m&&n(1,r=m.computedStyle),"transform"in m&&n(2,a=m.transform),"viewportScale"in m&&n(6,l=m.viewportScale)},e.$$.update=()=>{e.$$.dirty&1&&n(4,o=s.geometry),e.$$.dirty&64&&n(3,i=10/l)},[s,r,a,i,o,f,l,h,c,d]}class Jt extends se{constructor(t){super(),ie(this,t,ko,Bo,x,{shape:0,computedStyle:1,transform:2,viewportScale:6})}}function Po(e){let t,n,o,i,s,r,a,l,f,h,c,d,m,g,y,E,b,_,p,L,T,k,j,I,z,w,A,S,M,R,G,$,Pe,ee,Y,Q,W,Z,re,_e,dt,te,Ve,je,ht,ne,We,qe,mt,Bn;return{c(){t=U("rect"),a=oe(),l=U("rect"),m=oe(),g=U("rect"),_=oe(),p=U("rect"),j=oe(),I=U("rect"),S=oe(),M=U("rect"),Pe=oe(),ee=U("rect"),W=oe(),Z=U("rect"),dt=oe(),te=U("rect"),ht=oe(),ne=U("rect"),u(t,"class","a9s-outer"),u(t,"style",n=e[1]?"display:none;":void 0),u(t,"x",o=e[4].x),u(t,"y",i=e[4].y),u(t,"width",s=e[4].w),u(t,"height",r=e[4].h),u(l,"class","a9s-inner a9s-shape-handle"),u(l,"style",e[1]),u(l,"x",f=e[4].x),u(l,"y",h=e[4].y),u(l,"width",c=e[4].w),u(l,"height",d=e[4].h),u(g,"class","a9s-edge-handle a9s-edge-handle-top"),u(g,"x",y=e[4].x),u(g,"y",E=e[4].y),u(g,"height",1),u(g,"width",b=e[4].w),u(p,"class","a9s-edge-handle a9s-edge-handle-right"),u(p,"x",L=e[4].x+e[4].w),u(p,"y",T=e[4].y),u(p,"height",k=e[4].h),u(p,"width",1),u(I,"class","a9s-edge-handle a9s-edge-handle-bottom"),u(I,"x",z=e[4].x),u(I,"y",w=e[4].y+e[4].h),u(I,"height",1),u(I,"width",A=e[4].w),u(M,"class","a9s-edge-handle a9s-edge-handle-left"),u(M,"x",R=e[4].x),u(M,"y",G=e[4].y),u(M,"height",$=e[4].h),u(M,"width",1),u(ee,"class","a9s-corner-handle a9s-corner-handle-topleft"),u(ee,"x",Y=e[4].x-e[3]/2),u(ee,"y",Q=e[4].y-e[3]/2),u(ee,"height",e[3]),u(ee,"width",e[3]),u(Z,"class","a9s-corner-handle a9s-corner-handle-topright"),u(Z,"x",re=e[4].x+e[4].w-e[3]/2),u(Z,"y",_e=e[4].y-e[3]/2),u(Z,"height",e[3]),u(Z,"width",e[3]),u(te,"class","a9s-corner-handle a9s-corner-handle-bottomright"),u(te,"x",Ve=e[4].x+e[4].w-e[3]/2),u(te,"y",je=e[4].y+e[4].h-e[3]/2),u(te,"height",e[3]),u(te,"width",e[3]),u(ne,"class","a9s-corner-handle a9s-corner-handle-bottomleft"),u(ne,"x",We=e[4].x-e[3]/2),u(ne,"y",qe=e[4].y+e[4].h-e[3]/2),u(ne,"height",e[3]),u(ne,"width",e[3])},m(D,v){P(D,t,v),P(D,a,v),P(D,l,v),P(D,m,v),P(D,g,v),P(D,_,v),P(D,p,v),P(D,j,v),P(D,I,v),P(D,S,v),P(D,M,v),P(D,Pe,v),P(D,ee,v),P(D,W,v),P(D,Z,v),P(D,dt,v),P(D,te,v),P(D,ht,v),P(D,ne,v),mt||(Bn=[K(t,"pointerdown",function(){q(e[10](C.SHAPE))&&e[10](C.SHAPE).apply(this,arguments)}),K(l,"pointerdown",function(){q(e[10](C.SHAPE))&&e[10](C.SHAPE).apply(this,arguments)}),K(g,"pointerdown",function(){q(e[10](C.TOP))&&e[10](C.TOP).apply(this,arguments)}),K(p,"pointerdown",function(){q(e[10](C.RIGHT))&&e[10](C.RIGHT).apply(this,arguments)}),K(I,"pointerdown",function(){q(e[10](C.BOTTOM))&&e[10](C.BOTTOM).apply(this,arguments)}),K(M,"pointerdown",function(){q(e[10](C.LEFT))&&e[10](C.LEFT).apply(this,arguments)}),K(ee,"pointerdown",function(){q(e[10](C.TOP_LEFT))&&e[10](C.TOP_LEFT).apply(this,arguments)}),K(Z,"pointerdown",function(){q(e[10](C.TOP_RIGHT))&&e[10](C.TOP_RIGHT).apply(this,arguments)}),K(te,"pointerdown",function(){q(e[10](C.BOTTOM_RIGHT))&&e[10](C.BOTTOM_RIGHT).apply(this,arguments)}),K(ne,"pointerdown",function(){q(e[10](C.BOTTOM_LEFT))&&e[10](C.BOTTOM_LEFT).apply(this,arguments)})],mt=!0)},p(D,v){e=D,v&2&&n!==(n=e[1]?"display:none;":void 0)&&u(t,"style",n),v&16&&o!==(o=e[4].x)&&u(t,"x",o),v&16&&i!==(i=e[4].y)&&u(t,"y",i),v&16&&s!==(s=e[4].w)&&u(t,"width",s),v&16&&r!==(r=e[4].h)&&u(t,"height",r),v&2&&u(l,"style",e[1]),v&16&&f!==(f=e[4].x)&&u(l,"x",f),v&16&&h!==(h=e[4].y)&&u(l,"y",h),v&16&&c!==(c=e[4].w)&&u(l,"width",c),v&16&&d!==(d=e[4].h)&&u(l,"height",d),v&16&&y!==(y=e[4].x)&&u(g,"x",y),v&16&&E!==(E=e[4].y)&&u(g,"y",E),v&16&&b!==(b=e[4].w)&&u(g,"width",b),v&16&&L!==(L=e[4].x+e[4].w)&&u(p,"x",L),v&16&&T!==(T=e[4].y)&&u(p,"y",T),v&16&&k!==(k=e[4].h)&&u(p,"height",k),v&16&&z!==(z=e[4].x)&&u(I,"x",z),v&16&&w!==(w=e[4].y+e[4].h)&&u(I,"y",w),v&16&&A!==(A=e[4].w)&&u(I,"width",A),v&16&&R!==(R=e[4].x)&&u(M,"x",R),v&16&&G!==(G=e[4].y)&&u(M,"y",G),v&16&&$!==($=e[4].h)&&u(M,"height",$),v&24&&Y!==(Y=e[4].x-e[3]/2)&&u(ee,"x",Y),v&24&&Q!==(Q=e[4].y-e[3]/2)&&u(ee,"y",Q),v&8&&u(ee,"height",e[3]),v&8&&u(ee,"width",e[3]),v&24&&re!==(re=e[4].x+e[4].w-e[3]/2)&&u(Z,"x",re),v&24&&_e!==(_e=e[4].y-e[3]/2)&&u(Z,"y",_e),v&8&&u(Z,"height",e[3]),v&8&&u(Z,"width",e[3]),v&24&&Ve!==(Ve=e[4].x+e[4].w-e[3]/2)&&u(te,"x",Ve),v&24&&je!==(je=e[4].y+e[4].h-e[3]/2)&&u(te,"y",je),v&8&&u(te,"height",e[3]),v&8&&u(te,"width",e[3]),v&24&&We!==(We=e[4].x-e[3]/2)&&u(ne,"x",We),v&24&&qe!==(qe=e[4].y+e[4].h-e[3]/2)&&u(ne,"y",qe),v&8&&u(ne,"height",e[3]),v&8&&u(ne,"width",e[3])},d(D){D&&B(t),D&&B(a),D&&B(l),D&&B(m),D&&B(g),D&&B(_),D&&B(p),D&&B(j),D&&B(I),D&&B(S),D&&B(M),D&&B(Pe),D&&B(ee),D&&B(W),D&&B(Z),D&&B(dt),D&&B(te),D&&B(ht),D&&B(ne),mt=!1,le(Bn)}}}function Ro(e){let t,n;return t=new it({props:{shape:e[0],transform:e[2],editor:e[5],$$slots:{default:[Po,({grab:o})=>({10:o}),({grab:o})=>o?1024:0]},$$scope:{ctx:e}}}),t.$on("grab",e[7]),t.$on("change",e[8]),t.$on("release",e[9]),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,[i]){const s={};i&1&&(s.shape=o[0]),i&4&&(s.transform=o[2]),i&3098&&(s.$$scope={dirty:i,ctx:o}),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function Yo(e,t,n){let o,i,{shape:s}=t,{computedStyle:r=void 0}=t,{transform:a}=t,{viewportScale:l=1}=t;const f=(m,g,y)=>{const E=m.geometry.bounds;let[b,_]=[E.minX,E.minY],[p,L]=[E.maxX,E.maxY];const[T,k]=y;if(g===C.SHAPE)b+=T,p+=T,_+=k,L+=k;else{switch(g){case C.TOP:case C.TOP_LEFT:case C.TOP_RIGHT:{_+=k;break}case C.BOTTOM:case C.BOTTOM_LEFT:case C.BOTTOM_RIGHT:{L+=k;break}}switch(g){case C.LEFT:case C.TOP_LEFT:case C.BOTTOM_LEFT:{b+=T;break}case C.RIGHT:case C.TOP_RIGHT:case C.BOTTOM_RIGHT:{p+=T;break}}}const j=Math.min(b,p),I=Math.min(_,L),z=Math.abs(p-b),w=Math.abs(L-_);return{...m,geometry:{x:j,y:I,w:z,h:w,bounds:{minX:j,minY:I,maxX:j+z,maxY:I+w}}}};function h(m){we.call(this,e,m)}function c(m){we.call(this,e,m)}function d(m){we.call(this,e,m)}return e.$$set=m=>{"shape"in m&&n(0,s=m.shape),"computedStyle"in m&&n(1,r=m.computedStyle),"transform"in m&&n(2,a=m.transform),"viewportScale"in m&&n(6,l=m.viewportScale)},e.$$.update=()=>{e.$$.dirty&1&&n(4,o=s.geometry),e.$$.dirty&64&&n(3,i=10/l)},[s,r,a,i,o,f,l,h,c,d]}class Zt extends se{constructor(t){super(),ie(this,t,Yo,Ro,x,{shape:0,computedStyle:1,transform:2,viewportScale:6})}}const Qt=new Map([[V.RECTANGLE,Zt],[V.POLYGON,Jt]]),ot=e=>Qt.get(e.type),xt=(e,t)=>Qt.set(e,t),C=e=>`HANDLE-${e}`;C.SHAPE="SHAPE",C.TOP="TOP",C.RIGHT="RIGHT",C.BOTTOM="BOTTOM",C.LEFT="LEFT",C.TOP_LEFT="TOP_LEFT",C.TOP_RIGHT="TOP_RIGHT",C.BOTTOM_RIGHT="BOTTOM_RIGHT",C.BOTTOM_LEFT="BOTTOM_LEFT";const Do=e=>({}),$t=e=>({grab:e[0]});function Uo(e){let t,n,o,i;const s=e[7].default,r=Rn(s,e,e[6],$t);return{c(){t=U("g"),r&&r.c(),u(t,"class","a9s-annotation selected")},m(a,l){P(a,t,l),r&&r.m(t,null),n=!0,o||(i=[K(t,"pointerup",e[2]),K(t,"pointermove",e[1])],o=!0)},p(a,[l]){r&&r.p&&(!n||l&64)&&Dn(r,s,a,a[6],n?Yn(s,a[6],l,Do):Un(a[6]),$t)},i(a){n||(X(r,a),n=!0)},o(a){N(r,a),n=!1},d(a){a&&B(t),r&&r.d(a),o=!1,le(i)}}}function Xo(e,t,n){let{$$slots:o={},$$scope:i}=t;const s=ye();let{shape:r}=t,{editor:a}=t,{transform:l}=t,f=null,h,c=null;const d=y=>E=>{f=y,h=l.elementToImage(E.offsetX,E.offsetY),c=r,E.target.setPointerCapture(E.pointerId),s("grab")},m=y=>{if(f){const[E,b]=l.elementToImage(y.offsetX,y.offsetY),_=[E-h[0],b-h[1]];n(3,r=a(c,f,_)),s("change",r)}},g=y=>{y.target.releasePointerCapture(y.pointerId),f=null,c=r,s("release")};return e.$$set=y=>{"shape"in y&&n(3,r=y.shape),"editor"in y&&n(4,a=y.editor),"transform"in y&&n(5,l=y.transform),"$$scope"in y&&n(6,i=y.$$scope)},[d,m,g,r,a,l,i,o]}class it extends se{constructor(t){super(),ie(this,t,Xo,Uo,x,{shape:3,editor:4,transform:5})}}const Ge=(e,t)=>{const n=typeof t=="function"?t(e):t;if(n){const{fill:o,fillOpacity:i}=n;let s="";return o&&(s+=`fill:${o};stroke:${o};`),s+=`fill-opacity:${i||"0.25"};`,s}};function No(e,t,n){let o;const i=ye();let{annotation:s}=t,{editor:r}=t,{style:a=void 0}=t,{target:l}=t,{transform:f}=t,{viewportScale:h}=t,c;return ve(()=>(n(6,c=new r({target:l,props:{shape:s.target.selector,computedStyle:o,transform:f,viewportScale:h}})),c.$on("change",d=>{c.$$set({shape:d.detail}),i("change",d.detail)}),c.$on("grab",d=>i("grab",d.detail)),c.$on("release",d=>i("release",d.detail)),()=>{c.$destroy()})),e.$$set=d=>{"annotation"in d&&n(0,s=d.annotation),"editor"in d&&n(1,r=d.editor),"style"in d&&n(2,a=d.style),"target"in d&&n(3,l=d.target),"transform"in d&&n(4,f=d.transform),"viewportScale"in d&&n(5,h=d.viewportScale)},e.$$.update=()=>{e.$$.dirty&5&&(o=Ge(s,a)),e.$$.dirty&65&&s&&(c==null||c.$set({shape:s.target.selector})),e.$$.dirty&80&&c&&c.$set({transform:f}),e.$$.dirty&96&&c&&c.$set({viewportScale:h})},[s,r,a,l,f,h,c]}class en extends se{constructor(t){super(),ie(this,t,No,null,x,{annotation:0,editor:1,style:2,target:3,transform:4,viewportScale:5})}}function Go(e,t,n){const o=ye();let{drawingMode:i}=t,{target:s}=t,{tool:r}=t,{transform:a}=t,{viewportScale:l}=t,f;return ve(()=>{const h=s.closest("svg"),c=[],d=(m,g,y)=>{h.addEventListener(m,g,y),c.push(()=>h.removeEventListener(m,g,y))};return n(5,f=new r({target:s,props:{addEventListener:d,drawingMode:i,transform:a,viewportScale:l}})),f.$on("create",m=>o("create",m.detail)),()=>{c.forEach(m=>m()),f.$destroy()}}),e.$$set=h=>{"drawingMode"in h&&n(0,i=h.drawingMode),"target"in h&&n(1,s=h.target),"tool"in h&&n(2,r=h.tool),"transform"in h&&n(3,a=h.transform),"viewportScale"in h&&n(4,l=h.viewportScale)},e.$$.update=()=>{e.$$.dirty&40&&f&&f.$set({transform:a}),e.$$.dirty&48&&f&&f.$set({viewportScale:l})},[i,s,r,a,l,f]}class tn extends se{constructor(t){super(),ie(this,t,Go,null,x,{drawingMode:0,target:1,tool:2,transform:3,viewportScale:4})}}function nn(e){let t,n;return{c(){t=U("rect"),n=U("rect"),u(t,"class","a9s-outer"),u(t,"x",e[1]),u(t,"y",e[2]),u(t,"width",e[3]),u(t,"height",e[4]),u(n,"class","a9s-inner"),u(n,"x",e[1]),u(n,"y",e[2]),u(n,"width",e[3]),u(n,"height",e[4])},m(o,i){P(o,t,i),P(o,n,i)},p(o,i){i&2&&u(t,"x",o[1]),i&4&&u(t,"y",o[2]),i&8&&u(t,"width",o[3]),i&16&&u(t,"height",o[4]),i&2&&u(n,"x",o[1]),i&4&&u(n,"y",o[2]),i&8&&u(n,"width",o[3]),i&16&&u(n,"height",o[4])},d(o){o&&B(t),o&&B(n)}}}function Fo(e){let t,n=e[0]&&nn(e);return{c(){t=U("g"),n&&n.c(),u(t,"class","a9s-annotation a9s-rubberband")},m(o,i){P(o,t,i),n&&n.m(t,null)},p(o,[i]){o[0]?n?n.p(o,i):(n=nn(o),n.c(),n.m(t,null)):n&&(n.d(1),n=null)},i:F,o:F,d(o){o&&B(t),n&&n.d()}}}function Ho(e,t,n){const o=ye();let{addEventListener:i}=t,{drawingMode:s}=t,{transform:r}=t,a,l,f,h,c,d,m;const g=_=>{a=performance.now(),s==="drag"&&(n(0,l=r.elementToImage(_.offsetX,_.offsetY)),f=l,n(1,h=l[0]),n(2,c=l[1]),n(3,d=1),n(4,m=1))},y=_=>{l&&(f=r.elementToImage(_.offsetX,_.offsetY),n(1,h=Math.min(f[0],l[0])),n(2,c=Math.min(f[1],l[1])),n(3,d=Math.abs(f[0]-l[0])),n(4,m=Math.abs(f[1]-l[1])))},E=_=>{const p=performance.now()-a;if(s==="click"){if(p>300)return;_.stopPropagation(),l?b():(n(0,l=r.elementToImage(_.offsetX,_.offsetY)),f=l,n(1,h=l[0]),n(2,c=l[1]),n(3,d=1),n(4,m=1))}else l&&(p>300||d*m>100?(_.stopPropagation(),b()):(n(0,l=null),f=null))},b=()=>{if(d*m>15){const _={type:V.RECTANGLE,geometry:{bounds:{minX:h,minY:c,maxX:h+d,maxY:c+m},x:h,y:c,w:d,h:m}};o("create",_)}n(0,l=null),f=null};return ve(()=>{i("pointerdown",g),i("pointermove",y),i("pointerup",E,!0)}),e.$$set=_=>{"addEventListener"in _&&n(5,i=_.addEventListener),"drawingMode"in _&&n(6,s=_.drawingMode),"transform"in _&&n(7,r=_.transform)},[l,h,c,d,m,i,s,r]}class on extends se{constructor(t){super(),ie(this,t,Ho,Fo,x,{addEventListener:5,drawingMode:6,transform:7})}}const Fe=(e,t)=>{const n=Math.abs(t[0]-e[0]),o=Math.abs(t[1]-e[1]);return Math.sqrt(Math.pow(n,2)+Math.pow(o,2))},zo=(e,t)=>{const{naturalWidth:n,naturalHeight:o}=e;if(!n&&!o){const{width:i,height:s}=e;t.setAttribute("viewBox",`0 0 ${i} ${s}`),e.addEventListener("load",r=>{const a=r.target;t.setAttribute("viewBox",`0 0 ${a.naturalWidth} ${a.naturalHeight}`)})}else t.setAttribute("viewBox",`0 0 ${n} ${o}`)},sn=(e,t)=>{zo(e,t);const{subscribe:n,set:o}=Ne(1);let i;return window.ResizeObserver&&(i=new ResizeObserver(()=>{const r=t.getBoundingClientRect(),{width:a,height:l}=t.viewBox.baseVal,f=Math.max(r.width/a,r.height/l);o(f)}),i.observe(t.parentElement)),{destroy:()=>{i&&i.disconnect()},subscribe:n}},Vo="ontouchstart"in window||navigator.maxTouchPoints>0;function st(e){const t=e.slice(),n=(t[2]?t[0]:[...t[0],t[1]]).map(o=>o.join(",")).join(" ");return t[15]=n,t}function rn(e){let t,n,o,i,s,r=e[2]&&ln(e);return{c(){t=U("polygon"),o=U("polygon"),r&&r.c(),s=ae(),u(t,"class","a9s-outer"),u(t,"points",n=e[15]),u(o,"class","a9s-inner"),u(o,"points",i=e[15])},m(a,l){P(a,t,l),P(a,o,l),r&&r.m(a,l),P(a,s,l)},p(a,l){l&7&&n!==(n=a[15])&&u(t,"points",n),l&7&&i!==(i=a[15])&&u(o,"points",i),a[2]?r?r.p(a,l):(r=ln(a),r.c(),r.m(s.parentNode,s)):r&&(r.d(1),r=null)},d(a){a&&B(t),a&&B(o),r&&r.d(a),a&&B(s)}}}function ln(e){let t,n,o;return{c(){t=U("rect"),u(t,"class","a9s-corner-handle"),u(t,"x",n=e[0][0][0]-e[3]/2),u(t,"y",o=e[0][0][1]-e[3]/2),u(t,"height",e[3]),u(t,"width",e[3])},m(i,s){P(i,t,s)},p(i,s){s&9&&n!==(n=i[0][0][0]-i[3]/2)&&u(t,"x",n),s&9&&o!==(o=i[0][0][1]-i[3]/2)&&u(t,"y",o),s&8&&u(t,"height",i[3]),s&8&&u(t,"width",i[3])},d(i){i&&B(t)}}}function jo(e){let t,n=e[1]&&rn(st(e));return{c(){t=U("g"),n&&n.c(),u(t,"class","a9s-annotation a9s-rubberband")},m(o,i){P(o,t,i),n&&n.m(t,null)},p(o,[i]){o[1]?n?n.p(st(o),i):(n=rn(st(o)),n.c(),n.m(t,null)):n&&(n.d(1),n=null)},i:F,o:F,d(o){o&&B(t),n&&n.d()}}}const Wo=20;function qo(e,t,n){let o;const i=ye();let{addEventListener:s}=t,{drawingMode:r}=t,{transform:a}=t,{viewportScale:l=1}=t,f,h=[],c=null,d=!1;const m=_=>{const{timeStamp:p,offsetX:L,offsetY:T}=_;if(f={timeStamp:p,offsetX:L,offsetY:T},r==="drag"&&h.length===0){const k=a.elementToImage(_.offsetX,_.offsetY);h.push(k),n(1,c=k)}},g=_=>{if(h.length>0&&(n(1,c=a.elementToImage(_.offsetX,_.offsetY)),h.length>2)){const p=Fe(c,h[0])*l;n(2,d=p<Wo)}},y=_=>{if(r==="click"){const p=_.timeStamp-f.timeStamp,L=Fe([f.offsetX,f.offsetY],[_.offsetX,_.offsetY]);if(p>300||L>15)return;if(d)b();else if(h.length===0){const T=a.elementToImage(_.offsetX,_.offsetY);h.push(T),n(1,c=T)}else h.push(c)}else{if(h.length===1&&Fe(h[0],c)<=4){n(0,h=[]),n(1,c=null);return}_.stopImmediatePropagation(),d?b():h.push(c)}},E=()=>{const _=[...h,c],p={type:V.POLYGON,geometry:{bounds:Ce(_),points:_}};Ue(p)>4&&(n(0,h=[]),n(1,c=null),i("create",p))},b=()=>{const _={type:V.POLYGON,geometry:{bounds:Ce(h),points:[...h]}};n(0,h=[]),n(1,c=null),i("create",_)};return ve(()=>{s("pointerdown",m,!0),s("pointermove",g),s("pointerup",y,!0),s("dblclick",E,!0)}),e.$$set=_=>{"addEventListener"in _&&n(4,s=_.addEventListener),"drawingMode"in _&&n(5,r=_.drawingMode),"transform"in _&&n(6,a=_.transform),"viewportScale"in _&&n(7,l=_.viewportScale)},e.$$.update=()=>{e.$$.dirty&128&&n(3,o=10/l)},[h,c,d,o,s,r,a,l]}class Ko extends se{constructor(t){super(),ie(this,t,qo,jo,x,{addEventListener:4,drawingMode:5,transform:6,viewportScale:7})}}const rt=new Map([["rectangle",{tool:on}],["polygon",{tool:Ko}]]),an=()=>[...rt.keys()],lt=e=>rt.get(e),cn=(e,t,n)=>rt.set(e,{tool:t,opts:n});function Jo(e){let t,n,o,i,s;return{c(){t=U("g"),n=U("ellipse"),i=U("ellipse"),u(n,"class","a9s-outer"),u(n,"style",o=e[1]?"display:none;":void 0),u(n,"cx",e[2]),u(n,"cy",e[3]),u(n,"rx",e[4]),u(n,"ry",e[5]),u(i,"class","a9s-inner"),u(i,"style",e[1]),u(i,"cx",e[2]),u(i,"cy",e[3]),u(i,"rx",e[4]),u(i,"ry",e[5]),u(t,"data-id",s=e[0].id)},m(r,a){P(r,t,a),ue(t,n),ue(t,i)},p(r,[a]){a&2&&o!==(o=r[1]?"display:none;":void 0)&&u(n,"style",o),a&2&&u(i,"style",r[1]),a&1&&s!==(s=r[0].id)&&u(t,"data-id",s)},i:F,o:F,d(r){r&&B(t)}}}function Zo(e,t,n){let o,{annotation:i}=t,{geom:s}=t,{style:r=void 0}=t;const{cx:a,cy:l,rx:f,ry:h}=s;return e.$$set=c=>{"annotation"in c&&n(0,i=c.annotation),"geom"in c&&n(6,s=c.geom),"style"in c&&n(7,r=c.style)},e.$$.update=()=>{e.$$.dirty&129&&n(1,o=Ge(i,r))},[i,o,a,l,f,h,s,r]}class Qo extends se{constructor(t){super(),ie(this,t,Zo,Jo,x,{annotation:0,geom:6,style:7})}}function xo(e){let t,n,o,i,s;return{c(){t=U("g"),n=U("polygon"),i=U("polygon"),u(n,"class","a9s-outer"),u(n,"style",o=e[1]?"display:none;":void 0),u(n,"points",e[2].map($o).join(" ")),u(i,"class","a9s-inner"),u(i,"style",e[1]),u(i,"points",e[2].map(ei).join(" ")),u(t,"data-id",s=e[0].id)},m(r,a){P(r,t,a),ue(t,n),ue(t,i)},p(r,[a]){a&2&&o!==(o=r[1]?"display:none;":void 0)&&u(n,"style",o),a&2&&u(i,"style",r[1]),a&1&&s!==(s=r[0].id)&&u(t,"data-id",s)},i:F,o:F,d(r){r&&B(t)}}}const $o=e=>e.join(","),ei=e=>e.join(",");function ti(e,t,n){let o,{annotation:i}=t,{geom:s}=t,{style:r=void 0}=t;const{points:a}=s;return e.$$set=l=>{"annotation"in l&&n(0,i=l.annotation),"geom"in l&&n(3,s=l.geom),"style"in l&&n(4,r=l.style)},e.$$.update=()=>{e.$$.dirty&17&&n(1,o=Ge(i,r))},[i,o,a,s,r]}class ni extends se{constructor(t){super(),ie(this,t,ti,xo,x,{annotation:0,geom:3,style:4})}}function oi(e){let t,n,o,i,s;return{c(){t=U("g"),n=U("rect"),i=U("rect"),u(n,"class","a9s-outer"),u(n,"style",o=e[5]?"display:none;":void 0),u(n,"x",e[4]),u(n,"y",e[3]),u(n,"width",e[2]),u(n,"height",e[1]),u(i,"class","a9s-inner"),u(i,"style",e[5]),u(i,"x",e[4]),u(i,"y",e[3]),u(i,"width",e[2]),u(i,"height",e[1]),u(t,"data-id",s=e[0].id)},m(r,a){P(r,t,a),ue(t,n),ue(t,i)},p(r,[a]){a&32&&o!==(o=r[5]?"display:none;":void 0)&&u(n,"style",o),a&16&&u(n,"x",r[4]),a&8&&u(n,"y",r[3]),a&4&&u(n,"width",r[2]),a&2&&u(n,"height",r[1]),a&32&&u(i,"style",r[5]),a&16&&u(i,"x",r[4]),a&8&&u(i,"y",r[3]),a&4&&u(i,"width",r[2]),a&2&&u(i,"height",r[1]),a&1&&s!==(s=r[0].id)&&u(t,"data-id",s)},i:F,o:F,d(r){r&&B(t)}}}function ii(e,t,n){let o,i,s,r,a,{annotation:l}=t,{geom:f}=t,{style:h=void 0}=t;return e.$$set=c=>{"annotation"in c&&n(0,l=c.annotation),"geom"in c&&n(6,f=c.geom),"style"in c&&n(7,h=c.style)},e.$$.update=()=>{e.$$.dirty&129&&n(5,o=Ge(l,h)),e.$$.dirty&64&&n(4,{x:i,y:s,w:r,h:a}=f,i,(n(3,s),n(6,f)),(n(2,r),n(6,f)),(n(1,a),n(6,f)))},[l,a,r,s,i,o,f,h]}class si extends se{constructor(t){super(),ie(this,t,ii,oi,x,{annotation:0,geom:6,style:7})}}const ri={elementToImage:(e,t)=>[e,t]},fn=e=>({elementToImage:(t,n)=>{const o=e.getBoundingClientRect(),i=e.createSVGPoint();i.x=t+o.x,i.y=n+o.y;const{x:s,y:r}=i.matrixTransform(e.getScreenCTM().inverse());return[s,r]}}),li=250,un=(e,t)=>{const n=ye();let o;return{onPointerDown:()=>o=performance.now(),onPointerUp:r=>{if(performance.now()-o<li){const{x:l,y:f}=ai(r,e),h=t.getAt(l,f);h?n("click",{originalEvent:r,annotation:h}):n("click",{originalEvent:r})}}}},ai=(e,t)=>{const n=t.createSVGPoint(),o=t.getBoundingClientRect(),i=e.clientX-o.x,s=e.clientY-o.y,{left:r,top:a}=t.getBoundingClientRect();return n.x=i+r,n.y=s+a,n.matrixTransform(t.getScreenCTM().inverse())};function dn(e,t,n){const o=e.slice();return o[28]=t[n],o}function hn(e,t,n){const o=e.slice();return o[31]=t[n],o}function at(e){const t=e.slice(),n=t[31].target.selector;return t[34]=n,t}function mn(e){let t=e[31].id,n,o,i=gn(e);return{c(){i.c(),n=ae()},m(s,r){i.m(s,r),P(s,n,r),o=!0},p(s,r){r[0]&4096&&x(t,t=s[31].id)?(ce(),N(i,1,1,F),fe(),i=gn(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){N(i),o=!1},d(s){s&&B(n),i.d(s)}}}function ci(e){let t,n;return t=new ni({props:{annotation:e[31],geom:e[34].geometry,style:e[1]}}),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,i){const s={};i[0]&4096&&(s.annotation=o[31]),i[0]&4096&&(s.geom=o[34].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function fi(e){let t,n;return t=new si({props:{annotation:e[31],geom:e[34].geometry,style:e[1]}}),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,i){const s={};i[0]&4096&&(s.annotation=o[31]),i[0]&4096&&(s.geom=o[34].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function ui(e){let t,n;return t=new Qo({props:{annotation:e[31],geom:e[34].geometry,style:e[1]}}),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,i){const s={};i[0]&4096&&(s.annotation=o[31]),i[0]&4096&&(s.geom=o[34].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function gn(e){let t,n,o,i;const s=[ui,fi,ci],r=[];function a(l,f){return l[34].type===V.ELLIPSE?0:l[34].type===V.RECTANGLE?1:l[34].type===V.POLYGON?2:-1}return~(t=a(e))&&(n=r[t]=s[t](e)),{c(){n&&n.c(),o=ae()},m(l,f){~t&&r[t].m(l,f),P(l,o,f),i=!0},p(l,f){let h=t;t=a(l),t===h?~t&&r[t].p(l,f):(n&&(ce(),N(r[h],1,1,()=>{r[h]=null}),fe()),~t?(n=r[t],n?n.p(l,f):(n=r[t]=s[t](l),n.c()),X(n,1),n.m(o.parentNode,o)):n=null)},i(l){i||(X(n),i=!0)},o(l){N(n),i=!1},d(l){~t&&r[t].d(l),l&&B(o)}}}function pn(e){let t=!e[7](e[31]),n,o,i=t&&mn(at(e));return{c(){i&&i.c(),n=ae()},m(s,r){i&&i.m(s,r),P(s,n,r),o=!0},p(s,r){r[0]&4224&&(t=!s[7](s[31])),t?i?(i.p(at(s),r),r[0]&4224&&X(i,1)):(i=mn(at(s)),i.c(),X(i,1),i.m(n.parentNode,n)):i&&(ce(),N(i,1,1,()=>{i=null}),fe())},i(s){o||(X(i),o=!0)},o(s){N(i),o=!1},d(s){i&&i.d(s),s&&B(n)}}}function _n(e){let t,n,o,i;const s=[hi,di],r=[];function a(l,f){return l[6]?0:l[2]&&l[0]?1:-1}return~(t=a(e))&&(n=r[t]=s[t](e)),{c(){n&&n.c(),o=ae()},m(l,f){~t&&r[t].m(l,f),P(l,o,f),i=!0},p(l,f){let h=t;t=a(l),t===h?~t&&r[t].p(l,f):(n&&(ce(),N(r[h],1,1,()=>{r[h]=null}),fe()),~t?(n=r[t],n?n.p(l,f):(n=r[t]=s[t](l),n.c()),X(n,1),n.m(o.parentNode,o)):n=null)},i(l){i||(X(n),i=!0)},o(l){N(n),i=!1},d(l){~t&&r[t].d(l),l&&B(o)}}}function di(e){let t=e[2],n,o,i=yn(e);return{c(){i.c(),n=ae()},m(s,r){i.m(s,r),P(s,n,r),o=!0},p(s,r){r[0]&4&&x(t,t=s[2])?(ce(),N(i,1,1,F),fe(),i=yn(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){N(i),o=!1},d(s){s&&B(n),i.d(s)}}}function hi(e){let t,n,o=e[6],i=[];for(let r=0;r<o.length;r+=1)i[r]=bn(dn(e,o,r));const s=r=>N(i[r],1,1,()=>{i[r]=null});return{c(){for(let r=0;r<i.length;r+=1)i[r].c();t=ae()},m(r,a){for(let l=0;l<i.length;l+=1)i[l]&&i[l].m(r,a);P(r,t,a),n=!0},p(r,a){if(a[0]&140370){o=r[6];let l;for(l=0;l<o.length;l+=1){const f=dn(r,o,l);i[l]?(i[l].p(f,a),X(i[l],1)):(i[l]=bn(f),i[l].c(),X(i[l],1),i[l].m(t.parentNode,t))}for(ce(),l=o.length;l<i.length;l+=1)s(l);fe()}},i(r){if(!n){for(let a=0;a<o.length;a+=1)X(i[a]);n=!0}},o(r){i=i.filter(Boolean);for(let a=0;a<i.length;a+=1)N(i[a]);n=!1},d(r){Ke(i,r),r&&B(t)}}}function yn(e){let t,n;return t=new tn({props:{target:e[4],tool:e[2],drawingMode:e[11],transform:e[10],viewportScale:e[13]}}),t.$on("create",e[16]),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,i){const s={};i[0]&16&&(s.target=o[4]),i[0]&4&&(s.tool=o[2]),i[0]&2048&&(s.drawingMode=o[11]),i[0]&1024&&(s.transform=o[10]),i[0]&8192&&(s.viewportScale=o[13]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function wn(e){let t,n;return t=new en({props:{target:e[4],editor:ot(e[28].target.selector),annotation:e[28],style:e[1],transform:e[10],viewportScale:e[13]}}),t.$on("change",function(){q(e[17](e[28]))&&e[17](e[28]).apply(this,arguments)}),{c(){ge(t.$$.fragment)},m(o,i){de(t,o,i),n=!0},p(o,i){e=o;const s={};i[0]&16&&(s.target=e[4]),i[0]&64&&(s.editor=ot(e[28].target.selector)),i[0]&64&&(s.annotation=e[28]),i[0]&2&&(s.style=e[1]),i[0]&1024&&(s.transform=e[10]),i[0]&8192&&(s.viewportScale=e[13]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){N(t.$$.fragment,o),n=!1},d(o){he(t,o)}}}function bn(e){let t=e[28].id,n,o,i=wn(e);return{c(){i.c(),n=ae()},m(s,r){i.m(s,r),P(s,n,r),o=!0},p(s,r){r[0]&64&&x(t,t=s[28].id)?(ce(),N(i,1,1,F),fe(),i=wn(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){N(i),o=!1},d(s){s&&B(n),i.d(s)}}}function mi(e){let t,n,o,i,s,r,a=e[12],l=[];for(let c=0;c<a.length;c+=1)l[c]=pn(hn(e,a,c));const f=c=>N(l[c],1,1,()=>{l[c]=null});let h=e[4]&&_n(e);return{c(){t=U("svg"),n=U("g");for(let c=0;c<l.length;c+=1)l[c].c();o=U("g"),h&&h.c(),u(o,"class","drawing"),u(t,"class","a9s-annotationlayer"),Et(t,"drawing",e[2])},m(c,d){P(c,t,d),ue(t,n);for(let m=0;m<l.length;m+=1)l[m]&&l[m].m(n,null);ue(t,o),h&&h.m(o,null),e[24](o),e[25](t),i=!0,s||(r=[K(t,"pointerup",function(){q(e[8])&&e[8].apply(this,arguments)}),K(t,"pointerdown",function(){q(e[9])&&e[9].apply(this,arguments)})],s=!0)},p(c,d){if(e=c,d[0]&4226){a=e[12];let m;for(m=0;m<a.length;m+=1){const g=hn(e,a,m);l[m]?(l[m].p(g,d),X(l[m],1)):(l[m]=pn(g),l[m].c(),X(l[m],1),l[m].m(n,null))}for(ce(),m=a.length;m<l.length;m+=1)f(m);fe()}e[4]?h?(h.p(e,d),d[0]&16&&X(h,1)):(h=_n(e),h.c(),X(h,1),h.m(o,null)):h&&(ce(),N(h,1,1,()=>{h=null}),fe()),(!i||d[0]&4)&&Et(t,"drawing",e[2])},i(c){if(!i){for(let d=0;d<a.length;d+=1)X(l[d]);X(h),i=!0}},o(c){l=l.filter(Boolean);for(let d=0;d<l.length;d+=1)N(l[d]);N(h),i=!1},d(c){c&&B(t),Ke(l,c),h&&h.d(),e[24](null),e[25](null),s=!1,le(r)}}}function gi(e,t,n){let o,i,s,r,a,l,f,h,c=F,d=()=>(c(),c=_t(z,Y=>n(13,h=Y)),z);e.$$.on_destroy.push(()=>c());let{drawingEnabled:m}=t,{image:g}=t,{preferredDrawingMode:y}=t,{state:E}=t,{style:b=void 0}=t,{tool:_,opts:p}=lt("rectangle"),{tool:L=_,opts:T=p}=t,{user:k}=t,j,I,z;ve(()=>d(n(5,z=sn(g,I))));const{selection:w,store:A}=E;yt(e,w,Y=>n(23,l=Y)),yt(e,A,Y=>n(12,f=Y));let S=null,M=null;const R=Y=>{A.unobserve(S);const Q=Y.filter(({editable:W})=>W).map(({id:W})=>W);Q.length>0?(n(6,M=Q.map(W=>A.getAnnotation(W))),S=W=>{const{updated:Z}=W.changes;n(6,M=Z.map(re=>re.newValue))},A.observe(S,{annotations:Q})):n(6,M=null)},G=Y=>{const Q=$e(),W={id:Q,bodies:[],target:{annotation:Q,selector:Y.detail,creator:k,created:new Date}};A.addAnnotation(W),w.setSelected(W.id)},$=Y=>Q=>{var _e;const{target:W}=Y,Z=10*60*1e3,re=((_e=W.creator)==null?void 0:_e.id)!==k.id||!W.created||new Date().getTime()-W.created.getTime()>Z;A.updateTarget({...W,selector:Q.detail,created:re?W.created:new Date,updated:re?new Date:null,updatedBy:re?k:null})};function Pe(Y){Re[Y?"unshift":"push"](()=>{j=Y,n(4,j)})}function ee(Y){Re[Y?"unshift":"push"](()=>{I=Y,n(3,I)})}return e.$$set=Y=>{"drawingEnabled"in Y&&n(0,m=Y.drawingEnabled),"image"in Y&&n(18,g=Y.image),"preferredDrawingMode"in Y&&n(19,y=Y.preferredDrawingMode),"state"in Y&&n(20,E=Y.state),"style"in Y&&n(1,b=Y.style),"tool"in Y&&n(2,L=Y.tool),"opts"in Y&&n(21,T=Y.opts),"user"in Y&&n(22,k=Y.user)},e.$$.update=()=>{e.$$.dirty[0]&2621440&&n(11,o=(T==null?void 0:T.drawingMode)||y),e.$$.dirty[0]&8&&n(10,i=fn(I)),e.$$.dirty[0]&8&&n(9,{onPointerDown:s,onPointerUp:r}=un(I,A),s,(n(8,r),n(3,I))),e.$$.dirty[0]&8388608&&n(7,a=Y=>l.selected.find(Q=>Q.id===Y.id&&Q.editable)),e.$$.dirty[0]&8388608&&R(l.selected)},[m,b,L,I,j,z,M,a,r,s,i,o,f,h,w,A,G,$,g,y,E,T,k,l,Pe,ee]}class En extends se{constructor(t){super(),ie(this,t,gi,mi,x,{drawingEnabled:0,image:18,preferredDrawingMode:19,state:20,style:1,tool:2,opts:21,user:22},null,[-1,-1])}}function pi(e,t,n,o,i){An(e,t,n||0,o||e.length-1,i||_i)}function An(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,r=t-n+1,a=Math.log(s),l=.5*Math.exp(2*a/3),f=.5*Math.sqrt(a*l*(s-l)/s)*(r-s/2<0?-1:1),h=Math.max(n,Math.floor(t-r*l/s+f)),c=Math.min(o,Math.floor(t+(s-r)*l/s+f));An(e,t,h,c,i)}var d=e[t],m=n,g=o;for(Ie(e,n,t),i(e[o],d)>0&&Ie(e,n,o);m<g;){for(Ie(e,m,g),m++,g--;i(e[m],d)<0;)m++;for(;i(e[g],d)>0;)g--}i(e[n],d)===0?Ie(e,n,g):(g++,Ie(e,g,o)),g<=t&&(n=g+1),t<=g&&(o=g-1)}}function Ie(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function _i(e,t){return e<t?-1:e>t?1:0}class yi{constructor(t=9){this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(t){let n=this.data;const o=[];if(!ze(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let r=0;r<n.children.length;r++){const a=n.children[r],l=n.leaf?i(a):a;ze(t,l)&&(n.leaf?o.push(a):ft(t,l)?this._all(a,o):s.push(a))}n=s.pop()}return o}collides(t){let n=this.data;if(!ze(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],r=n.leaf?this.toBBox(s):s;if(ze(t,r)){if(n.leaf||ft(t,r))return!0;o.push(s)}}n=o.pop()}return!1}load(t){if(!(t&&t.length))return this;if(t.length<this._minEntries){for(let o=0;o<t.length;o++)this.insert(t[o]);return this}let n=this._build(t.slice(),0,t.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(t){return t&&this._insert(t,this.data.height-1),this}clear(){return this.data=Me([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],r=[];let a,l,f;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],a=r.pop(),f=!0),o.leaf){const h=wi(t,o.children,n);if(h!==-1)return o.children.splice(h,1),s.push(o),this._condense(s),this}!f&&!o.leaf&&ft(o,i)?(s.push(o),r.push(a),a=0,l=o,o=o.children[0]):l?(a++,o=l.children[a],f=!1):o=null}return this}toBBox(t){return t}compareMinX(t,n){return t.minX-n.minX}compareMinY(t,n){return t.minY-n.minY}toJSON(){return this.data}fromJSON(t){return this.data=t,this}_all(t,n){const o=[];for(;t;)t.leaf?n.push(...t.children):o.push(...t.children),t=o.pop();return n}_build(t,n,o,i){const s=o-n+1;let r=this._maxEntries,a;if(s<=r)return a=Me(t.slice(n,o+1)),Te(a,this.toBBox),a;i||(i=Math.ceil(Math.log(s)/Math.log(r)),r=Math.ceil(s/Math.pow(r,i-1))),a=Me([]),a.leaf=!1,a.height=i;const l=Math.ceil(s/r),f=l*Math.ceil(Math.sqrt(r));Sn(t,n,o,f,this.compareMinX);for(let h=n;h<=o;h+=f){const c=Math.min(h+f-1,o);Sn(t,h,c,l,this.compareMinY);for(let d=h;d<=c;d+=l){const m=Math.min(d+l-1,c);a.children.push(this._build(t,d,m,i-1))}}return Te(a,this.toBBox),a}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,r=1/0,a;for(let l=0;l<n.children.length;l++){const f=n.children[l],h=ct(f),c=Ai(t,f)-h;c<r?(r=c,s=h<s?h:s,a=f):c===r&&h<s&&(s=h,a=f)}n=a||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],r=this._chooseSubtree(i,this.data,n,s);for(r.children.push(t),ke(r,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(t,n){const o=t[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const r=this._chooseSplitIndex(o,s,i),a=Me(o.children.splice(r,o.children.length-r));a.height=o.height,a.leaf=o.leaf,Te(o,this.toBBox),Te(a,this.toBBox),n?t[n-1].children.push(a):this._splitRoot(o,a)}_splitRoot(t,n){this.data=Me([t,n]),this.data.height=t.height+1,this.data.leaf=!1,Te(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,r=1/0;for(let a=n;a<=o-n;a++){const l=Be(t,0,a,this.toBBox),f=Be(t,a,o,this.toBBox),h=Si(l,f),c=ct(l)+ct(f);h<s?(s=h,i=a,r=c<r?c:r):h===s&&c<r&&(r=c,i=a)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:bi,s=t.leaf?this.compareMinY:Ei,r=this._allDistMargin(t,n,o,i),a=this._allDistMargin(t,n,o,s);r<a&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,r=Be(t,0,n,s),a=Be(t,o-n,o,s);let l=He(r)+He(a);for(let f=n;f<o-n;f++){const h=t.children[f];ke(r,t.leaf?s(h):h),l+=He(r)}for(let f=o-n-1;f>=n;f--){const h=t.children[f];ke(a,t.leaf?s(h):h),l+=He(a)}return l}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)ke(n[i],t)}_condense(t){for(let n=t.length-1,o;n>=0;n--)t[n].children.length===0?n>0?(o=t[n-1].children,o.splice(o.indexOf(t[n]),1)):this.clear():Te(t[n],this.toBBox)}}function wi(e,t,n){if(!n)return t.indexOf(e);for(let o=0;o<t.length;o++)if(n(e,t[o]))return o;return-1}function Te(e,t){Be(e,0,e.children.length,t,e)}function Be(e,t,n,o,i){i||(i=Me(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=t;s<n;s++){const r=e.children[s];ke(i,e.leaf?o(r):r)}return i}function ke(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e}function bi(e,t){return e.minX-t.minX}function Ei(e,t){return e.minY-t.minY}function ct(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function He(e){return e.maxX-e.minX+(e.maxY-e.minY)}function Ai(e,t){return(Math.max(t.maxX,e.maxX)-Math.min(t.minX,e.minX))*(Math.max(t.maxY,e.maxY)-Math.min(t.minY,e.minY))}function Si(e,t){const n=Math.max(e.minX,t.minX),o=Math.max(e.minY,t.minY),i=Math.min(e.maxX,t.maxX),s=Math.min(e.maxY,t.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function ft(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function ze(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function Me(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Sn(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const r=t+Math.ceil((n-t)/o/2)*o;pi(e,r,t,n,i),s.push(t,r,r,n)}}const Ti=()=>{const e=new yi,t=new Map,n=()=>[...t.values()],o=()=>{e.clear(),t.clear()},i=c=>{const{minX:d,minY:m,maxX:g,maxY:y}=c.selector.geometry.bounds,E={minX:d,minY:m,maxX:g,maxY:y,target:c};e.insert(E),t.set(c.annotation,E)},s=c=>{const d=t.get(c.annotation);e.remove(d),t.delete(c.annotation)};return{all:n,clear:o,getAt:(c,d)=>{const g=e.search({minX:c,minY:d,maxX:c,maxY:d}).map(y=>y.target).filter(y=>y.selector.type===V.RECTANGLE||Mt(y.selector,c,d));if(g.length>0)return g.sort((y,E)=>Ue(y.selector)-Ue(E.selector)),g[0]},getIntersecting:(c,d,m,g)=>e.search({minX:c,minY:d,maxX:c+m,maxY:d+g}).map(y=>y.target),insert:i,remove:s,set:(c,d=!0)=>{d&&o();const m=c.map(g=>{const{minX:y,minY:E,maxX:b,maxY:_}=g.selector.geometry.bounds;return{minX:y,minY:E,maxX:b,maxY:_,target:g}});m.forEach(g=>t.set(g.target.annotation,g)),e.load(m)},size:()=>e.all().length,update:(c,d)=>{s(c),i(d)}}},Tn=e=>{const t=ho(),n=Ti(),o=to(t,e.pointerSelectAction),i=eo(t),s=_o();return t.observe(({changes:l})=>{n.set(l.created.map(f=>f.target),!1),l.deleted.forEach(f=>n.remove(f.target)),l.updated.forEach(({oldValue:f,newValue:h})=>n.update(f.target,h.target))}),{store:{...t,getAt:(l,f)=>{const h=n.getAt(l,f);return h?t.getAnnotation(h.annotation):void 0},getIntersecting:(l,f,h,c)=>n.getIntersecting(l,f,h,c).map(d=>t.getAnnotation(d.annotation))},selection:o,hover:i,viewport:s}},Mn=e=>{const t=Tn(e);return{...t,store:mo(t.store)}},Ln=e=>{let t,n;if(e.nodeName==="CANVAS")t=e,n=t.getContext("2d",{willReadFrequently:!0});else{const i=e;t=document.createElement("canvas"),t.width=i.width,t.height=i.height,n=t.getContext("2d",{willReadFrequently:!0}),n.drawImage(i,0,0,i.width,i.height)}let o=0;for(let i=1;i<10;i++)for(let s=1;s<10;s++){const r=Math.round(s*t.width/10),a=Math.round(i*t.height/10),l=n.getImageData(r,a,1,1).data,f=(.299*l[0]+.587*l[1]+.114*l[2])/255;o+=f}return o/81},On=e=>{const t=Ln(e),n=t>.6?"dark":"light";return console.log(`[Annotorious] Image brightness: ${t.toFixed(1)}. Setting ${n} theme.`),n},ut=(e,t,n)=>t.setAttribute("data-theme",n==="auto"?On(e):n),vn=e=>({...e,drawingEnabled:e.drawingEnabled===void 0?!0:e.drawingEnabled,drawingMode:e.drawingMode||"drag",pointerSelectAction:e.pointerSelectAction||et.EDIT,theme:e.theme||"light"}),Cn=navigator.userAgent.indexOf("Mac OS X")!==-1,In=(e,t)=>{const n=t||document,o=r=>{r.key==="Z"&&r.ctrlKey?e.undo():r.key==="Y"&&r.ctrlKey&&e.redo()},i=r=>{r.key==="z"&&r.metaKey&&(r.shiftKey?e.redo():e.undo())},s=()=>{Cn?n.removeEventListener("keydown",i):n.removeEventListener("keydown",o)};return Cn?n.addEventListener("keydown",i):n.addEventListener("keydown",o),{destroy:s}},Oi="",vi="",Ci="",Mi=(e,t={})=>{if(!e)throw"Missing argument: image";const n=typeof e=="string"?document.getElementById(e):e,o=vn(t),i=Mn(o),{selection:s,store:r}=i,a=po(r),l=yo(i,a,o.adapter,o.autoSave),f=document.createElement("DIV");f.style.position="relative",f.style.display="inline-block",n.style.display="block",n.parentNode.insertBefore(f,n),f.appendChild(n);const h=In(a);let c=Xt();ut(n,f,o.theme);const d=new En({target:f,props:{drawingEnabled:o.drawingEnabled,image:n,preferredDrawingMode:o.drawingMode,state:i,style:o.style,user:c}});d.$on("click",I=>{const{originalEvent:z,annotation:w}=I.detail;w?s.clickSelect(w.id,z):s.isEmpty()||s.clear()});const m=Ut(i,a,o.adapter),g=()=>{d.$destroy(),f.parentNode.insertBefore(n,f),f.parentNode.removeChild(f),h.destroy(),a.destroy()},y=()=>c,E=(I,z,w)=>cn(I,z,w),b=(I,z)=>xt(I,z),_=I=>{const{tool:z,opts:w}=lt(I);d.$set({tool:z,opts:w})},p=I=>d.$set({drawingEnabled:I}),L=I=>{console.warn("Filter not implemented yet")},T=I=>d.$set({style:I}),k=I=>ut(n,f,I),j=I=>{c=I,d.$set({user:I})};return{...m,destroy:g,getUser:y,listDrawingTools:an,on:l.on,off:l.off,registerDrawingTool:E,registerShapeEditor:b,setDrawingEnabled:p,setDrawingTool:_,setFilter:L,setStyle:T,setTheme:k,setUser:j,state:i}};O.Editor=it,O.EditorMount=en,O.Handle=C,O.IdentityTransform=ri,O.Origin=H,O.PRESENCE_KEY=Oo,O.PointerSelectAction=et,O.PolygonEditor=Jt,O.RectangleEditor=Zt,O.RectangleUtil=Lt,O.RubberbandRectangle=on,O.SVGAnnotationLayer=En,O.ShapeType=V,O.ToolMount=tn,O.W3CImageFormat=Co,O.addEventListeners=un,O.boundsFromPoints=Ce,O.computeArea=Ue,O.createAnonymousGuest=Xt,O.createBaseAnnotator=Ut,O.createBody=io,O.createDefaultAppearenceProvider=Ht,O.createImageAnnotator=Mi,O.createImageAnnotatorState=Tn,O.createPresenceState=vo,O.createSVGTransform=fn,O.createSvelteImageAnnotatorState=Mn,O.defaultColorProvider=Ft,O.detectTheme=On,O.diffAnnotations=nt,O.distance=Fe,O.enableResponsive=sn,O.fillDefaults=vn,O.getContributors=oo,O.getEditor=ot,O.getTool=lt,O.initKeyboardCommands=In,O.intersects=Mt,O.isTouch=Vo,O.listDrawingTools=an,O.parseAll=Dt,O.parseFragmentSelector=Ot,O.parseSVGSelector=Bt,O.parseW3CBodies=Nt,O.parseW3CImageAnnotation=zt,O.registerEditor=xt,O.registerShapeUtil=De,O.registerTool=cn,O.sampleBrightness=Ln,O.serializeAll=wo,O.serializeFragmentSelector=vt,O.serializeSVGSelector=kt,O.serializeW3CBodies=Gt,O.serializeW3CImageAnnotation=Vt,O.setTheme=ut,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
1
+ (function(v,V){typeof exports=="object"&&typeof module<"u"?V(exports):typeof define=="function"&&define.amd?define(["exports"],V):(v=typeof globalThis<"u"?globalThis:v||self,V(v.Annotorious={}))})(this,function(v){"use strict";function V(){}function Pn(e,t){for(const n in t)e[n]=t[n];return e}function _t(e){return e()}function wt(){return Object.create(null)}function oe(e){e.forEach(_t)}function W(e){return typeof e=="function"}function Z(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}function Rn(e){return Object.keys(e).length===0}function bt(e,...t){if(e==null)return V;const n=e.subscribe(...t);return n.unsubscribe?()=>n.unsubscribe():n}function Et(e,t,n){e.$$.on_destroy.push(bt(t,n))}function Un(e,t,n,o){if(e){const i=At(e,t,n,o);return e[0](i)}}function At(e,t,n,o){return e[1]&&o?Pn(n.ctx.slice(),e[1](o(t))):n.ctx}function Dn(e,t,n,o){if(e[2]&&o){const i=e[2](o(n));if(t.dirty===void 0)return i;if(typeof i=="object"){const s=[],r=Math.max(t.dirty.length,i.length);for(let a=0;a<r;a+=1)s[a]=t.dirty[a]|i[a];return s}return t.dirty|i}return t.dirty}function Yn(e,t,n,o,i,s){if(i){const r=At(t,n,o,s);e.p(r,i)}}function Xn(e){if(e.ctx.length>32){const t=[],n=e.ctx.length/32;for(let o=0;o<n;o++)t[o]=-1;return t}return-1}function le(e,t){e.appendChild(t)}function k(e,t,n){e.insertBefore(t,n||null)}function B(e){e.parentNode&&e.parentNode.removeChild(e)}function Qe(e,t){for(let n=0;n<e.length;n+=1)e[n]&&e[n].d(t)}function Y(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function Tt(e){return document.createTextNode(e)}function ee(){return Tt(" ")}function ie(){return Tt("")}function q(e,t,n,o){return e.addEventListener(t,n,o),()=>e.removeEventListener(t,n,o)}function f(e,t,n){n==null?e.removeAttribute(t):e.getAttribute(t)!==n&&e.setAttribute(t,n)}function Nn(e){return Array.from(e.childNodes)}function St(e,t,n){e.classList[n?"add":"remove"](t)}function Gn(e,t,{bubbles:n=!1,cancelable:o=!1}={}){const i=document.createEvent("CustomEvent");return i.initCustomEvent(e,n,o,t),i}let Oe;function Le(e){Oe=e}function vt(){if(!Oe)throw new Error("Function called outside component initialization");return Oe}function Ie(e){vt().$$.on_mount.push(e)}function me(){const e=vt();return(t,n,{cancelable:o=!1}={})=>{const i=e.$$.callbacks[t];if(i){const s=Gn(t,n,{cancelable:o});return i.slice().forEach(r=>{r.call(e,s)}),!s.defaultPrevented}return!0}}function pe(e,t){const n=e.$$.callbacks[t.type];n&&n.slice().forEach(o=>o.call(this,t))}const ye=[],Ue=[];let _e=[];const Mt=[],Vn=Promise.resolve();let xe=!1;function zn(){xe||(xe=!0,Vn.then(Ot))}function $e(e){_e.push(e)}const et=new Set;let we=0;function Ot(){if(we!==0)return;const e=Oe;do{try{for(;we<ye.length;){const t=ye[we];we++,Le(t),Fn(t.$$)}}catch(t){throw ye.length=0,we=0,t}for(Le(null),ye.length=0,we=0;Ue.length;)Ue.pop()();for(let t=0;t<_e.length;t+=1){const n=_e[t];et.has(n)||(et.add(n),n())}_e.length=0}while(ye.length);for(;Mt.length;)Mt.pop()();xe=!1,et.clear(),Le(e)}function Fn(e){if(e.fragment!==null){e.update(),oe(e.before_update);const t=e.dirty;e.dirty=[-1],e.fragment&&e.fragment.p(e.ctx,t),e.after_update.forEach($e)}}function Hn(e){const t=[],n=[];_e.forEach(o=>e.indexOf(o)===-1?t.push(o):n.push(o)),n.forEach(o=>o()),_e=t}const De=new Set;let de;function se(){de={r:0,c:[],p:de}}function re(){de.r||oe(de.c),de=de.p}function X(e,t){e&&e.i&&(De.delete(e),e.i(t))}function G(e,t,n,o){if(e&&e.o){if(De.has(e))return;De.add(e),de.c.push(()=>{De.delete(e),o&&(n&&e.d(1),o())}),e.o(t)}else o&&o()}function ue(e){e&&e.c()}function ae(e,t,n,o){const{fragment:i,after_update:s}=e.$$;i&&i.m(t,n),o||$e(()=>{const r=e.$$.on_mount.map(_t).filter(W);e.$$.on_destroy?e.$$.on_destroy.push(...r):oe(r),e.$$.on_mount=[]}),s.forEach($e)}function ce(e,t){const n=e.$$;n.fragment!==null&&(Hn(n.after_update),oe(n.on_destroy),n.fragment&&n.fragment.d(t),n.on_destroy=n.fragment=null,n.ctx=[])}function jn(e,t){e.$$.dirty[0]===-1&&(ye.push(e),zn(),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<<t%31}function te(e,t,n,o,i,s,r,a=[-1]){const l=Oe;Le(e);const d=e.$$={fragment:null,ctx:[],props:s,update:V,not_equal:i,bound:wt(),on_mount:[],on_destroy:[],on_disconnect:[],before_update:[],after_update:[],context:new Map(t.context||(l?l.$$.context:[])),callbacks:wt(),dirty:a,skip_bound:!1,root:t.target||l.$$.root};r&&r(d.root);let h=!1;if(d.ctx=n?n(e,t.props||{},(c,g,...u)=>{const m=u.length?u[0]:g;return d.ctx&&i(d.ctx[c],d.ctx[c]=m)&&(!d.skip_bound&&d.bound[c]&&d.bound[c](m),h&&jn(e,c)),g}):[],d.update(),h=!0,oe(d.before_update),d.fragment=o?o(d.ctx):!1,t.target){if(t.hydrate){const c=Nn(t.target);d.fragment&&d.fragment.l(c),c.forEach(B)}else d.fragment&&d.fragment.c();t.intro&&X(e.$$.fragment),ae(e,t.target,t.anchor,t.customElement),Ot()}Le(l)}class ne{$destroy(){ce(this,1),this.$destroy=V}$on(t,n){if(!W(n))return V;const o=this.$$.callbacks[t]||(this.$$.callbacks[t]=[]);return o.push(n),()=>{const i=o.indexOf(n);i!==-1&&o.splice(i,1)}}$set(t){this.$$set&&!Rn(t)&&(this.$$.skip_bound=!0,this.$$set(t),this.$$.skip_bound=!1)}}var H=(e=>(e.ELLIPSE="ELLIPSE",e.POLYGON="POLYGON",e.RECTANGLE="RECTANGLE",e))(H||{});const tt={},Ye=(e,t)=>tt[e]=t,Xe=e=>tt[e.type].area(e),Lt=(e,t,n)=>tt[e.type].intersects(e,t,n),Be=e=>{let t=1/0,n=1/0,o=-1/0,i=-1/0;return e.forEach(([s,r])=>{t=Math.min(t,s),n=Math.min(n,r),o=Math.max(o,s),i=Math.max(i,r)}),{minX:t,minY:n,maxX:o,maxY:i}},Wn={area:e=>Math.PI*e.geometry.rx*e.geometry.ry,intersects:(e,t,n)=>{const{cx:o,cy:i,rx:s,ry:r}=e.geometry,a=0,l=Math.cos(a),d=Math.sin(a),h=t-o,c=n-i,g=l*h+d*c,u=d*h-l*c;return g*g/(s*s)+u*u/(r*r)<=1}};Ye(H.ELLIPSE,Wn);const qn={area:e=>{const{points:t}=e.geometry;let n=0,o=t.length-1;for(let i=0;i<t.length;i++)n+=(t[o][0]+t[i][0])*(t[o][1]-t[i][1]),o=i;return Math.abs(.5*n)},intersects:(e,t,n)=>{const{points:o}=e.geometry;let i=!1;for(let s=0,r=o.length-1;s<o.length;r=s++){const a=o[s][0],l=o[s][1],d=o[r][0],h=o[r][1];l>n!=h>n&&t<(d-a)*(n-l)/(h-l)+a&&(i=!i)}return i}};Ye(H.POLYGON,qn);const It={area:e=>e.geometry.w*e.geometry.h,intersects:(e,t,n)=>t>=e.geometry.x&&t<=e.geometry.x+e.geometry.w&&n>=e.geometry.y&&n<=e.geometry.y+e.geometry.h};Ye(H.RECTANGLE,It);const Bt=(e,t=!1)=>{const n=typeof e=="string"?e:e.value,o=/^(xywh)=(pixel|percent)?:?(.+?),(.+?),(.+?),(.+)*/g,i=[...n.matchAll(o)][0],[s,r,a,l,d,h,c]=i;if(r!=="xywh")throw new Error("Unsupported MediaFragment: "+n);if(a&&a!=="pixel")throw new Error(`Unsupported MediaFragment unit: ${a}`);const[g,u,m,w]=[l,d,h,c].map(parseFloat);return{type:H.RECTANGLE,geometry:{x:g,y:u,w:m,h:w,bounds:{minX:g,minY:t?u-w:u,maxX:g+m,maxY:t?u:u+w}}}},kt=e=>{const{x:t,y:n,w:o,h:i}=e;return{type:"FragmentSelector",conformsTo:"http://www.w3.org/TR/media-frags/",value:`xywh=pixel:${t},${n},${o},${i}`}},Ct="http://www.w3.org/2000/svg",Pt=e=>{const t=o=>{Array.from(o.attributes).forEach(i=>{i.name.startsWith("on")&&o.removeAttribute(i.name)})},n=e.getElementsByTagName("script");return Array.from(n).reverse().forEach(o=>o.parentNode.removeChild(o)),Array.from(e.querySelectorAll("*")).forEach(t),e},Kn=e=>{const o=new XMLSerializer().serializeToString(e.documentElement).replace("<svg>",`<svg xmlns="${Ct}">`);return new DOMParser().parseFromString(o,"image/svg+xml").documentElement},Jn=e=>{const n=new DOMParser().parseFromString(e,"image/svg+xml"),o=n.lookupPrefix(Ct),i=n.lookupNamespaceURI(null);return o||i?Pt(n).firstChild:Pt(Kn(n)).firstChild},Zn=e=>{const[t,n,o]=e.match(/(<polygon points=["|'])([^("|')]*)/)||[];if(!o)return;const i=o.split(" ").map(s=>s.split(",").map(parseFloat));return{type:H.POLYGON,geometry:{points:i,bounds:Be(i)}}},Qn=e=>{const t=Jn(e),n=parseFloat(t.getAttribute("cx")),o=parseFloat(t.getAttribute("cy")),i=parseFloat(t.getAttribute("rx")),s=parseFloat(t.getAttribute("ry")),r={minX:n-i,minY:o-s,maxX:n+i,maxY:o+s};return{type:H.ELLIPSE,geometry:{cx:n,cy:o,rx:i,ry:s,bounds:r}}},Rt=e=>{const t=typeof e=="string"?e:e.value;if(t.includes("<polygon points="))return Zn(t);if(t.includes("<ellipse "))return Qn(t)},Ut=e=>{let t;if(e.type===H.POLYGON){const n=e.geometry,{points:o}=n;t=`<svg><polygon points="${o.map(i=>i.join(",")).join(" ")}" /></svg>`}else if(e.type===H.ELLIPSE){const n=e.geometry;t=`<svg><ellipse cx="${n.cx}" cy="${n.cy}" rx="${n.rx}" ry="${n.ry}" /></svg>`}if(t)return{type:"SvgSelector",value:t};throw`Unsupported shape type: ${e.type}`};let Ne;const xn=new Uint8Array(16);function $n(){if(!Ne&&(Ne=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!Ne))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return Ne(xn)}const K=[];for(let e=0;e<256;++e)K.push((e+256).toString(16).slice(1));function eo(e,t=0){return K[e[t+0]]+K[e[t+1]]+K[e[t+2]]+K[e[t+3]]+"-"+K[e[t+4]]+K[e[t+5]]+"-"+K[e[t+6]]+K[e[t+7]]+"-"+K[e[t+8]]+K[e[t+9]]+"-"+K[e[t+10]]+K[e[t+11]]+K[e[t+12]]+K[e[t+13]]+K[e[t+14]]+K[e[t+15]]}const Dt={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function nt(e,t,n){if(Dt.randomUUID&&!t&&!e)return Dt.randomUUID();e=e||{};const o=e.random||(e.rng||$n)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(let i=0;i<16;++i)t[n+i]=o[i];return t}return eo(o)}var Yt=Object.prototype.hasOwnProperty;function fe(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&fe(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(Yt.call(e,n)&&++o&&!Yt.call(t,n)||!(n in t)||!fe(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function ot(){}function to(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const be=[];function it(e,t=ot){let n;const o=new Set;function i(a){if(to(e,a)&&(e=a,n)){const l=!be.length;for(const d of o)d[1](),be.push(d,e);if(l){for(let d=0;d<be.length;d+=2)be[d][0](be[d+1]);be.length=0}}}function s(a){i(a(e))}function r(a,l=ot){const d=[a,l];return o.add(d),o.size===1&&(n=t(i)||ot),a(e),()=>{o.delete(d),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:r}}const no=e=>{const{subscribe:t,set:n}=it(null);let o=null;return t(i=>o=i),e.observe(({changes:i})=>{if(o){i.deleted.some(r=>r.id===o)&&n(null);const s=i.updated.find(({oldValue:r})=>r.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:t,set:n}};var Xt=(e=>(e.EDIT="EDIT",e.SELECT="SELECT",e.NONE="NONE",e))(Xt||{});const st={selected:[]},oo=(e,t="EDIT")=>{const{subscribe:n,set:o}=it(st);let i=st;n(c=>i=c);const s=()=>o(st),r=()=>{var c;return((c=i.selected)==null?void 0:c.length)===0},a=c=>{if(i.selected.length===0)return!1;const g=typeof c=="string"?c:c.id;return i.selected.some(u=>u.id===g)},l=(c,g)=>{const u=e.getAnnotation(c);if(u){const m=io(u,t);o(m==="EDIT"?{selected:[{id:c,editable:!0}],pointerEvent:g}:m==="SELECT"?{selected:[{id:c}],pointerEvent:g}:{selected:[],pointerEvent:g})}else console.warn("Invalid selection: "+c)},d=(c,g=!0)=>{const u=Array.isArray(c)?c:[c],m=u.map(w=>e.getAnnotation(w)).filter(w=>w);o({selected:m.map(({id:w})=>({id:w,editable:g}))}),m.length!==u.length&&console.warn("Invalid selection",c)},h=c=>{if(i.selected.length===0)return!1;const{selected:g}=i;g.filter(({id:u})=>c.includes(u)).length>0&&o({selected:g.filter(({id:u})=>!c.includes(u))})};return e.observe(({changes:c})=>h(c.deleted.map(g=>g.id))),{clear:s,clickSelect:l,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:r,isSelected:a,setSelected:d,subscribe:n}},io=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT",so=[];for(let e=0;e<256;++e)so.push((e+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const ro=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},lo=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},ao=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!fe(o,n)?o:void 0}}).filter(({oldBody:n})=>n),co=(e,t)=>!fe(e.target,t.target),Nt=(e,t)=>{const n=ro(e,t),o=lo(e,t),i=ao(e,t);return{oldValue:e,newValue:t,bodiesCreated:n.length>0?n:void 0,bodiesDeleted:o.length>0?o:void 0,bodiesUpdated:i.length>0?i:void 0,targetUpdated:co(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var z=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(z||{});const uo=(e,t)=>{var n,o;const{changes:i,origin:s}=t;if(!(!e.options.origin||e.options.origin===s))return!1;if(e.options.ignore){const{ignore:r}=e.options,a=l=>(l==null?void 0:l.length)>0;if(!(a(i.created)||a(i.deleted))){const l=(n=i.updated)==null?void 0:n.some(h=>a(h.bodiesCreated)||a(h.bodiesDeleted)||a(h.bodiesUpdated)),d=(o=i.updated)==null?void 0:o.some(h=>h.targetUpdated);if(r==="BODY_ONLY"&&l&&!d||r==="TARGET_ONLY"&&d&&!l)return!1}}if(e.options.annotations){const r=new Set([...i.created.map(a=>a.id),...i.deleted.map(a=>a.id),...i.updated.map(({oldValue:a})=>a.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(a=>r.has(a))}else return!0},fo=(e,t)=>{const n=new Set((e.created||[]).map(c=>c.id)),o=new Set((e.updated||[]).map(({newValue:c})=>c.id)),i=new Set((t.created||[]).map(c=>c.id)),s=new Set((t.deleted||[]).map(c=>c.id)),r=new Set((t.updated||[]).map(({oldValue:c})=>c.id)),a=new Set((t.updated||[]).filter(({oldValue:c})=>n.has(c.id)||o.has(c.id)).map(({oldValue:c})=>c.id)),l=[...(e.created||[]).filter(c=>!s.has(c.id)).map(c=>r.has(c.id)?t.updated.find(({oldValue:g})=>g.id===c.id).newValue:c),...t.created||[]],d=[...(e.deleted||[]).filter(c=>!i.has(c.id)),...(t.deleted||[]).filter(c=>!n.has(c.id))],h=[...(e.updated||[]).filter(({newValue:c})=>!s.has(c.id)).map(c=>{const{oldValue:g,newValue:u}=c;if(r.has(u.id)){const m=t.updated.find(w=>w.oldValue.id===u.id).newValue;return Nt(g,m)}else return c}),...(t.updated||[]).filter(({oldValue:c})=>!a.has(c.id))];return{created:l,deleted:d,updated:h}},ho=e=>e.id!==void 0,go=()=>{const e=new Map,t=new Map,n=[],o=(y,T={})=>n.push({onChange:y,options:T}),i=y=>{const T=n.findIndex(E=>E.onChange==y);T>-1&&n.splice(T,1)},s=(y,T)=>{const E={origin:y,changes:{created:T.created||[],updated:T.updated||[],deleted:T.deleted||[]},state:[...e.values()]};n.forEach(O=>{uo(O,E)&&O.onChange(E)})},r=(y,T=z.LOCAL)=>{if(e.get(y.id))throw Error(`Cannot add annotation ${y.id} - exists already`);e.set(y.id,y),y.bodies.forEach(E=>t.set(E.id,y.id)),s(T,{created:[y]})},a=(y,T)=>{const E=typeof y=="string"?T:y,O=typeof y=="string"?y:y.id,C=e.get(O);if(C){const D=Nt(C,E);return O===E.id?e.set(O,E):(e.delete(O),e.set(E.id,E)),C.bodies.forEach(Q=>t.delete(Q.id)),E.bodies.forEach(Q=>t.set(Q.id,E.id)),D}else console.warn(`Cannot update annotation ${O} - does not exist`)},l=(y,T=z.LOCAL,E=z.LOCAL)=>{const O=ho(T)?E:T,C=a(y,T);C&&s(O,{updated:[C]})},d=(y,T=z.LOCAL)=>{const E=y.reduce((O,C)=>{const D=a(C);return D?[...O,D]:O},[]);E.length>0&&s(T,{updated:E})},h=(y,T=z.LOCAL)=>{const E=e.get(y.annotation);if(E){const O={...E,bodies:[...E.bodies,y]};e.set(E.id,O),t.set(y.id,O.id),s(T,{updated:[{oldValue:E,newValue:O,bodiesCreated:[y]}]})}else console.warn(`Attempt to add body to missing annotation: ${y.annotation}`)},c=()=>[...e.values()],g=(y=z.LOCAL)=>{const T=[...e.values()];e.clear(),t.clear(),s(y,{deleted:T})},u=(y,T=!0,E=z.LOCAL)=>{if(T){const O=[...e.values()];e.clear(),t.clear(),y.forEach(C=>{e.set(C.id,C),C.bodies.forEach(D=>t.set(D.id,C.id))}),s(E,{created:y,deleted:O})}else{const O=y.reduce((C,D)=>{const Q=e.get(D.id);return Q?[...C,Q]:C},[]);if(O.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${O.map(C=>C.id).join(", ")}`);y.forEach(C=>{e.set(C.id,C),C.bodies.forEach(D=>t.set(D.id,C.id))}),s(E,{created:y})}},m=y=>{const T=typeof y=="string"?y:y.id,E=e.get(T);if(E)return e.delete(T),E.bodies.forEach(O=>t.delete(O.id)),E;console.warn(`Attempt to delete missing annotation: ${T}`)},w=(y,T=z.LOCAL)=>{const E=m(y);E&&s(T,{deleted:[E]})},b=(y,T=z.LOCAL)=>{const E=y.reduce((O,C)=>{const D=m(C);return D?[...O,D]:O},[]);E.length>0&&s(T,{deleted:E})},_=(y,T=z.LOCAL)=>{const E=e.get(y.annotation);if(E){const O=E.bodies.find(C=>C.id===y.id);if(O){t.delete(O.id);const C={...E,bodies:E.bodies.filter(D=>D.id!==y.id)};e.set(E.id,C),s(T,{updated:[{oldValue:E,newValue:C,bodiesDeleted:[O]}]})}else console.warn(`Attempt to delete missing body ${y.id} from annotation ${y.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${y.annotation}`)},p=y=>{const T=e.get(y);return T?{...T}:void 0},A=y=>{const T=t.get(y);if(T){const E=p(T).bodies.find(O=>O.id===y);if(E)return E;console.error(`Store integrity error: body ${y} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${y}`)},S=(y,T)=>{if(y.annotation!==T.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const E=e.get(y.annotation);if(E){const O=E.bodies.find(D=>D.id===y.id),C={...E,bodies:E.bodies.map(D=>D.id===O.id?T:D)};return e.set(E.id,C),O.id!==T.id&&(t.delete(O.id),t.set(T.id,C.id)),{oldValue:E,newValue:C,bodiesUpdated:[{oldBody:O,newBody:T}]}}else console.warn(`Attempt to add body to missing annotation ${y.annotation}`)},P=(y,T,E=z.LOCAL)=>{const O=S(y,T);s(E,{updated:[O]})},U=(y,T=z.LOCAL)=>{const E=y.map(O=>S({id:O.id,annotation:O.annotation},O));s(T,{updated:E})},N=y=>{const T=e.get(y.annotation);if(T){const E={...T,target:{...T.target,...y}};return e.set(T.id,E),{oldValue:T,newValue:E,targetUpdated:{oldTarget:T.target,newTarget:y}}}else console.warn(`Attempt to update target on missing annotation: ${y.annotation}`)};return{addAnnotation:r,addBody:h,all:c,bulkAddAnnotation:u,bulkDeleteAnnotation:b,bulkUpdateAnnotation:d,bulkUpdateBodies:U,bulkUpdateTargets:(y,T=z.LOCAL)=>{const E=y.map(N).filter(O=>O);E.length>0&&s(T,{updated:E})},clear:g,deleteAnnotation:w,deleteBody:_,getAnnotation:p,getBody:A,observe:o,unobserve:i,updateAnnotation:l,updateBody:P,updateTarget:(y,T=z.LOCAL)=>{const E=N(y);E&&s(T,{updated:[E]})}}},mo=e=>({...e,subscribe:t=>{const n=o=>t(o.state);return e.observe(n),t(e.all()),()=>e.unobserve(n)}});let po=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const yo=250,_o=e=>{const t=po(),n=[];let o=-1,i=!1,s=0;const r=u=>{if(!i){const{changes:m}=u,w=performance.now();if(w-s>yo)n.splice(o+1),n.push(m),o=n.length-1;else{const b=n.length-1;n[b]=fo(n[b],m)}s=w}i=!1};e.observe(r,{origin:z.LOCAL});const a=u=>(u==null?void 0:u.length)>0&&e.bulkDeleteAnnotation(u),l=u=>(u==null?void 0:u.length)>0&&e.bulkAddAnnotation(u,!1),d=u=>(u==null?void 0:u.length)>0&&e.bulkUpdateAnnotation(u.map(({oldValue:m})=>m)),h=u=>(u==null?void 0:u.length)>0&&e.bulkUpdateAnnotation(u.map(({newValue:m})=>m)),c=u=>(u==null?void 0:u.length)>0&&e.bulkAddAnnotation(u,!1),g=u=>(u==null?void 0:u.length)>0&&e.bulkDeleteAnnotation(u);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(r),on:(u,m)=>t.on(u,m),redo:()=>{if(n.length-1>o){i=!0;const{created:u,updated:m,deleted:w}=n[o+1];l(u),h(m),g(w),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:u,updated:m,deleted:w}=n[o];a(u),d(m),c(w),t.emit("undo",n[o]),o-=1}}}},wo=()=>{const{subscribe:e,set:t}=it([]);return{subscribe:e,set:t}},bo=(e,t,n,o)=>{const{store:i,selection:s,hover:r,viewport:a}=e,l=new Map;let d=[],h,c;const g=(_,p)=>{l.has(_)?l.get(_).push(p):l.set(_,[p])},u=(_,p)=>{const A=l.get(_);A&&A.indexOf(p)>0&&A.splice(A.indexOf(p),1)},m=(_,p,A)=>{l.has(_)&&setTimeout(()=>{l.get(_).forEach(S=>{if(n){const P=Array.isArray(p)?p.map(N=>n.serialize(N)):n.serialize(p),U=A?A instanceof PointerEvent?A:n.serialize(A):void 0;S(P,U)}else S(p,A)})},1)},w=()=>{const{selected:_}=s,p=_.map(({id:A})=>i.getAnnotation(A));p.forEach(A=>{const S=d.find(P=>P.id===A.id);(!S||!fe(S,A))&&m("updateAnnotation",A,S)}),d=d.map(A=>p.find(({id:P})=>P===A.id)||A)};s.subscribe(({selected:_})=>{if(!(d.length===0&&_.length===0)){if(d.length===0&&_.length>0)d=_.map(({id:p})=>i.getAnnotation(p));else if(d.length>0&&_.length===0)d.forEach(p=>{const A=i.getAnnotation(p.id);A&&!fe(A,p)&&m("updateAnnotation",A,p)}),d=[];else{const p=new Set(d.map(S=>S.id)),A=new Set(_.map(({id:S})=>S));d.filter(S=>!A.has(S.id)).forEach(S=>{const P=i.getAnnotation(S.id);P&&!fe(P,S)&&m("updateAnnotation",P,S)}),d=[...d.filter(S=>A.has(S.id)),..._.filter(({id:S})=>!p.has(S)).map(({id:S})=>i.getAnnotation(S))]}m("selectionChanged",d)}}),r.subscribe(_=>{!h&&_?m("mouseEnterAnnotation",i.getAnnotation(_)):h&&!_?m("mouseLeaveAnnotation",i.getAnnotation(h)):h&&_&&(m("mouseLeaveAnnotation",i.getAnnotation(h)),m("mouseEnterAnnotation",i.getAnnotation(_))),h=_}),a==null||a.subscribe(_=>m("viewportIntersect",_.map(i.getAnnotation))),i.observe(_=>{o&&(c&&clearTimeout(c),c=setTimeout(w,1e3));const{created:p,deleted:A}=_.changes;p.forEach(S=>m("createAnnotation",S)),A.forEach(S=>m("deleteAnnotation",S)),_.changes.updated.filter(S=>[...S.bodiesCreated||[],...S.bodiesDeleted||[],...S.bodiesUpdated||[]].length>0).forEach(({oldValue:S,newValue:P})=>{const U=d.find(N=>N.id===S.id)||S;d=d.map(N=>N.id===S.id?P:N),m("updateAnnotation",P,U)})},{origin:z.LOCAL}),i.observe(_=>{if(d){const p=new Set(d.map(S=>S.id)),A=_.changes.updated.filter(({newValue:S})=>p.has(S.id)).map(({newValue:S})=>S);A.length>0&&(d=d.map(S=>A.find(U=>U.id===S.id)||S))}},{origin:z.REMOTE});const b=_=>p=>{const{created:A,deleted:S,updated:P}=p;A.forEach(U=>m("createAnnotation",U)),S.forEach(U=>m("deleteAnnotation",U)),_?P.forEach(U=>m("updateAnnotation",U.oldValue,U.newValue)):P.forEach(U=>m("updateAnnotation",U.newValue,U.oldValue))};return t.on("undo",b(!0)),t.on("redo",b(!1)),{on:g,off:u,emit:m}},Eo=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),Ao=(e,t,n)=>{const{store:o,selection:i}=e,s=b=>{if(n){const{parsed:_,error:p}=n.parse(b);_?o.addAnnotation(_,z.REMOTE):console.error(p)}else o.addAnnotation(b,z.REMOTE)},r=()=>i.clear(),a=()=>o.clear(),l=b=>{const _=o.getAnnotation(b);return n&&_?n.serialize(_):_},d=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var b;const _=(((b=i.selected)==null?void 0:b.map(p=>p.id))||[]).map(p=>o.getAnnotation(p));return n?_.map(n.serialize):_},c=b=>fetch(b).then(_=>_.json()).then(_=>(u(_),_)),g=b=>{if(typeof b=="string"){const _=o.getAnnotation(b);return o.deleteAnnotation(b),n?n.serialize(_):_}else{const _=n?n.parse(b).parsed:b;return o.deleteAnnotation(_),b}},u=b=>{if(n){const{parsed:_,failed:p}=Eo(n)(b);p.length>0&&console.warn(`Discarded ${p.length} invalid annotations`,p),o.bulkAddAnnotation(_,!0,z.REMOTE)}else o.bulkAddAnnotation(b,!0,z.REMOTE)},m=b=>{b?i.setSelected(b):i.clear()},w=b=>{if(n){const _=n.parse(b).parsed,p=n.serialize(o.getAnnotation(_.id));return o.updateAnnotation(_),p}else{const _=o.getAnnotation(b.id);return o.updateAnnotation(b),_}};return{addAnnotation:s,cancelSelected:r,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:a,getAnnotationById:l,getAnnotations:d,getSelected:h,loadAnnotations:c,redo:t.redo,removeAnnotation:g,setAnnotations:u,setSelected:m,undo:t.undo,updateAnnotation:w}};let To=e=>crypto.getRandomValues(new Uint8Array(e)),So=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let r="";for(;;){let a=n(i),l=i;for(;l--;)if(r+=e[a[l]&o]||"",r.length===s)return r}}},vo=(e,t=21)=>So(e,t,To),Mo=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Oo=()=>({isGuest:!0,id:vo("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Lo=e=>{const t=JSON.stringify(e);let n=0;for(let o=0,i=t.length;o<i;o++){let s=t.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},Io=(e,t)=>(Array.isArray(e)?e:[e]).map(n=>{const{id:o,type:i,purpose:s,value:r,created:a,creator:l,...d}=n;return{id:o||`temp-${Lo(n)}`,annotation:t,type:i,purpose:s,value:r,created:a,creator:l?typeof l=="object"?{...l}:l:void 0,...d}}),Bo=e=>e.map(t=>{var n;const o={...t};return delete o.annotation,(n=o.id)!=null&&n.startsWith("temp-")&&delete o.id,o});Mo();const ko=(e,t=!1)=>({parse:i=>Gt(i,t),serialize:i=>Vt(i,e)}),Gt=(e,t=!1)=>{const n=e.id||nt(),{body:o,...i}=e,s=Io(o,n),r=Array.isArray(e.target)?e.target[0]:e.target,a=Array.isArray(r.selector)?r.selector[0]:r.selector,l=a.type==="FragmentSelector"?Bt(a,t):a.type==="SvgSelector"?Rt(a):void 0;return l?{parsed:{...i,id:n,bodies:s,target:{...i.target,annotation:n,selector:l}}}:{error:Error(`Unknown selector type: ${a.type}`)}},Vt=(e,t)=>{const n=e.target.selector,o=n.type==H.RECTANGLE?kt(n.geometry):Ut(n);return{...e,"@context":"http://www.w3.org/ns/anno.jsonld",id:e.id,type:"Annotation",body:Bo(e.bodies),target:{source:t,selector:o}}};function zt(e,t,n){const o=e.slice();return o[11]=t[n],o[13]=n,o}function Ft(e){let t,n,o,i,s;return{c(){t=Y("rect"),f(t,"class","a9s-corner-handle"),f(t,"x",n=e[11][0]-e[3]/2),f(t,"y",o=e[11][1]-e[3]/2),f(t,"height",e[3]),f(t,"width",e[3])},m(r,a){k(r,t,a),i||(s=q(t,"pointerdown",function(){W(e[10](L(e[13])))&&e[10](L(e[13])).apply(this,arguments)}),i=!0)},p(r,a){e=r,a&24&&n!==(n=e[11][0]-e[3]/2)&&f(t,"x",n),a&24&&o!==(o=e[11][1]-e[3]/2)&&f(t,"y",o),a&8&&f(t,"height",e[3]),a&8&&f(t,"width",e[3])},d(r){r&&B(t),i=!1,s()}}}function Co(e){let t,n,o,i,s,r,a,l,d,h,c=e[4].points,g=[];for(let u=0;u<c.length;u+=1)g[u]=Ft(zt(e,c,u));return{c(){t=Y("polygon"),i=ee(),s=Y("polygon"),a=ee();for(let u=0;u<g.length;u+=1)g[u].c();l=ie(),f(t,"class","a9s-outer"),f(t,"style",n=e[1]?"display:none;":void 0),f(t,"points",o=e[4].points.map(Ht).join(" ")),f(s,"class","a9s-inner a9s-shape-handle"),f(s,"style",e[1]),f(s,"points",r=e[4].points.map(jt).join(" "))},m(u,m){k(u,t,m),k(u,i,m),k(u,s,m),k(u,a,m);for(let w=0;w<g.length;w+=1)g[w]&&g[w].m(u,m);k(u,l,m),d||(h=[q(t,"pointerdown",function(){W(e[10](L.SHAPE))&&e[10](L.SHAPE).apply(this,arguments)}),q(s,"pointerdown",function(){W(e[10](L.SHAPE))&&e[10](L.SHAPE).apply(this,arguments)})],d=!0)},p(u,m){if(e=u,m&2&&n!==(n=e[1]?"display:none;":void 0)&&f(t,"style",n),m&16&&o!==(o=e[4].points.map(Ht).join(" "))&&f(t,"points",o),m&2&&f(s,"style",e[1]),m&16&&r!==(r=e[4].points.map(jt).join(" "))&&f(s,"points",r),m&1048){c=e[4].points;let w;for(w=0;w<c.length;w+=1){const b=zt(e,c,w);g[w]?g[w].p(b,m):(g[w]=Ft(b),g[w].c(),g[w].m(l.parentNode,l))}for(;w<g.length;w+=1)g[w].d(1);g.length=c.length}},d(u){u&&B(t),u&&B(i),u&&B(s),u&&B(a),Qe(g,u),u&&B(l),d=!1,oe(h)}}}function Po(e){let t,n;return t=new lt({props:{shape:e[0],transform:e[2],editor:e[5],$$slots:{default:[Co,({grab:o})=>({10:o}),({grab:o})=>o?1024:0]},$$scope:{ctx:e}}}),t.$on("change",e[7]),t.$on("grab",e[8]),t.$on("release",e[9]),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,[i]){const s={};i&1&&(s.shape=o[0]),i&4&&(s.transform=o[2]),i&17434&&(s.$$scope={dirty:i,ctx:o}),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}const Ht=e=>e.join(","),jt=e=>e.join(",");function Ro(e,t,n){let o,i,{shape:s}=t,{computedStyle:r=void 0}=t,{transform:a}=t,{viewportScale:l=1}=t;const d=(u,m,w)=>{let b;m===L.SHAPE?b=u.geometry.points.map(([p,A])=>[p+w[0],A+w[1]]):b=u.geometry.points.map(([p,A],S)=>m===L(S)?[p+w[0],A+w[1]]:[p,A]);const _=Be(b);return{...u,geometry:{points:b,bounds:_}}};function h(u){pe.call(this,e,u)}function c(u){pe.call(this,e,u)}function g(u){pe.call(this,e,u)}return e.$$set=u=>{"shape"in u&&n(0,s=u.shape),"computedStyle"in u&&n(1,r=u.computedStyle),"transform"in u&&n(2,a=u.transform),"viewportScale"in u&&n(6,l=u.viewportScale)},e.$$.update=()=>{e.$$.dirty&1&&n(4,o=s.geometry),e.$$.dirty&64&&n(3,i=10/l)},[s,r,a,i,o,d,l,h,c,g]}class Wt extends ne{constructor(t){super(),te(this,t,Ro,Po,Z,{shape:0,computedStyle:1,transform:2,viewportScale:6})}}function Uo(e){let t,n,o,i,s,r,a,l,d,h,c,g,u,m,w,b,_,p,A,S,P,U,N,y,T,E,O,C,D,Q,ve,Me,Re,I,J,F,he,j,ge,We,mt,x,qe,Ke,pt,$,Je,Ze,yt,Cn;return{c(){t=Y("rect"),a=ee(),l=Y("rect"),u=ee(),m=Y("rect"),p=ee(),A=Y("rect"),N=ee(),y=Y("rect"),C=ee(),D=Y("rect"),Re=ee(),I=Y("rect"),he=ee(),j=Y("rect"),mt=ee(),x=Y("rect"),pt=ee(),$=Y("rect"),f(t,"class","a9s-outer"),f(t,"style",n=e[1]?"display:none;":void 0),f(t,"x",o=e[4].x),f(t,"y",i=e[4].y),f(t,"width",s=e[4].w),f(t,"height",r=e[4].h),f(l,"class","a9s-inner a9s-shape-handle"),f(l,"style",e[1]),f(l,"x",d=e[4].x),f(l,"y",h=e[4].y),f(l,"width",c=e[4].w),f(l,"height",g=e[4].h),f(m,"class","a9s-edge-handle a9s-edge-handle-top"),f(m,"x",w=e[4].x),f(m,"y",b=e[4].y),f(m,"height",1),f(m,"width",_=e[4].w),f(A,"class","a9s-edge-handle a9s-edge-handle-right"),f(A,"x",S=e[4].x+e[4].w),f(A,"y",P=e[4].y),f(A,"height",U=e[4].h),f(A,"width",1),f(y,"class","a9s-edge-handle a9s-edge-handle-bottom"),f(y,"x",T=e[4].x),f(y,"y",E=e[4].y+e[4].h),f(y,"height",1),f(y,"width",O=e[4].w),f(D,"class","a9s-edge-handle a9s-edge-handle-left"),f(D,"x",Q=e[4].x),f(D,"y",ve=e[4].y),f(D,"height",Me=e[4].h),f(D,"width",1),f(I,"class","a9s-corner-handle a9s-corner-handle-topleft"),f(I,"x",J=e[4].x-e[3]/2),f(I,"y",F=e[4].y-e[3]/2),f(I,"height",e[3]),f(I,"width",e[3]),f(j,"class","a9s-corner-handle a9s-corner-handle-topright"),f(j,"x",ge=e[4].x+e[4].w-e[3]/2),f(j,"y",We=e[4].y-e[3]/2),f(j,"height",e[3]),f(j,"width",e[3]),f(x,"class","a9s-corner-handle a9s-corner-handle-bottomright"),f(x,"x",qe=e[4].x+e[4].w-e[3]/2),f(x,"y",Ke=e[4].y+e[4].h-e[3]/2),f(x,"height",e[3]),f(x,"width",e[3]),f($,"class","a9s-corner-handle a9s-corner-handle-bottomleft"),f($,"x",Je=e[4].x-e[3]/2),f($,"y",Ze=e[4].y+e[4].h-e[3]/2),f($,"height",e[3]),f($,"width",e[3])},m(R,M){k(R,t,M),k(R,a,M),k(R,l,M),k(R,u,M),k(R,m,M),k(R,p,M),k(R,A,M),k(R,N,M),k(R,y,M),k(R,C,M),k(R,D,M),k(R,Re,M),k(R,I,M),k(R,he,M),k(R,j,M),k(R,mt,M),k(R,x,M),k(R,pt,M),k(R,$,M),yt||(Cn=[q(t,"pointerdown",function(){W(e[10](L.SHAPE))&&e[10](L.SHAPE).apply(this,arguments)}),q(l,"pointerdown",function(){W(e[10](L.SHAPE))&&e[10](L.SHAPE).apply(this,arguments)}),q(m,"pointerdown",function(){W(e[10](L.TOP))&&e[10](L.TOP).apply(this,arguments)}),q(A,"pointerdown",function(){W(e[10](L.RIGHT))&&e[10](L.RIGHT).apply(this,arguments)}),q(y,"pointerdown",function(){W(e[10](L.BOTTOM))&&e[10](L.BOTTOM).apply(this,arguments)}),q(D,"pointerdown",function(){W(e[10](L.LEFT))&&e[10](L.LEFT).apply(this,arguments)}),q(I,"pointerdown",function(){W(e[10](L.TOP_LEFT))&&e[10](L.TOP_LEFT).apply(this,arguments)}),q(j,"pointerdown",function(){W(e[10](L.TOP_RIGHT))&&e[10](L.TOP_RIGHT).apply(this,arguments)}),q(x,"pointerdown",function(){W(e[10](L.BOTTOM_RIGHT))&&e[10](L.BOTTOM_RIGHT).apply(this,arguments)}),q($,"pointerdown",function(){W(e[10](L.BOTTOM_LEFT))&&e[10](L.BOTTOM_LEFT).apply(this,arguments)})],yt=!0)},p(R,M){e=R,M&2&&n!==(n=e[1]?"display:none;":void 0)&&f(t,"style",n),M&16&&o!==(o=e[4].x)&&f(t,"x",o),M&16&&i!==(i=e[4].y)&&f(t,"y",i),M&16&&s!==(s=e[4].w)&&f(t,"width",s),M&16&&r!==(r=e[4].h)&&f(t,"height",r),M&2&&f(l,"style",e[1]),M&16&&d!==(d=e[4].x)&&f(l,"x",d),M&16&&h!==(h=e[4].y)&&f(l,"y",h),M&16&&c!==(c=e[4].w)&&f(l,"width",c),M&16&&g!==(g=e[4].h)&&f(l,"height",g),M&16&&w!==(w=e[4].x)&&f(m,"x",w),M&16&&b!==(b=e[4].y)&&f(m,"y",b),M&16&&_!==(_=e[4].w)&&f(m,"width",_),M&16&&S!==(S=e[4].x+e[4].w)&&f(A,"x",S),M&16&&P!==(P=e[4].y)&&f(A,"y",P),M&16&&U!==(U=e[4].h)&&f(A,"height",U),M&16&&T!==(T=e[4].x)&&f(y,"x",T),M&16&&E!==(E=e[4].y+e[4].h)&&f(y,"y",E),M&16&&O!==(O=e[4].w)&&f(y,"width",O),M&16&&Q!==(Q=e[4].x)&&f(D,"x",Q),M&16&&ve!==(ve=e[4].y)&&f(D,"y",ve),M&16&&Me!==(Me=e[4].h)&&f(D,"height",Me),M&24&&J!==(J=e[4].x-e[3]/2)&&f(I,"x",J),M&24&&F!==(F=e[4].y-e[3]/2)&&f(I,"y",F),M&8&&f(I,"height",e[3]),M&8&&f(I,"width",e[3]),M&24&&ge!==(ge=e[4].x+e[4].w-e[3]/2)&&f(j,"x",ge),M&24&&We!==(We=e[4].y-e[3]/2)&&f(j,"y",We),M&8&&f(j,"height",e[3]),M&8&&f(j,"width",e[3]),M&24&&qe!==(qe=e[4].x+e[4].w-e[3]/2)&&f(x,"x",qe),M&24&&Ke!==(Ke=e[4].y+e[4].h-e[3]/2)&&f(x,"y",Ke),M&8&&f(x,"height",e[3]),M&8&&f(x,"width",e[3]),M&24&&Je!==(Je=e[4].x-e[3]/2)&&f($,"x",Je),M&24&&Ze!==(Ze=e[4].y+e[4].h-e[3]/2)&&f($,"y",Ze),M&8&&f($,"height",e[3]),M&8&&f($,"width",e[3])},d(R){R&&B(t),R&&B(a),R&&B(l),R&&B(u),R&&B(m),R&&B(p),R&&B(A),R&&B(N),R&&B(y),R&&B(C),R&&B(D),R&&B(Re),R&&B(I),R&&B(he),R&&B(j),R&&B(mt),R&&B(x),R&&B(pt),R&&B($),yt=!1,oe(Cn)}}}function Do(e){let t,n;return t=new lt({props:{shape:e[0],transform:e[2],editor:e[5],$$slots:{default:[Uo,({grab:o})=>({10:o}),({grab:o})=>o?1024:0]},$$scope:{ctx:e}}}),t.$on("grab",e[7]),t.$on("change",e[8]),t.$on("release",e[9]),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,[i]){const s={};i&1&&(s.shape=o[0]),i&4&&(s.transform=o[2]),i&3098&&(s.$$scope={dirty:i,ctx:o}),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function Yo(e,t,n){let o,i,{shape:s}=t,{computedStyle:r=void 0}=t,{transform:a}=t,{viewportScale:l=1}=t;const d=(u,m,w)=>{const b=u.geometry.bounds;let[_,p]=[b.minX,b.minY],[A,S]=[b.maxX,b.maxY];const[P,U]=w;if(m===L.SHAPE)_+=P,A+=P,p+=U,S+=U;else{switch(m){case L.TOP:case L.TOP_LEFT:case L.TOP_RIGHT:{p+=U;break}case L.BOTTOM:case L.BOTTOM_LEFT:case L.BOTTOM_RIGHT:{S+=U;break}}switch(m){case L.LEFT:case L.TOP_LEFT:case L.BOTTOM_LEFT:{_+=P;break}case L.RIGHT:case L.TOP_RIGHT:case L.BOTTOM_RIGHT:{A+=P;break}}}const N=Math.min(_,A),y=Math.min(p,S),T=Math.abs(A-_),E=Math.abs(S-p);return{...u,geometry:{x:N,y,w:T,h:E,bounds:{minX:N,minY:y,maxX:N+T,maxY:y+E}}}};function h(u){pe.call(this,e,u)}function c(u){pe.call(this,e,u)}function g(u){pe.call(this,e,u)}return e.$$set=u=>{"shape"in u&&n(0,s=u.shape),"computedStyle"in u&&n(1,r=u.computedStyle),"transform"in u&&n(2,a=u.transform),"viewportScale"in u&&n(6,l=u.viewportScale)},e.$$.update=()=>{e.$$.dirty&1&&n(4,o=s.geometry),e.$$.dirty&64&&n(3,i=10/l)},[s,r,a,i,o,d,l,h,c,g]}class qt extends ne{constructor(t){super(),te(this,t,Yo,Do,Z,{shape:0,computedStyle:1,transform:2,viewportScale:6})}}const Kt=new Map([[H.RECTANGLE,qt],[H.POLYGON,Wt]]),rt=e=>Kt.get(e.type),Jt=(e,t)=>Kt.set(e,t),L=e=>`HANDLE-${e}`;L.SHAPE="SHAPE",L.TOP="TOP",L.RIGHT="RIGHT",L.BOTTOM="BOTTOM",L.LEFT="LEFT",L.TOP_LEFT="TOP_LEFT",L.TOP_RIGHT="TOP_RIGHT",L.BOTTOM_RIGHT="BOTTOM_RIGHT",L.BOTTOM_LEFT="BOTTOM_LEFT";const Xo=e=>({}),Zt=e=>({grab:e[0]});function No(e){let t,n,o,i;const s=e[7].default,r=Un(s,e,e[6],Zt);return{c(){t=Y("g"),r&&r.c(),f(t,"class","a9s-annotation selected")},m(a,l){k(a,t,l),r&&r.m(t,null),n=!0,o||(i=[q(t,"pointerup",e[2]),q(t,"pointermove",e[1])],o=!0)},p(a,[l]){r&&r.p&&(!n||l&64)&&Yn(r,s,a,a[6],n?Dn(s,a[6],l,Xo):Xn(a[6]),Zt)},i(a){n||(X(r,a),n=!0)},o(a){G(r,a),n=!1},d(a){a&&B(t),r&&r.d(a),o=!1,oe(i)}}}function Go(e,t,n){let{$$slots:o={},$$scope:i}=t;const s=me();let{shape:r}=t,{editor:a}=t,{transform:l}=t,d=null,h,c=null;const g=w=>b=>{d=w,h=l.elementToImage(b.offsetX,b.offsetY),c=r,b.target.setPointerCapture(b.pointerId),s("grab")},u=w=>{if(d){const[b,_]=l.elementToImage(w.offsetX,w.offsetY),p=[b-h[0],_-h[1]];n(3,r=a(c,d,p)),s("change",r)}},m=w=>{w.target.releasePointerCapture(w.pointerId),d=null,c=r,s("release")};return e.$$set=w=>{"shape"in w&&n(3,r=w.shape),"editor"in w&&n(4,a=w.editor),"transform"in w&&n(5,l=w.transform),"$$scope"in w&&n(6,i=w.$$scope)},[g,u,m,r,a,l,i,o]}class lt extends ne{constructor(t){super(),te(this,t,Go,No,Z,{shape:3,editor:4,transform:5})}}const Ge=(e,t)=>{const n=typeof t=="function"?t(e):t;if(n){const{fill:o,fillOpacity:i}=n;let s="";return o&&(s+=`fill:${o};stroke:${o};`),s+=`fill-opacity:${i||"0.25"};`,s}};function Vo(e,t,n){let o;const i=me();let{annotation:s}=t,{editor:r}=t,{style:a=void 0}=t,{target:l}=t,{transform:d}=t,{viewportScale:h}=t,c;return Ie(()=>(n(6,c=new r({target:l,props:{shape:s.target.selector,computedStyle:o,transform:d,viewportScale:h}})),c.$on("change",g=>{c.$$set({shape:g.detail}),i("change",g.detail)}),c.$on("grab",g=>i("grab",g.detail)),c.$on("release",g=>i("release",g.detail)),()=>{c.$destroy()})),e.$$set=g=>{"annotation"in g&&n(0,s=g.annotation),"editor"in g&&n(1,r=g.editor),"style"in g&&n(2,a=g.style),"target"in g&&n(3,l=g.target),"transform"in g&&n(4,d=g.transform),"viewportScale"in g&&n(5,h=g.viewportScale)},e.$$.update=()=>{e.$$.dirty&5&&(o=Ge(s,a)),e.$$.dirty&65&&s&&(c==null||c.$set({shape:s.target.selector})),e.$$.dirty&80&&c&&c.$set({transform:d}),e.$$.dirty&96&&c&&c.$set({viewportScale:h})},[s,r,a,l,d,h,c]}class Qt extends ne{constructor(t){super(),te(this,t,Vo,null,Z,{annotation:0,editor:1,style:2,target:3,transform:4,viewportScale:5})}}function zo(e,t,n){const o=me();let{drawingMode:i}=t,{target:s}=t,{tool:r}=t,{transform:a}=t,{viewportScale:l}=t,d;return Ie(()=>{const h=s.closest("svg"),c=[],g=(u,m,w)=>{h.addEventListener(u,m,w),c.push(()=>h.removeEventListener(u,m,w))};return n(5,d=new r({target:s,props:{addEventListener:g,drawingMode:i,transform:a,viewportScale:l}})),d.$on("create",u=>o("create",u.detail)),()=>{c.forEach(u=>u()),d.$destroy()}}),e.$$set=h=>{"drawingMode"in h&&n(0,i=h.drawingMode),"target"in h&&n(1,s=h.target),"tool"in h&&n(2,r=h.tool),"transform"in h&&n(3,a=h.transform),"viewportScale"in h&&n(4,l=h.viewportScale)},e.$$.update=()=>{e.$$.dirty&40&&d&&d.$set({transform:a}),e.$$.dirty&48&&d&&d.$set({viewportScale:l})},[i,s,r,a,l,d]}class xt extends ne{constructor(t){super(),te(this,t,zo,null,Z,{drawingMode:0,target:1,tool:2,transform:3,viewportScale:4})}}function $t(e){let t,n;return{c(){t=Y("rect"),n=Y("rect"),f(t,"class","a9s-outer"),f(t,"x",e[1]),f(t,"y",e[2]),f(t,"width",e[3]),f(t,"height",e[4]),f(n,"class","a9s-inner"),f(n,"x",e[1]),f(n,"y",e[2]),f(n,"width",e[3]),f(n,"height",e[4])},m(o,i){k(o,t,i),k(o,n,i)},p(o,i){i&2&&f(t,"x",o[1]),i&4&&f(t,"y",o[2]),i&8&&f(t,"width",o[3]),i&16&&f(t,"height",o[4]),i&2&&f(n,"x",o[1]),i&4&&f(n,"y",o[2]),i&8&&f(n,"width",o[3]),i&16&&f(n,"height",o[4])},d(o){o&&B(t),o&&B(n)}}}function Fo(e){let t,n=e[0]&&$t(e);return{c(){t=Y("g"),n&&n.c(),f(t,"class","a9s-annotation a9s-rubberband")},m(o,i){k(o,t,i),n&&n.m(t,null)},p(o,[i]){o[0]?n?n.p(o,i):(n=$t(o),n.c(),n.m(t,null)):n&&(n.d(1),n=null)},i:V,o:V,d(o){o&&B(t),n&&n.d()}}}function Ho(e,t,n){const o=me();let{addEventListener:i}=t,{drawingMode:s}=t,{transform:r}=t,a,l,d,h,c,g,u;const m=p=>{a=performance.now(),s==="drag"&&(n(0,l=r.elementToImage(p.offsetX,p.offsetY)),d=l,n(1,h=l[0]),n(2,c=l[1]),n(3,g=1),n(4,u=1))},w=p=>{l&&(d=r.elementToImage(p.offsetX,p.offsetY),n(1,h=Math.min(d[0],l[0])),n(2,c=Math.min(d[1],l[1])),n(3,g=Math.abs(d[0]-l[0])),n(4,u=Math.abs(d[1]-l[1])))},b=p=>{const A=performance.now()-a;if(s==="click"){if(A>300)return;p.stopPropagation(),l?_():(n(0,l=r.elementToImage(p.offsetX,p.offsetY)),d=l,n(1,h=l[0]),n(2,c=l[1]),n(3,g=1),n(4,u=1))}else l&&(A>300||g*u>100?(p.stopPropagation(),_()):(n(0,l=null),d=null))},_=()=>{if(g*u>15){const p={type:H.RECTANGLE,geometry:{bounds:{minX:h,minY:c,maxX:h+g,maxY:c+u},x:h,y:c,w:g,h:u}};o("create",p)}n(0,l=null),d=null};return Ie(()=>{i("pointerdown",m),i("pointermove",w),i("pointerup",b,!0)}),e.$$set=p=>{"addEventListener"in p&&n(5,i=p.addEventListener),"drawingMode"in p&&n(6,s=p.drawingMode),"transform"in p&&n(7,r=p.transform)},[l,h,c,g,u,i,s,r]}class en extends ne{constructor(t){super(),te(this,t,Ho,Fo,Z,{addEventListener:5,drawingMode:6,transform:7})}}const Ve=(e,t)=>{const n=Math.abs(t[0]-e[0]),o=Math.abs(t[1]-e[1]);return Math.sqrt(Math.pow(n,2)+Math.pow(o,2))},Ee=[];function jo(e,t=V){let n;const o=new Set;function i(a){if(Z(e,a)&&(e=a,n)){const l=!Ee.length;for(const d of o)d[1](),Ee.push(d,e);if(l){for(let d=0;d<Ee.length;d+=2)Ee[d][0](Ee[d+1]);Ee.length=0}}}function s(a){i(a(e))}function r(a,l=V){const d=[a,l];return o.add(d),o.size===1&&(n=t(i)||V),a(e),()=>{o.delete(d),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:r}}const Wo=(e,t)=>{const{naturalWidth:n,naturalHeight:o}=e;if(!n&&!o){const{width:i,height:s}=e;t.setAttribute("viewBox",`0 0 ${i} ${s}`),e.addEventListener("load",r=>{const a=r.target;t.setAttribute("viewBox",`0 0 ${a.naturalWidth} ${a.naturalHeight}`)})}else t.setAttribute("viewBox",`0 0 ${n} ${o}`)},tn=(e,t)=>{Wo(e,t);const{subscribe:n,set:o}=jo(1);let i;return window.ResizeObserver&&(i=new ResizeObserver(()=>{const r=t.getBoundingClientRect(),{width:a,height:l}=t.viewBox.baseVal,d=Math.max(r.width/a,r.height/l);o(d)}),i.observe(t.parentElement)),{destroy:()=>{i&&i.disconnect()},subscribe:n}},qo="ontouchstart"in window||navigator.maxTouchPoints>0;function at(e){const t=e.slice(),n=(t[2]?t[0]:[...t[0],t[1]]).map(o=>o.join(",")).join(" ");return t[15]=n,t}function nn(e){let t,n,o,i,s,r=e[2]&&on(e);return{c(){t=Y("polygon"),o=Y("polygon"),r&&r.c(),s=ie(),f(t,"class","a9s-outer"),f(t,"points",n=e[15]),f(o,"class","a9s-inner"),f(o,"points",i=e[15])},m(a,l){k(a,t,l),k(a,o,l),r&&r.m(a,l),k(a,s,l)},p(a,l){l&7&&n!==(n=a[15])&&f(t,"points",n),l&7&&i!==(i=a[15])&&f(o,"points",i),a[2]?r?r.p(a,l):(r=on(a),r.c(),r.m(s.parentNode,s)):r&&(r.d(1),r=null)},d(a){a&&B(t),a&&B(o),r&&r.d(a),a&&B(s)}}}function on(e){let t,n,o;return{c(){t=Y("rect"),f(t,"class","a9s-corner-handle"),f(t,"x",n=e[0][0][0]-e[3]/2),f(t,"y",o=e[0][0][1]-e[3]/2),f(t,"height",e[3]),f(t,"width",e[3])},m(i,s){k(i,t,s)},p(i,s){s&9&&n!==(n=i[0][0][0]-i[3]/2)&&f(t,"x",n),s&9&&o!==(o=i[0][0][1]-i[3]/2)&&f(t,"y",o),s&8&&f(t,"height",i[3]),s&8&&f(t,"width",i[3])},d(i){i&&B(t)}}}function Ko(e){let t,n=e[1]&&nn(at(e));return{c(){t=Y("g"),n&&n.c(),f(t,"class","a9s-annotation a9s-rubberband")},m(o,i){k(o,t,i),n&&n.m(t,null)},p(o,[i]){o[1]?n?n.p(at(o),i):(n=nn(at(o)),n.c(),n.m(t,null)):n&&(n.d(1),n=null)},i:V,o:V,d(o){o&&B(t),n&&n.d()}}}const Jo=20;function Zo(e,t,n){let o;const i=me();let{addEventListener:s}=t,{drawingMode:r}=t,{transform:a}=t,{viewportScale:l=1}=t,d,h=[],c=null,g=!1;const u=p=>{const{timeStamp:A,offsetX:S,offsetY:P}=p;if(d={timeStamp:A,offsetX:S,offsetY:P},r==="drag"&&h.length===0){const U=a.elementToImage(p.offsetX,p.offsetY);h.push(U),n(1,c=U)}},m=p=>{if(h.length>0&&(n(1,c=a.elementToImage(p.offsetX,p.offsetY)),h.length>2)){const A=Ve(c,h[0])*l;n(2,g=A<Jo)}},w=p=>{if(r==="click"){const A=p.timeStamp-d.timeStamp,S=Ve([d.offsetX,d.offsetY],[p.offsetX,p.offsetY]);if(A>300||S>15)return;if(g)_();else if(h.length===0){const P=a.elementToImage(p.offsetX,p.offsetY);h.push(P),n(1,c=P)}else h.push(c)}else{if(h.length===1&&Ve(h[0],c)<=4){n(0,h=[]),n(1,c=null);return}p.stopImmediatePropagation(),g?_():h.push(c)}},b=()=>{const p=[...h,c],A={type:H.POLYGON,geometry:{bounds:Be(p),points:p}};Xe(A)>4&&(n(0,h=[]),n(1,c=null),i("create",A))},_=()=>{const p={type:H.POLYGON,geometry:{bounds:Be(h),points:[...h]}};n(0,h=[]),n(1,c=null),i("create",p)};return Ie(()=>{s("pointerdown",u,!0),s("pointermove",m),s("pointerup",w,!0),s("dblclick",b,!0)}),e.$$set=p=>{"addEventListener"in p&&n(4,s=p.addEventListener),"drawingMode"in p&&n(5,r=p.drawingMode),"transform"in p&&n(6,a=p.transform),"viewportScale"in p&&n(7,l=p.viewportScale)},e.$$.update=()=>{e.$$.dirty&128&&n(3,o=10/l)},[h,c,g,o,s,r,a,l]}class Qo extends ne{constructor(t){super(),te(this,t,Zo,Ko,Z,{addEventListener:4,drawingMode:5,transform:6,viewportScale:7})}}const ct=new Map([["rectangle",{tool:en}],["polygon",{tool:Qo}]]),ze=()=>[...ct.keys()],dt=e=>ct.get(e),sn=(e,t,n)=>ct.set(e,{tool:t,opts:n});function xo(e){let t,n,o,i,s;return{c(){t=Y("g"),n=Y("ellipse"),i=Y("ellipse"),f(n,"class","a9s-outer"),f(n,"style",o=e[1]?"display:none;":void 0),f(n,"cx",e[2]),f(n,"cy",e[3]),f(n,"rx",e[4]),f(n,"ry",e[5]),f(i,"class","a9s-inner"),f(i,"style",e[1]),f(i,"cx",e[2]),f(i,"cy",e[3]),f(i,"rx",e[4]),f(i,"ry",e[5]),f(t,"data-id",s=e[0].id)},m(r,a){k(r,t,a),le(t,n),le(t,i)},p(r,[a]){a&2&&o!==(o=r[1]?"display:none;":void 0)&&f(n,"style",o),a&2&&f(i,"style",r[1]),a&1&&s!==(s=r[0].id)&&f(t,"data-id",s)},i:V,o:V,d(r){r&&B(t)}}}function $o(e,t,n){let o,{annotation:i}=t,{geom:s}=t,{style:r=void 0}=t;const{cx:a,cy:l,rx:d,ry:h}=s;return e.$$set=c=>{"annotation"in c&&n(0,i=c.annotation),"geom"in c&&n(6,s=c.geom),"style"in c&&n(7,r=c.style)},e.$$.update=()=>{e.$$.dirty&129&&n(1,o=Ge(i,r))},[i,o,a,l,d,h,s,r]}class ei extends ne{constructor(t){super(),te(this,t,$o,xo,Z,{annotation:0,geom:6,style:7})}}function ti(e){let t,n,o,i,s;return{c(){t=Y("g"),n=Y("polygon"),i=Y("polygon"),f(n,"class","a9s-outer"),f(n,"style",o=e[1]?"display:none;":void 0),f(n,"points",e[2].map(ni).join(" ")),f(i,"class","a9s-inner"),f(i,"style",e[1]),f(i,"points",e[2].map(oi).join(" ")),f(t,"data-id",s=e[0].id)},m(r,a){k(r,t,a),le(t,n),le(t,i)},p(r,[a]){a&2&&o!==(o=r[1]?"display:none;":void 0)&&f(n,"style",o),a&2&&f(i,"style",r[1]),a&1&&s!==(s=r[0].id)&&f(t,"data-id",s)},i:V,o:V,d(r){r&&B(t)}}}const ni=e=>e.join(","),oi=e=>e.join(",");function ii(e,t,n){let o,{annotation:i}=t,{geom:s}=t,{style:r=void 0}=t;const{points:a}=s;return e.$$set=l=>{"annotation"in l&&n(0,i=l.annotation),"geom"in l&&n(3,s=l.geom),"style"in l&&n(4,r=l.style)},e.$$.update=()=>{e.$$.dirty&17&&n(1,o=Ge(i,r))},[i,o,a,s,r]}class si extends ne{constructor(t){super(),te(this,t,ii,ti,Z,{annotation:0,geom:3,style:4})}}function ri(e){let t,n,o,i,s;return{c(){t=Y("g"),n=Y("rect"),i=Y("rect"),f(n,"class","a9s-outer"),f(n,"style",o=e[5]?"display:none;":void 0),f(n,"x",e[4]),f(n,"y",e[3]),f(n,"width",e[2]),f(n,"height",e[1]),f(i,"class","a9s-inner"),f(i,"style",e[5]),f(i,"x",e[4]),f(i,"y",e[3]),f(i,"width",e[2]),f(i,"height",e[1]),f(t,"data-id",s=e[0].id)},m(r,a){k(r,t,a),le(t,n),le(t,i)},p(r,[a]){a&32&&o!==(o=r[5]?"display:none;":void 0)&&f(n,"style",o),a&16&&f(n,"x",r[4]),a&8&&f(n,"y",r[3]),a&4&&f(n,"width",r[2]),a&2&&f(n,"height",r[1]),a&32&&f(i,"style",r[5]),a&16&&f(i,"x",r[4]),a&8&&f(i,"y",r[3]),a&4&&f(i,"width",r[2]),a&2&&f(i,"height",r[1]),a&1&&s!==(s=r[0].id)&&f(t,"data-id",s)},i:V,o:V,d(r){r&&B(t)}}}function li(e,t,n){let o,i,s,r,a,{annotation:l}=t,{geom:d}=t,{style:h=void 0}=t;return e.$$set=c=>{"annotation"in c&&n(0,l=c.annotation),"geom"in c&&n(6,d=c.geom),"style"in c&&n(7,h=c.style)},e.$$.update=()=>{e.$$.dirty&129&&n(5,o=Ge(l,h)),e.$$.dirty&64&&n(4,{x:i,y:s,w:r,h:a}=d,i,(n(3,s),n(6,d)),(n(2,r),n(6,d)),(n(1,a),n(6,d)))},[l,a,r,s,i,o,d,h]}class ai extends ne{constructor(t){super(),te(this,t,li,ri,Z,{annotation:0,geom:6,style:7})}}const ci={elementToImage:(e,t)=>[e,t]},rn=e=>({elementToImage:(t,n)=>{const o=e.getBoundingClientRect(),i=e.createSVGPoint();i.x=t+o.x,i.y=n+o.y;const{x:s,y:r}=i.matrixTransform(e.getScreenCTM().inverse());return[s,r]}}),di=250,ln=(e,t)=>{const n=me();let o;return{onPointerDown:()=>o=performance.now(),onPointerUp:r=>{if(performance.now()-o<di){const{x:l,y:d}=ui(r,e),h=t.getAt(l,d);h?n("click",{originalEvent:r,annotation:h}):n("click",{originalEvent:r})}}}},ui=(e,t)=>{const n=t.createSVGPoint(),o=t.getBoundingClientRect(),i=e.clientX-o.x,s=e.clientY-o.y,{left:r,top:a}=t.getBoundingClientRect();return n.x=i+r,n.y=s+a,n.matrixTransform(t.getScreenCTM().inverse())};function an(e,t,n){const o=e.slice();return o[29]=t[n],o}function cn(e,t,n){const o=e.slice();return o[32]=t[n],o}function ut(e){const t=e.slice(),n=t[32].target.selector;return t[35]=n,t}function dn(e){let t=e[32].id,n,o,i=un(e);return{c(){i.c(),n=ie()},m(s,r){i.m(s,r),k(s,n,r),o=!0},p(s,r){r[0]&8192&&Z(t,t=s[32].id)?(se(),G(i,1,1,V),re(),i=un(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){G(i),o=!1},d(s){s&&B(n),i.d(s)}}}function fi(e){let t,n;return t=new si({props:{annotation:e[32],geom:e[35].geometry,style:e[1]}}),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,i){const s={};i[0]&8192&&(s.annotation=o[32]),i[0]&8192&&(s.geom=o[35].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function hi(e){let t,n;return t=new ai({props:{annotation:e[32],geom:e[35].geometry,style:e[1]}}),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,i){const s={};i[0]&8192&&(s.annotation=o[32]),i[0]&8192&&(s.geom=o[35].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function gi(e){let t,n;return t=new ei({props:{annotation:e[32],geom:e[35].geometry,style:e[1]}}),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,i){const s={};i[0]&8192&&(s.annotation=o[32]),i[0]&8192&&(s.geom=o[35].geometry),i[0]&2&&(s.style=o[1]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function un(e){let t,n,o,i;const s=[gi,hi,fi],r=[];function a(l,d){return l[35].type===H.ELLIPSE?0:l[35].type===H.RECTANGLE?1:l[35].type===H.POLYGON?2:-1}return~(t=a(e))&&(n=r[t]=s[t](e)),{c(){n&&n.c(),o=ie()},m(l,d){~t&&r[t].m(l,d),k(l,o,d),i=!0},p(l,d){let h=t;t=a(l),t===h?~t&&r[t].p(l,d):(n&&(se(),G(r[h],1,1,()=>{r[h]=null}),re()),~t?(n=r[t],n?n.p(l,d):(n=r[t]=s[t](l),n.c()),X(n,1),n.m(o.parentNode,o)):n=null)},i(l){i||(X(n),i=!0)},o(l){G(n),i=!1},d(l){~t&&r[t].d(l),l&&B(o)}}}function fn(e){let t=!e[7](e[32]),n,o,i=t&&dn(ut(e));return{c(){i&&i.c(),n=ie()},m(s,r){i&&i.m(s,r),k(s,n,r),o=!0},p(s,r){r[0]&8320&&(t=!s[7](s[32])),t?i?(i.p(ut(s),r),r[0]&8320&&X(i,1)):(i=dn(ut(s)),i.c(),X(i,1),i.m(n.parentNode,n)):i&&(se(),G(i,1,1,()=>{i=null}),re())},i(s){o||(X(i),o=!0)},o(s){G(i),o=!1},d(s){i&&i.d(s),s&&B(n)}}}function hn(e){let t,n,o,i;const s=[pi,mi],r=[];function a(l,d){return l[6]?0:l[12]&&l[0]?1:-1}return~(t=a(e))&&(n=r[t]=s[t](e)),{c(){n&&n.c(),o=ie()},m(l,d){~t&&r[t].m(l,d),k(l,o,d),i=!0},p(l,d){let h=t;t=a(l),t===h?~t&&r[t].p(l,d):(n&&(se(),G(r[h],1,1,()=>{r[h]=null}),re()),~t?(n=r[t],n?n.p(l,d):(n=r[t]=s[t](l),n.c()),X(n,1),n.m(o.parentNode,o)):n=null)},i(l){i||(X(n),i=!0)},o(l){G(n),i=!1},d(l){~t&&r[t].d(l),l&&B(o)}}}function mi(e){let t=e[2],n,o,i=gn(e);return{c(){i.c(),n=ie()},m(s,r){i.m(s,r),k(s,n,r),o=!0},p(s,r){r[0]&4&&Z(t,t=s[2])?(se(),G(i,1,1,V),re(),i=gn(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){G(i),o=!1},d(s){s&&B(n),i.d(s)}}}function pi(e){let t,n,o=e[6],i=[];for(let r=0;r<o.length;r+=1)i[r]=pn(an(e,o,r));const s=r=>G(i[r],1,1,()=>{i[r]=null});return{c(){for(let r=0;r<i.length;r+=1)i[r].c();t=ie()},m(r,a){for(let l=0;l<i.length;l+=1)i[l]&&i[l].m(r,a);k(r,t,a),n=!0},p(r,a){if(a[0]&279634){o=r[6];let l;for(l=0;l<o.length;l+=1){const d=an(r,o,l);i[l]?(i[l].p(d,a),X(i[l],1)):(i[l]=pn(d),i[l].c(),X(i[l],1),i[l].m(t.parentNode,t))}for(se(),l=o.length;l<i.length;l+=1)s(l);re()}},i(r){if(!n){for(let a=0;a<o.length;a+=1)X(i[a]);n=!0}},o(r){i=i.filter(Boolean);for(let a=0;a<i.length;a+=1)G(i[a]);n=!1},d(r){Qe(i,r),r&&B(t)}}}function gn(e){let t,n;return t=new xt({props:{target:e[4],tool:e[12],drawingMode:e[11],transform:e[10],viewportScale:e[14]}}),t.$on("create",e[17]),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,i){const s={};i[0]&16&&(s.target=o[4]),i[0]&4096&&(s.tool=o[12]),i[0]&2048&&(s.drawingMode=o[11]),i[0]&1024&&(s.transform=o[10]),i[0]&16384&&(s.viewportScale=o[14]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function mn(e){let t,n;return t=new Qt({props:{target:e[4],editor:rt(e[29].target.selector),annotation:e[29],style:e[1],transform:e[10],viewportScale:e[14]}}),t.$on("change",function(){W(e[18](e[29]))&&e[18](e[29]).apply(this,arguments)}),{c(){ue(t.$$.fragment)},m(o,i){ae(t,o,i),n=!0},p(o,i){e=o;const s={};i[0]&16&&(s.target=e[4]),i[0]&64&&(s.editor=rt(e[29].target.selector)),i[0]&64&&(s.annotation=e[29]),i[0]&2&&(s.style=e[1]),i[0]&1024&&(s.transform=e[10]),i[0]&16384&&(s.viewportScale=e[14]),t.$set(s)},i(o){n||(X(t.$$.fragment,o),n=!0)},o(o){G(t.$$.fragment,o),n=!1},d(o){ce(t,o)}}}function pn(e){let t=e[29].id,n,o,i=mn(e);return{c(){i.c(),n=ie()},m(s,r){i.m(s,r),k(s,n,r),o=!0},p(s,r){r[0]&64&&Z(t,t=s[29].id)?(se(),G(i,1,1,V),re(),i=mn(s),i.c(),X(i,1),i.m(n.parentNode,n)):i.p(s,r)},i(s){o||(X(i),o=!0)},o(s){G(i),o=!1},d(s){s&&B(n),i.d(s)}}}function yi(e){let t,n,o,i,s,r,a=e[13],l=[];for(let c=0;c<a.length;c+=1)l[c]=fn(cn(e,a,c));const d=c=>G(l[c],1,1,()=>{l[c]=null});let h=e[4]&&hn(e);return{c(){t=Y("svg"),n=Y("g");for(let c=0;c<l.length;c+=1)l[c].c();o=Y("g"),h&&h.c(),f(o,"class","drawing"),f(t,"class","a9s-annotationlayer"),St(t,"drawing",e[12])},m(c,g){k(c,t,g),le(t,n);for(let u=0;u<l.length;u+=1)l[u]&&l[u].m(n,null);le(t,o),h&&h.m(o,null),e[25](o),e[26](t),i=!0,s||(r=[q(t,"pointerup",function(){W(e[8])&&e[8].apply(this,arguments)}),q(t,"pointerdown",function(){W(e[9])&&e[9].apply(this,arguments)})],s=!0)},p(c,g){if(e=c,g[0]&8322){a=e[13];let u;for(u=0;u<a.length;u+=1){const m=cn(e,a,u);l[u]?(l[u].p(m,g),X(l[u],1)):(l[u]=fn(m),l[u].c(),X(l[u],1),l[u].m(n,null))}for(se(),u=a.length;u<l.length;u+=1)d(u);re()}e[4]?h?(h.p(e,g),g[0]&16&&X(h,1)):(h=hn(e),h.c(),X(h,1),h.m(o,null)):h&&(se(),G(h,1,1,()=>{h=null}),re()),(!i||g[0]&4096)&&St(t,"drawing",e[12])},i(c){if(!i){for(let g=0;g<a.length;g+=1)X(l[g]);X(h),i=!0}},o(c){l=l.filter(Boolean);for(let g=0;g<l.length;g+=1)G(l[g]);G(h),i=!1},d(c){c&&B(t),Qe(l,c),h&&h.d(),e[25](null),e[26](null),s=!1,oe(r)}}}function _i(e,t,n){let o,i,s,r,a,l,d,h,c,g,u=V,m=()=>(u(),u=bt(y,I=>n(14,g=I)),y);e.$$.on_destroy.push(()=>u());let{drawingEnabled:w}=t,{image:b}=t,{preferredDrawingMode:_}=t,{state:p}=t,{style:A=void 0}=t,{toolName:S=ze().length>0?ze()[0]:void 0}=t,{user:P}=t,U,N,y;Ie(()=>m(n(5,y=tn(b,N))));const{selection:T,store:E}=p;Et(e,T,I=>n(24,h=I)),Et(e,E,I=>n(13,c=I));let O=null,C=null;const D=I=>{E.unobserve(O);const J=I.filter(({editable:F})=>F).map(({id:F})=>F);J.length>0?(n(6,C=J.map(F=>E.getAnnotation(F))),O=F=>{const{updated:he}=F.changes;n(6,C=he.map(j=>j.newValue))},E.observe(O,{annotations:J})):n(6,C=null)},Q=I=>{const J=nt(),F={id:J,bodies:[],target:{annotation:J,selector:I.detail,creator:P,created:new Date}};E.addAnnotation(F),T.setSelected(F.id)},ve=I=>J=>{var ge;const{target:F}=I,he=10*60*1e3,j=((ge=F.creator)==null?void 0:ge.id)!==P.id||!F.created||new Date().getTime()-F.created.getTime()>he;E.updateTarget({...F,selector:J.detail,created:j?F.created:new Date,updated:j?new Date:null,updatedBy:j?P:null})};function Me(I){Ue[I?"unshift":"push"](()=>{U=I,n(4,U)})}function Re(I){Ue[I?"unshift":"push"](()=>{N=I,n(3,N)})}return e.$$set=I=>{"drawingEnabled"in I&&n(0,w=I.drawingEnabled),"image"in I&&n(19,b=I.image),"preferredDrawingMode"in I&&n(20,_=I.preferredDrawingMode),"state"in I&&n(21,p=I.state),"style"in I&&n(1,A=I.style),"toolName"in I&&n(2,S=I.toolName),"user"in I&&n(22,P=I.user)},e.$$.update=()=>{e.$$.dirty[0]&4&&n(12,{tool:o,opts:i}=dt(S),o,(n(23,i),n(2,S))),e.$$.dirty[0]&9437184&&n(11,s=(i==null?void 0:i.drawingMode)||_),e.$$.dirty[0]&8&&n(10,r=rn(N)),e.$$.dirty[0]&8&&n(9,{onPointerDown:a,onPointerUp:l}=ln(N,E),a,(n(8,l),n(3,N))),e.$$.dirty[0]&16777216&&n(7,d=I=>h.selected.find(J=>J.id===I.id&&J.editable)),e.$$.dirty[0]&16777216&&D(h.selected)},[w,A,S,N,U,y,C,d,l,a,r,s,o,c,g,T,E,Q,ve,b,_,p,P,i,h,Me,Re]}class yn extends ne{constructor(t){super(),te(this,t,_i,yi,Z,{drawingEnabled:0,image:19,preferredDrawingMode:20,state:21,style:1,toolName:2,user:22},null,[-1,-1])}}function wi(e,t,n,o,i){_n(e,t,n||0,o||e.length-1,i||bi)}function _n(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,r=t-n+1,a=Math.log(s),l=.5*Math.exp(2*a/3),d=.5*Math.sqrt(a*l*(s-l)/s)*(r-s/2<0?-1:1),h=Math.max(n,Math.floor(t-r*l/s+d)),c=Math.min(o,Math.floor(t+(s-r)*l/s+d));_n(e,t,h,c,i)}var g=e[t],u=n,m=o;for(ke(e,n,t),i(e[o],g)>0&&ke(e,n,o);u<m;){for(ke(e,u,m),u++,m--;i(e[u],g)<0;)u++;for(;i(e[m],g)>0;)m--}i(e[n],g)===0?ke(e,n,m):(m++,ke(e,m,o)),m<=t&&(n=m+1),t<=m&&(o=m-1)}}function ke(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function bi(e,t){return e<t?-1:e>t?1:0}class Ei{constructor(t=9){this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(t){let n=this.data;const o=[];if(!He(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let r=0;r<n.children.length;r++){const a=n.children[r],l=n.leaf?i(a):a;He(t,l)&&(n.leaf?o.push(a):ht(t,l)?this._all(a,o):s.push(a))}n=s.pop()}return o}collides(t){let n=this.data;if(!He(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],r=n.leaf?this.toBBox(s):s;if(He(t,r)){if(n.leaf||ht(t,r))return!0;o.push(s)}}n=o.pop()}return!1}load(t){if(!(t&&t.length))return this;if(t.length<this._minEntries){for(let o=0;o<t.length;o++)this.insert(t[o]);return this}let n=this._build(t.slice(),0,t.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(t){return t&&this._insert(t,this.data.height-1),this}clear(){return this.data=Te([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],r=[];let a,l,d;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],a=r.pop(),d=!0),o.leaf){const h=Ai(t,o.children,n);if(h!==-1)return o.children.splice(h,1),s.push(o),this._condense(s),this}!d&&!o.leaf&&ht(o,i)?(s.push(o),r.push(a),a=0,l=o,o=o.children[0]):l?(a++,o=l.children[a],d=!1):o=null}return this}toBBox(t){return t}compareMinX(t,n){return t.minX-n.minX}compareMinY(t,n){return t.minY-n.minY}toJSON(){return this.data}fromJSON(t){return this.data=t,this}_all(t,n){const o=[];for(;t;)t.leaf?n.push(...t.children):o.push(...t.children),t=o.pop();return n}_build(t,n,o,i){const s=o-n+1;let r=this._maxEntries,a;if(s<=r)return a=Te(t.slice(n,o+1)),Ae(a,this.toBBox),a;i||(i=Math.ceil(Math.log(s)/Math.log(r)),r=Math.ceil(s/Math.pow(r,i-1))),a=Te([]),a.leaf=!1,a.height=i;const l=Math.ceil(s/r),d=l*Math.ceil(Math.sqrt(r));wn(t,n,o,d,this.compareMinX);for(let h=n;h<=o;h+=d){const c=Math.min(h+d-1,o);wn(t,h,c,l,this.compareMinY);for(let g=h;g<=c;g+=l){const u=Math.min(g+l-1,c);a.children.push(this._build(t,g,u,i-1))}}return Ae(a,this.toBBox),a}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,r=1/0,a;for(let l=0;l<n.children.length;l++){const d=n.children[l],h=ft(d),c=vi(t,d)-h;c<r?(r=c,s=h<s?h:s,a=d):c===r&&h<s&&(s=h,a=d)}n=a||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],r=this._chooseSubtree(i,this.data,n,s);for(r.children.push(t),Pe(r,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(t,n){const o=t[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const r=this._chooseSplitIndex(o,s,i),a=Te(o.children.splice(r,o.children.length-r));a.height=o.height,a.leaf=o.leaf,Ae(o,this.toBBox),Ae(a,this.toBBox),n?t[n-1].children.push(a):this._splitRoot(o,a)}_splitRoot(t,n){this.data=Te([t,n]),this.data.height=t.height+1,this.data.leaf=!1,Ae(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,r=1/0;for(let a=n;a<=o-n;a++){const l=Ce(t,0,a,this.toBBox),d=Ce(t,a,o,this.toBBox),h=Mi(l,d),c=ft(l)+ft(d);h<s?(s=h,i=a,r=c<r?c:r):h===s&&c<r&&(r=c,i=a)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:Ti,s=t.leaf?this.compareMinY:Si,r=this._allDistMargin(t,n,o,i),a=this._allDistMargin(t,n,o,s);r<a&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,r=Ce(t,0,n,s),a=Ce(t,o-n,o,s);let l=Fe(r)+Fe(a);for(let d=n;d<o-n;d++){const h=t.children[d];Pe(r,t.leaf?s(h):h),l+=Fe(r)}for(let d=o-n-1;d>=n;d--){const h=t.children[d];Pe(a,t.leaf?s(h):h),l+=Fe(a)}return l}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)Pe(n[i],t)}_condense(t){for(let n=t.length-1,o;n>=0;n--)t[n].children.length===0?n>0?(o=t[n-1].children,o.splice(o.indexOf(t[n]),1)):this.clear():Ae(t[n],this.toBBox)}}function Ai(e,t,n){if(!n)return t.indexOf(e);for(let o=0;o<t.length;o++)if(n(e,t[o]))return o;return-1}function Ae(e,t){Ce(e,0,e.children.length,t,e)}function Ce(e,t,n,o,i){i||(i=Te(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=t;s<n;s++){const r=e.children[s];Pe(i,e.leaf?o(r):r)}return i}function Pe(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e}function Ti(e,t){return e.minX-t.minX}function Si(e,t){return e.minY-t.minY}function ft(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function Fe(e){return e.maxX-e.minX+(e.maxY-e.minY)}function vi(e,t){return(Math.max(t.maxX,e.maxX)-Math.min(t.minX,e.minX))*(Math.max(t.maxY,e.maxY)-Math.min(t.minY,e.minY))}function Mi(e,t){const n=Math.max(e.minX,t.minX),o=Math.max(e.minY,t.minY),i=Math.min(e.maxX,t.maxX),s=Math.min(e.maxY,t.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function ht(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function He(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function Te(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function wn(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const r=t+Math.ceil((n-t)/o/2)*o;wi(e,r,t,n,i),s.push(t,r,r,n)}}const Oi=()=>{const e=new Ei,t=new Map,n=()=>[...t.values()],o=()=>{e.clear(),t.clear()},i=c=>{const{minX:g,minY:u,maxX:m,maxY:w}=c.selector.geometry.bounds,b={minX:g,minY:u,maxX:m,maxY:w,target:c};e.insert(b),t.set(c.annotation,b)},s=c=>{const g=t.get(c.annotation);e.remove(g),t.delete(c.annotation)};return{all:n,clear:o,getAt:(c,g)=>{const m=e.search({minX:c,minY:g,maxX:c,maxY:g}).map(w=>w.target).filter(w=>w.selector.type===H.RECTANGLE||Lt(w.selector,c,g));if(m.length>0)return m.sort((w,b)=>Xe(w.selector)-Xe(b.selector)),m[0]},getIntersecting:(c,g,u,m)=>e.search({minX:c,minY:g,maxX:c+u,maxY:g+m}).map(w=>w.target),insert:i,remove:s,set:(c,g=!0)=>{g&&o();const u=c.map(m=>{const{minX:w,minY:b,maxX:_,maxY:p}=m.selector.geometry.bounds;return{minX:w,minY:b,maxX:_,maxY:p,target:m}});u.forEach(m=>t.set(m.target.annotation,m)),e.load(u)},size:()=>e.all().length,update:(c,g)=>{s(c),i(g)}}},bn=e=>{const t=go(),n=Oi(),o=oo(t,e.pointerSelectAction),i=no(t),s=wo();return t.observe(({changes:l})=>{n.set(l.created.map(d=>d.target),!1),l.deleted.forEach(d=>n.remove(d.target)),l.updated.forEach(({oldValue:d,newValue:h})=>n.update(d.target,h.target))}),{store:{...t,getAt:(l,d)=>{const h=n.getAt(l,d);return h?t.getAnnotation(h.annotation):void 0},getIntersecting:(l,d,h,c)=>n.getIntersecting(l,d,h,c).map(g=>t.getAnnotation(g.annotation))},selection:o,hover:i,viewport:s}},En=e=>{const t=bn(e);return{...t,store:mo(t.store)}},An=e=>{let t,n;if(e.nodeName==="CANVAS")t=e,n=t.getContext("2d",{willReadFrequently:!0});else{const i=e;t=document.createElement("canvas"),t.width=i.width,t.height=i.height,n=t.getContext("2d",{willReadFrequently:!0}),n.drawImage(i,0,0,i.width,i.height)}let o=0;for(let i=1;i<10;i++)for(let s=1;s<10;s++){const r=Math.round(s*t.width/10),a=Math.round(i*t.height/10),l=n.getImageData(r,a,1,1).data,d=(.299*l[0]+.587*l[1]+.114*l[2])/255;o+=d}return o/81},Tn=e=>{const t=An(e),n=t>.6?"dark":"light";return console.log(`[Annotorious] Image brightness: ${t.toFixed(1)}. Setting ${n} theme.`),n},gt=(e,t,n)=>t.setAttribute("data-theme",n==="auto"?Tn(e):n),Sn=e=>({...e,drawingEnabled:e.drawingEnabled===void 0?!0:e.drawingEnabled,drawingMode:e.drawingMode||"drag",pointerSelectAction:e.pointerSelectAction||Xt.EDIT,theme:e.theme||"light"}),vn=navigator.userAgent.indexOf("Mac OS X")!==-1,Mn=(e,t)=>{const n=t||document,o=r=>{r.key==="Z"&&r.ctrlKey?e.undo():r.key==="Y"&&r.ctrlKey&&e.redo()},i=r=>{r.key==="z"&&r.metaKey&&(r.shiftKey?e.redo():e.undo())},s=()=>{vn?n.removeEventListener("keydown",i):n.removeEventListener("keydown",o)};return vn?n.addEventListener("keydown",i):n.addEventListener("keydown",o),{destroy:s}},xi="",$i="",es="",Li=(e,t={})=>{if(!e)throw"Missing argument: image";const n=typeof e=="string"?document.getElementById(e):e,o=Sn(t),i=En(o),{selection:s,store:r}=i,a=_o(r),l=bo(i,a,o.adapter,o.autoSave),d=document.createElement("DIV");d.style.position="relative",d.style.display="inline-block",n.style.display="block",n.parentNode.insertBefore(d,n),d.appendChild(n);const h=Mn(a);let c=Oo();gt(n,d,o.theme);const g=new yn({target:d,props:{drawingEnabled:o.drawingEnabled,image:n,preferredDrawingMode:o.drawingMode,state:i,style:o.style,user:c}});g.$on("click",y=>{const{originalEvent:T,annotation:E}=y.detail;E?s.clickSelect(E.id,T):s.isEmpty()||s.clear()});const u=Ao(i,a,o.adapter),m=()=>{g.$destroy(),d.parentNode.insertBefore(n,d),d.parentNode.removeChild(d),h.destroy(),a.destroy()},w=()=>c,b=(y,T,E)=>sn(y,T,E),_=(y,T)=>Jt(y,T),p=y=>{if(!dt(y))throw`No drawing tool named ${y}`;g.$set({toolName:y})},A=y=>g.$set({drawingEnabled:y}),S=y=>{console.warn("Filter not implemented yet")},P=y=>g.$set({style:y}),U=y=>gt(n,d,y),N=y=>{c=y,g.$set({user:y})};return{...u,destroy:m,getUser:w,listDrawingTools:ze,on:l.on,off:l.off,registerDrawingTool:b,registerShapeEditor:_,setDrawingEnabled:A,setDrawingTool:p,setFilter:S,setStyle:P,setTheme:U,setUser:N,state:i}};var On=(e=>(e.EDIT="EDIT",e.SELECT="SELECT",e.NONE="NONE",e))(On||{});const Ii=e=>{const{creator:t,updatedBy:n}=e.target,o=e.bodies.reduce((i,s)=>[...i,s.creator,s.updatedBy],[]);return[t,n,...o].filter(i=>i)},Bi=(e,t,n,o)=>({id:nt(),annotation:e.id,created:n||new Date,creator:o,...t});var Ln=Object.prototype.hasOwnProperty;function je(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&je(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(Ln.call(e,n)&&++o&&!Ln.call(t,n)||!(n in t)||!je(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}const ki=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},Ci=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Pi=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!je(o,n)?o:void 0}}).filter(({oldBody:n})=>n),Ri=(e,t)=>!je(e.target,t.target),Ui=(e,t)=>{const n=ki(e,t),o=Ci(e,t),i=Pi(e,t);return{oldValue:e,newValue:t,bodiesCreated:n.length>0?n:void 0,bodiesDeleted:o.length>0?o:void 0,bodiesUpdated:i.length>0?i:void 0,targetUpdated:Ri(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var Se=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(Se||{});let Di=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Yi=e=>t=>t.map(n=>e.serialize(n)),In=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),Xi=(e,t,n)=>{const{store:o,selection:i}=e,s=b=>{if(n){const{parsed:_,error:p}=n.parse(b);_?o.addAnnotation(_,Se.REMOTE):console.error(p)}else o.addAnnotation(b,Se.REMOTE)},r=()=>i.clear(),a=()=>o.clear(),l=b=>{const _=o.getAnnotation(b);return n&&_?n.serialize(_):_},d=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var p;const _=(((p=i.selected)==null?void 0:p.map(A=>A.id))||[]).map(A=>o.getAnnotation(A));return n?_.map(n.serialize):_},c=b=>fetch(b).then(_=>_.json()).then(_=>(u(_),_)),g=b=>{if(typeof b=="string"){const _=o.getAnnotation(b);return o.deleteAnnotation(b),n?n.serialize(_):_}else{const _=n?n.parse(b).parsed:b;return o.deleteAnnotation(_),b}},u=b=>{if(n){const{parsed:_,failed:p}=In(n)(b);p.length>0&&console.warn(`Discarded ${p.length} invalid annotations`,p),o.bulkAddAnnotation(_,!0,Se.REMOTE)}else o.bulkAddAnnotation(b,!0,Se.REMOTE)},m=b=>{b?i.setSelected(b):i.clear()},w=b=>{if(n){const _=n.parse(b).parsed,p=n.serialize(o.getAnnotation(_.id));return o.updateAnnotation(_),p}else{const _=o.getAnnotation(b.id);return o.updateAnnotation(b),_}};return{addAnnotation:s,cancelSelected:r,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:a,getAnnotationById:l,getAnnotations:d,getSelected:h,loadAnnotations:c,redo:t.redo,removeAnnotation:g,setAnnotations:u,setSelected:m,undo:t.undo,updateAnnotation:w}};let Ni=e=>crypto.getRandomValues(new Uint8Array(e)),Gi=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let r="";for(;;){let a=n(i),l=i;for(;l--;)if(r+=e[a[l]&o]||"",r.length===s)return r}}},Vi=(e,t=21)=>Gi(e,t,Ni),zi=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Fi=()=>({isGuest:!0,id:Vi("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Hi=e=>{const t=JSON.stringify(e);let n=0;for(let o=0,i=t.length;o<i;o++){let s=t.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},ji=(e,t)=>(Array.isArray(e)?e:[e]).map(n=>{const{id:o,type:i,purpose:s,value:r,created:a,creator:l,...d}=n;return{id:o||`temp-${Hi(n)}`,annotation:t,type:i,purpose:s,value:r,created:a,creator:l?typeof l=="object"?{...l}:l:void 0,...d}}),Wi=e=>e.map(t=>{var o;const n={...t};return delete n.annotation,(o=n.id)!=null&&o.startsWith("temp-")&&delete n.id,n}),qi=["#ff7c00","#1ac938","#e8000b","#8b2be2","#9f4800","#f14cc1","#ffc400","#00d7ff","#023eff"],Bn=()=>{const e=[...qi];return{assignRandomColor:()=>{const o=Math.floor(Math.random()*e.length),i=e[o];return e.splice(o,1),i},releaseColor:o=>e.push(o)}},kn=()=>{const e=Bn();return{addUser:(o,i)=>{const s=e.assignRandomColor();return{label:i.name||i.id,avatar:i.avatar,color:s}},removeUser:o=>e.releaseColor(o.appearance.color)}},Ki=(e,t)=>e.every(n=>e.includes(n))&&t.every(n=>e.includes(n)),Ji=zi(),Zi=(e=kn())=>{const t=Di(),n=new Map,o=new Map,i=(c,g)=>{if(n.has(c)){console.warn("Attempt to add user that is already present",c,g);return}const u=e.addUser(c,g);n.set(c,{...g,presenceKey:c,appearance:u})},s=c=>{const g=n.get(c);if(!g){console.warn("Attempt to remove user that is not present",c);return}e.removeUser(g),n.delete(c)},r=c=>{const g=new Set(c.map(w=>w.presenceKey)),u=c.filter(({presenceKey:w})=>!n.has(w)),m=Array.from(n.values()).filter(w=>!g.has(w.presenceKey));u.forEach(({presenceKey:w,user:b})=>i(w,b)),m.forEach(w=>{const{presenceKey:b}=w;o.has(b)&&t.emit("selectionChange",w,null),s(b)}),(u.length>0||m.length>0)&&t.emit("presence",d())},a=(c,g)=>{const u=n.get(c);if(!u){console.warn("Activity notification from user that is not present");return}const m=o.get(c);(!m||!Ki(m,g))&&(o.set(c,g),t.emit("selectionChange",u,g))},l=(c,g)=>{const u=n.get(c);if(!u){console.warn("Selection change for user that is not present",c);return}g?o.set(c,g):o.delete(c),t.emit("selectionChange",u,g)},d=()=>[...Array.from(n.values())];return{getPresentUsers:d,notifyActivity:a,on:(c,g)=>t.on(c,g),syncUsers:r,updateSelection:l}};v.Editor=lt,v.EditorMount=Qt,v.Handle=L,v.IdentityTransform=ci,v.Origin=Se,v.PRESENCE_KEY=Ji,v.PointerSelectAction=On,v.PolygonEditor=Wt,v.RectangleEditor=qt,v.RectangleUtil=It,v.RubberbandRectangle=en,v.SVGAnnotationLayer=yn,v.ShapeType=H,v.ToolMount=xt,v.W3CImageFormat=ko,v.addEventListeners=ln,v.boundsFromPoints=Be,v.computeArea=Xe,v.createAnonymousGuest=Fi,v.createBaseAnnotator=Xi,v.createBody=Bi,v.createDefaultAppearenceProvider=kn,v.createImageAnnotator=Li,v.createImageAnnotatorState=bn,v.createPresenceState=Zi,v.createSVGTransform=rn,v.createSvelteImageAnnotatorState=En,v.defaultColorProvider=Bn,v.detectTheme=Tn,v.diffAnnotations=Ui,v.distance=Ve,v.enableResponsive=tn,v.fillDefaults=Sn,v.getContributors=Ii,v.getEditor=rt,v.getTool=dt,v.initKeyboardCommands=Mn,v.intersects=Lt,v.isTouch=qo,v.listDrawingTools=ze,v.parseAll=In,v.parseFragmentSelector=Bt,v.parseSVGSelector=Rt,v.parseW3CBodies=ji,v.parseW3CImageAnnotation=Gt,v.registerEditor=Jt,v.registerShapeUtil=Ye,v.registerTool=sn,v.sampleBrightness=An,v.serializeAll=Yi,v.serializeFragmentSelector=kt,v.serializeSVGSelector=Ut,v.serializeW3CBodies=Wi,v.serializeW3CImageAnnotation=Vt,v.setTheme=gt,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=annotorious.js.map