react-iiif-vault 2.0.8 → 2.0.9

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.
package/dist/bundle.d.ts CHANGED
@@ -388,13 +388,22 @@ interface AtlasStore {
388
388
  cancelRequest(requestId?: string): void;
389
389
  reset(): void;
390
390
  changeMode(mode: ViewerMode): void;
391
- nudgeLeft(): void;
392
- nudgeRight(): void;
393
- nudgeUp(): void;
394
- nudgeDown(): void;
391
+ nudgeLeft(amount?: number): void;
392
+ nudgeRight(amount?: number): void;
393
+ nudgeUp(amount?: number): void;
394
+ nudgeDown(amount?: number): void;
395
395
  zoomIn(): void;
396
396
  zoomOut(): void;
397
397
  goHome(): void;
398
+ goToRegion(region: {
399
+ x: number;
400
+ y: number;
401
+ width: number;
402
+ height: number;
403
+ padding?: number;
404
+ immediate?: boolean;
405
+ }): void;
406
+ getRuntime(): Runtime | null;
398
407
  }
399
408
  declare function polygonToTarget(polygon: InputShape, on?: {
400
409
  width: number;
@@ -82,7 +82,7 @@ Error generating stack: `+i.message+`
82
82
  width: var(--atlas-container-width, ${p?`${p}px`:"100%"});
83
83
  height: var(--atlas-container-height, ${Oj(d||(e?m.height:512))})
84
84
  }
85
- `}),i]})}),iv=()=>(0,st.useContext)(ev),av=()=>{let t=iv();return t?t.runtime:void 0};var DI=(t,e=[])=>{let n=av();(0,st.useEffect)(()=>n?n.registerHook("useFrame",t):()=>{},e)},ov=()=>{let t=iv();return t&&t.canvas?t.canvas:void 0};function _j(){return(0,st.useContext)(nv)}async function zj(t,e,n){if(st.version.startsWith("18.")||st.version.startsWith("19.")){let i=await Promise.resolve().then(()=>_(eI(),1)),a=i.default?i.default.createRoot:i.createRoot;n.current||(n.current=a(t)),n.current.render(e)}else if(typeof ReactDOM<"u"){let{render:i,unmountComponentAtNode:a}=ReactDOM;i(e,t),n.current={unmount(){a(t)}}}else{let i=await Promise.resolve().then(()=>_(ds(),1)),a=i.default?i.default.render:i.render,o=i.default?i.default.unmountComponentAtNode:i.unmountComponentAtNode;a(e,t),n.current={unmount(){o(t)}}}}var _i=st.default.forwardRef(({children:t,...e},n)=>{let i=(0,st.useRef)(),a=av(),o=(0,st.useRef)(0),s=(0,st.useRef)(),u=(0,st.useRef)();return DI(()=>{if(e.relative){let d=i.current;if(d&&a){let p=a.getScaleFactor();o.current!==p&&(o.current=p,d.style.transformOrigin="0 0",d.style.transform=`scale(${1/o.current})`,d.style.width=`${o.current*100}%`,d.style.height=`${o.current*100}%`,i.current&&s.current?.__owner.value?.rotation&&(d.style.transform=`scale(${1/o.current}) translate(50%, 50%) rotate(${s.current?.__owner.value?.rotation||0}deg) translate(-50%, -50%)`))}}},[e.relative]),(0,st.useLayoutEffect)(()=>{let d=s.current;n&&d&&(typeof n=="function"?n(d):n.current=d);async function p(){if(d&&d.__host){let f=e.relative?(0,Ce.jsx)("div",{ref:i,children:t}):t;await zj(d.__host.element,f,u)}}d&&d.__host?p():d&&(d.__onCreate=p)},[n,t,s,e.relative]),(0,st.useLayoutEffect)(()=>()=>{u.current&&setTimeout(()=>{u.current.unmount()},0)},[]),(0,Ce.jsx)("box",{html:!0,...e,ref:s})});_i.displayName="HTMLPortal";var pI=(t,e,n=[])=>{let i=av();(0,st.useEffect)(()=>{if(i){let a=e,o=Uc[t];i.world.activatedEvents.push(o);let s=o.slice(2).toLowerCase();return i.world.addEventListener(s,a),()=>{i.world.removeEventListener(s,a)}}return()=>{}},[i?i.world:void 0,t,...n])};function mI(t,e){if(t===0)return[0,1];if(e===0)return[1,0];let n=Math.abs(t)/Math.abs(e);return[n,1-n]}function Nj(){let t=(0,st.useRef)({ctrl:!1,shift:!1,alt:!1});return(0,st.useLayoutEffect)(()=>{function e(i){i.key==="Shift"&&(t.current.shift=!0),i.key==="Control"&&(t.current.ctrl=!0),i.key==="Alt"&&(t.current.alt=!0)}function n(i){i.key==="Shift"&&(t.current.shift=!1),i.key==="Control"&&(t.current.ctrl=!1),i.key==="Alt"&&(t.current.alt=!1)}return window.addEventListener("keydown",e),window.addEventListener("keyup",n),()=>{window.removeEventListener("keydown",e),window.removeEventListener("keyup",n)}},[]),t}var LI=(t,e)=>{let n=hj(),i=av(),a=_j(),o=(0,st.useRef)(),s=(0,st.useRef)(null),u=(0,st.useRef)(),[d,p]=(0,st.useState)(!1),f=(0,st.useRef)(!1),m=(0,st.useRef)({north:0,south:0,east:0,west:0}),g=Nj(),v=(0,st.useCallback)(x=>A=>{if(f.current=!0,p(!0),a&&i){let{top:T,left:M}=a,z=i.viewerToWorld(A.pageX-M,A.pageY-T);u.current={x:z.x,y:z.y},o.current=x}},[a,i]),S=(0,st.useMemo)(()=>t.width/t.height,[t.width,t.height]),E=(0,st.useCallback)(x=>{if(!Math.abs(x.north-x.south+(x.east-x.west)))return;let A=-x.west+x.east,T=-x.north+x.south,M=t.width+A,z=t.height+T;if(M/z>=S){let D=M-z*S,[I,q]=mI(x.east,x.west);x.west+=D*q,x.east-=D*I}else{let D=z-M/S,[I,q]=mI(x.north,x.south);x.north+=D*I,x.south-=D*q}},[t.width,t.height,S]);DI(()=>{u&&i&&i.updateNextFrame()}),(0,st.useEffect)(()=>{i&&i.updateNextFrame()},[i,d]);let C=(0,st.useCallback)(x=>{if(!i||!a||i.mode!=="sketch")return;let{top:A,left:T}=a,M=i.viewerToWorld(x.pageX-T,x.pageY-A),z=s.current,D=!t.maintainAspectRatio&&g.current.alt,I=!D&&g.current.shift&&o.current?.indexOf("-")!==-1;if((o.current==="translate"||o.current==="east"||o.current==="north-east"||o.current==="south-east")&&(m.current.east=M.x-(u.current?u.current.x:0),D&&(m.current.west=-m.current.east)),(o.current==="translate"||o.current==="west"||o.current==="north-west"||o.current==="south-west")&&(m.current.west=M.x-(u.current?u.current.x:0),D&&(m.current.east=-m.current.west)),(o.current==="translate"||o.current==="north"||o.current==="north-east"||o.current==="north-west")&&(m.current.north=M.y-(u.current?u.current.y:0),D&&(m.current.south=-m.current.north)),(o.current==="translate"||o.current==="south"||o.current==="south-west"||o.current==="south-east")&&(m.current.south=M.y-(u.current?u.current.y:0),D&&(m.current.north=-m.current.south)),(t.maintainAspectRatio||I)&&E(m.current),z){let q=m.current.west,B=m.current.north,U=t.width+m.current.east,W=t.height+m.current.south;z.points[1]=Math.min(q,U),z.points[2]=Math.min(B,W),z.points[3]=Math.max(q,U),z.points[4]=Math.max(B,W),i.updateNextFrame()}},[i,t.width,t.height,t.maintainAspectRatio,a]);pI("mousemove",C,[t.width,t.height,a]),pI("pointermove",C,[t.width,t.height,a]);let R=(0,st.useRef)();return(0,st.useEffect)(()=>{R.current=()=>{if(f.current){let x=m.current.west,A=m.current.north,T=t.width+m.current.east,M=t.height+m.current.south,z=Math.min(x,T),D=Math.min(A,M),I=Math.max(x,T),q=Math.max(A,M),B={x:(t.x||0)+z,y:(t.y||0)+D,width:I-z||1,height:q-D||1};t.maintainAspectRatio,e(B),o.current=void 0,u.current=void 0,m.current.east=0,m.current.west=0,m.current.north=0,m.current.south=0,f.current=!1,p(!1)}}},[e,t.height,t.width,t.x,t.y]),(0,st.useEffect)(()=>{let x=()=>{R.current&&R.current()};return window.addEventListener("pointerup",x),window.addEventListener("touchend",x),()=>{window.removeEventListener("pointerup",x),window.removeEventListener("touchend",x)}},[]),{portalRef:s,mode:n,mouseEvent:v,onPointerMoveCallback:C,isEditing:d}};function gh(t,e){return t?e?{...t,...e||{},":hover":t[":hover"]?Object.assign(t[":hover"]||{},e[":hover"]||{}):e[":hover"],":active":t[":active"]?Object.assign(t[":active"]||{},e[":active"]||{}):e[":hover"]}:t:e}var ar=_(nt(),1);function ka(t){return{all:t=t||new Map,on:function(e,n){var i=t.get(e);i?i.push(n):t.set(e,[n])},off:function(e,n){var i=t.get(e);i&&(n?i.splice(i.indexOf(n)>>>0,1):t.set(e,[]))},emit:function(e,n){var i=t.get(e);i&&i.slice().map(function(a){a(n)}),(i=t.get("*"))&&i.slice().map(function(a){a(e,n)})}}}var Is=_(nt(),1),PI=_(Y(),1),Ij=ka(),rv=(0,Is.createContext)(Ij);function Dj({children:t,emitter:e}){return(0,PI.jsx)(rv.Provider,{value:e,children:t})}function gR(){return(0,Is.useContext)(rv)}function Ds(t,e,n=[]){let i=(0,Is.useContext)(rv);(0,Is.useEffect)(()=>{let a=o=>{e(o)};return i.on(t,a),()=>{i.off(t,a)}},[i,t,...n])}function Lj(t){return{all:t=t||new Map,on:function(e,n){var i=t.get(e);i?i.push(n):t.set(e,[n])},off:function(e,n){var i=t.get(e);i&&(n?i.splice(i.indexOf(n)>>>0,1):t.set(e,[]))},emit:function(e,n){var i=t.get(e);i&&i.slice().map(function(a){a(n)}),(i=t.get("*"))&&i.slice().map(function(a){a(e,n)})}}}function Pj(t,e,n){let[i,a]=t,[o,s]=e,[u,d]=n,p=u-o,f=d-s,m=Math.sqrt(p*p+f*f);if(m===0)return{distance:Math.sqrt((i-o)**2+(a-s)**2),closestPoint:[o,s]};let g=Math.max(0,Math.min(1,((i-o)*p+(a-s)*f)/(m*m))),v=o+g*p,S=s+g*f,E=[v,S];return{distance:Math.sqrt((i-v)**2+(a-S)**2),closestPoint:E}}function Bj(t,e,n,i){let[a,o]=t,[s,u]=e,[d,p]=n,[f,m]=i,g=(a-s)*(p-m)-(o-u)*(d-f);if(Math.abs(g)<1e-10)return null;let v=((a-d)*(p-m)-(o-p)*(d-f))/g,S=-((a-s)*(o-p)-(o-u)*(a-d))/g;if(v>=0&&v<=1&&S>=0&&S<=1){let E=a+v*(s-a),C=o+v*(u-o);return[E,C]}return null}function Hj(t,e,n){let i=n||e.snapThreshold||15,a=[],[o,s]=t,u=e.polygon.points,d=e.slowState.transitionIntentType==="move-point";if(e.slowState.snapToPoints)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=u[p];if(f[0]===o&&f[1]===s)continue;let m=f[0]-o,g=f[1]-s,v=Math.sqrt(m*m+g*g);v<=i&&a.push({type:"point",point:[f[0],f[1]],source:{pointIndex:p,polygon:e.polygon},distance:v})}if(e.slowState.snapToLines)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=(p+1)%u.length;if(p===f)continue;let m=u[p],g=u[f],{distance:v,closestPoint:S}=Pj(t,m,g);v<=i&&a.push({type:"line",point:S,source:{lineIndex:p,polygon:e.polygon},distance:v})}if(e.slowState.snapToIntersections&&u.length>=4)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=(p+1)%u.length,m=u[p],g=u[f];for(let v=p+2;v<u.length;v++){if(d&&e.selectedPoints.includes(v))continue;let S=(v+1)%u.length;if(v===f||S===p)continue;let E=u[v],C=u[S],R=Bj(m,g,E,C);if(R){let[x,A]=R,T=Math.sqrt((o-x)**2+(s-A)**2);T<=i&&a.push({type:"intersection",point:R,source:{lineIndex:p,polygon:e.polygon},distance:T})}}}if(e.slowState.snapToParallel)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=u[p],[m,g]=f,v=Math.abs(s-g);v<=i&&a.push({type:"parallel",point:[o,g],source:{pointIndex:p,polygon:e.polygon},distance:v});let S=Math.abs(o-m);S<=i&&a.push({type:"parallel",point:[m,s],source:{pointIndex:p,polygon:e.polygon},distance:S})}return a.sort((p,f)=>p.distance-f.distance)}function Uj(t){return t.sort((e,n)=>{let i=e.type==="point"?0:e.type==="intersection"?1:e.type==="parallel"?2:3,a=n.type==="point"?0:n.type==="intersection"?1:n.type==="parallel"?2:3;return i!==a?i-a:e.distance-n.distance})}function Vj(t,e){let n=[];for(let i of t)switch(i.type){case"point":n.push({type:"point",points:[i.point],target:i});break;case"line":if(i.source?.lineIndex!==void 0){let a=e.polygon.points,o=i.source.lineIndex,s=(o+1)%a.length,u=a[o],d=a[s];n.push({type:"line",points:[u,d,i.point],target:i})}else n.push({type:"point",points:[i.point],target:i});break;case"intersection":n.push({type:"cross",points:[i.point],target:i});break;case"parallel":if(i.source?.pointIndex!==void 0){let a=e.polygon.points[i.source.pointIndex];n.push({type:"parallel-line",points:[a,i.point],target:i})}break}return n}function vl(t,e,n=5,i){if(!e.slowState.snapEnabled)return e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null,null;let a=Hj(t,e,void 0);if(a.length===0)return e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null,null;let o=Uj(a);return e.snapTargets=o,e.activeSnapGuides=Vj(o.slice(0,n),e),e.isSnapping=!0,e.snapPoint=o[0].point,e.snapPoint}function jI(t){t.snapTargets=[],t.activeSnapGuides=[],t.isSnapping=!1,t.snapPoint=null}function jj(t){return t.isSnapping&&t.snapPoint!==null}function xh(t,e){return jj(e)&&e.snapPoint?e.snapPoint:t}var qj={type:"add-open-point",label:"Add point",tools:["pen","line","lineBox"],trigger:{type:"click"},isValid(t,e,n){if(!e.isOpen)return!1;if(e.polygon.points.length===0)return!0;if(e.selectedPoints.length!==1||e.polygon.points.length>=2&&(e.slowState.currentTool==="line"||e.slowState.currentTool==="lineBox"))return!1;if(e.slowState.bounds){let{x:a,y:o,width:s,height:u}=e.slowState.bounds,[d,p]=t[0];if(d<a||d>a+s||p<o||p>o+u)return!1}let i=e.selectedPoints[0];return i===0||i===e.polygon.points.length-1},commit(t,e,n){let i=e.line?e.line[1]:t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3),i=xh(i,e));let a=e.polygon.points;if(a.length===0)return{selectedPoints:[0],points:[i]};n.Shift;let o=e.selectedPoints[0],s=e.slowState.currentTool==="line"||e.slowState.currentTool==="lineBox";if(o===0)return~~a[0][0]===~~i[0]&&~~a[0][1]===~~i[1]?{selectedPoints:[0],points:a}:{selectedPoints:s?[]:[0],points:[i,...a]};let u=a[a.length-1];return~~u[0]===~~i[0]&&~~u[1]===~~i[1]?{selectedPoints:[a.length-1],points:a}:(e.line=null,{selectedPoints:s?[]:[a.length],points:[...a,i]})}};function BI(t,e){return Math.abs(t-e)}function aa(t,e){let n=BI(t[0],e[0]),i=BI(t[1],e[1]);return Math.sqrt(n**2+i**2)}function yR(t,e){return[t[0]-e[0],t[1]-e[1]]}function Fj(t,e){return t[0]*e[0]+t[1]*e[1]}function qI(t){return t[0]*t[0]+t[1]*t[1]}function lv(t,e){return qI(yR(t,e))}function FI(t,e,n,i){return i&&n&&(t<0?t=Math.max(t,-i.x-n.x):t>0&&(t=Math.min(t,i.x+i.width-n.x-n.width)),e<0?e=Math.max(e,-i.y-n.y):e>0&&(e=Math.min(e,i.y+i.height-n.y-n.height))),[t,e]}function GI(t,e){let n,i,a,o;var s=(S,E,C,R,x)=>{if(R<=C+1)return;let A=C,T=0,M=S*S;n=[E[C],E[R]],i=yR(n[1],n[0]);let z=qI(i),D,I,q,B,U;for(let W=C+1;W<R;W++)D=yR(E[W],n[0]),B=Fj(D,i),B<=0?U=lv(E[W],n[0]):z<=B?U=lv(E[W],n[1]):(q=B/z,I=[n[0][0]+q*i[0],n[0][1]+q*i[1]],U=lv(E[W],I)),!(U<=T)&&(A=W,T=U);T>M&&(x[A]=1,s(S,E,C,A,x),s(S,E,A,R,x))};let u=t.length,d=[],p,f,m,g,v=e*e;for(a=[],o=[],a[0]=t[0],p=f=1,g=0;p<u;p++)lv(t[p],t[g])<v||(a[f++]=t[p],g=p);for(g<u-1&&(a[f++]=t[u-1]),o[0]=o[f-1]=1,s(e,a,0,f-1,o),p=m=0;p<f;p++)o[p]&&(d[m++]=a[p]);return d}function Gj(t){if(t.length!==4)return!1;let e=1e-10,n=Math.min(...t.map(u=>u[0])),i=Math.max(...t.map(u=>u[0])),a=Math.min(...t.map(u=>u[1])),o=Math.max(...t.map(u=>u[1]));for(let u of t){let d=Math.abs(u[0]-n)<e||Math.abs(u[0]-i)<e,p=Math.abs(u[1]-a)<e||Math.abs(u[1]-o)<e;if(!d||!p)return!1}let s=[[n,a],[n,o],[i,a],[i,o]];for(let u of s)if(!t.some(d=>Math.abs(d[0]-u[0])<e&&Math.abs(d[1]-u[1])<e))return!1;return!0}function kI(t){let e=t.points.length,n=!1,i=[];if(e>1){t.iedges=[];let a=t.points[e-1];for(let o=0;o<e;o++){let s=a;if(a=t.points[o],a.length===6){let p=a[2],f=a[3],m=a[4],g=a[5];i.push([o,[a[0]+p,a[1]+f],[a[0]+m,a[1]+g]]),n=!0}let u=[a[0]-s[0],a[1]-s[1]],d=u[0]*u[0]+u[1]*u[1];d>0?t.iedges.push([u[0]/d,u[1]/d]):t.iedges.push([0,0])}}else e===1?t.iedges=[[0,0]]:t.iedges=null;t.isBezier=n,t.bezierLines=i}function kj(t,e){let n=1/0,i=null,a=-1,o=t.points.length;if(o<=1)return[null,n,a];let s=t.points[o-1];for(let u=0;u<o;u++){let d=s;s=t.points[u];let p=d[0]-e[0],f=d[1]-e[1],m=p*p+f*f;m<n&&(i=d,n=m,a=u===0?o-1:u-1)}return[i,n,a]}function uv(t){if(t.points.length>2){let e=Math.min(...t.points.map(o=>o[0])),n=Math.min(...t.points.map(o=>o[1])),i=Math.max(0,...t.points.map(o=>o[0])),a=Math.max(0,...t.points.map(o=>o[1]));t.boundingBox={x:e,y:n,width:i-e,height:a-n};return}t.boundingBox=null}function Yj(t){return t[0]*t[0]+t[1]*t[1]}function HI(t,e){let n=1/0,i=null,a=-1,o=[],s=t.points.length;if(s<=1)return[null,n,o,a];if(t.iedges===null&&(kI(t),!t.iedges))return[null,n,o,a];let u=t.points[s-1];for(let d=0;d<s;d++){let p=u;u=t.points[d];let f=(e[0]-p[0])*t.iedges[d][0]+(e[1]-p[1])*t.iedges[d][1],m;f<=0?m=p:f<1?m=[(1-f)*p[0]+f*u[0],(1-f)*p[1]+f*u[1]]:m=u;let g=Yj([m[0]-e[0],m[1]-e[1]]);g<n&&(i=m,n=g,o=[p,u],a=d===0?s-1:d-1)}return[i,n,o,a]}var yh={type:"bounding-box-corners",label:"Drag to resize",tools:["pointer","box"],modifiers:{Shift:"Maintain aspect ratio",Alt:"Scale from center",Meta:"Rotate"},isValid(t,e,n){if(e.transitionDirection=null,e.transitionRotate=!1,e.isOpen||!e.slowState.boxMode&&e.selectedPoints.length!==e.polygon.points.length||!e.polygon.boundingBox)return!1;let i=n.proximity*.5,a=n.proximity*5,o=t[0],s=e.polygon.boundingBox,u=s.x+i,d=s.x+s.width-i*2,p=s.y+i,f=s.y+s.height-i*2;if(o[0]>u&&o[0]<d&&o[1]>p&&o[1]<f)return!1;let m=[s.x+s.width,s.y+s.height],g=[s.x,s.y],v=[s.x+s.width,s.y],S=[s.x,s.y+s.height],E=["ne","nw","se","sw"],C={ne:v,nw:g,se:m,sw:S},R=[aa(t[0],v),aa(t[0],g),aa(t[0],m),aa(t[0],S)],x=Math.min(...R),A=R.indexOf(x),T=A===-1?null:E[A],M=T?C[T]:null;if(R[0]<a||R[1]<a||R[2]<a||R[3]<a){if(T){let[z,D]=o;(n.Meta||z>=M[0]-n.proximity*4&&D>=M[1]-n.proximity*4&&z<=M[0]+n.proximity*4&&D<=M[1]+n.proximity*4&&!(z>=M[0]-n.proximity*2&&D>=M[1]-n.proximity*2&&z<=M[0]+n.proximity*2&&D<=M[1]+n.proximity*2))&&(e.slowState.boxMode||(e.transitionRotate=!0)),e.transitionDirection=T}return!0}return!1},start(t,e,n){let i=t[0],a=e.polygon.boundingBox,o=[a.x+a.width,a.y+a.height],s=[a.x,a.y],u=[a.x+a.width,a.y],d=[a.x,a.y+a.height],p=["ne","nw","se","sw"],f=[aa(t[0],u),aa(t[0],s),aa(t[0],o),aa(t[0],d)],m=Math.min(...f),g=f.indexOf(m);e.transitionDirection=g!==-1?p[g]:null},transition(t,e,n){let i=e.polygon.boundingBox,a=e.transitionOrigin||t[0],[o,s]=t[0];if((n.Meta||e.transitionRotate)&&!e.slowState.boxMode){let x=[i.x+i.width/2,i.y+i.height/2],A=Math.atan2(a[1]-x[1],a[0]-x[0]),T=Math.atan2(s-x[1],o-x[0]),M=n.Shift,z=Math.PI/(n.Alt?4:12),D=T-A;M&&(D=Math.round(D/z)*z);let I=Math.cos(D),q=Math.sin(D),B=[];for(let U of e.polygon.points){let W=U[0]-x[0],it=U[1]-x[1],K=W*I-it*q,tt=W*q+it*I,ut=K+x[0],xt=tt+x[1];B.push(U.length===6?[ut,xt,U[2],U[3],U[4],U[5]]:[ut,xt])}e.transitionPoints=B,uv({points:B,iedges:null,boundingBox:null,isBezier:!1,bezierLines:[]}),e.transitionBoundingBox=e.polygon.boundingBox?{...e.polygon.boundingBox,rotation:D}:null;return}let u=e.transitionDirection,d=u.includes("e")?1:-1,p=u.includes("s")?1:-1,f=!!n.Alt,m;if(f)m=[i.x+i.width/2,i.y+i.height/2];else switch(u){case"se":m=[i.x,i.y];break;case"sw":m=[i.x+i.width,i.y];break;case"ne":m=[i.x,i.y+i.height];break;case"nw":m=[i.x+i.width,i.y+i.height];break}let g=f?i.width/2:i.width,v=f?i.height/2:i.height,S,E;if(n.Shift||e.slowState.fixedAspectRatio){let x=d*g,A=p*v,T=o-m[0],M=s-m[1],z=x*x+A*A,D=z>0?(T*x+M*A)/z:1;D=Math.max(D,.01),S=D,E=D}else{let x=0,A=0;switch(u){case"se":x=o-a[0],A=s-a[1];break;case"sw":x=a[0]-o,A=s-a[1];break;case"ne":x=o-a[0],A=a[1]-s;break;case"nw":x=a[0]-o,A=a[1]-s;break}f&&(x*=2,A*=2),S=(i.width+x)/i.width,E=(i.height+A)/i.height}let C=[];for(let x of e.polygon.points){let A=x[0]-m[0],T=x[1]-m[1],M=A*S,z=T*E,D=M+m[0],I=z+m[1];C.push(x.length===6?[D,I,x[2],x[3],x[4],x[5]]:[D,I])}e.transitionPoints=C;let R={points:C,iedges:null,boundingBox:null,bezierLines:[],isBezier:!1};uv(R),e.transitionBoundingBox=R.boundingBox},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,e.transitionRotate=!1,{points:i}}},Qj={type:"close-line-box",label:"Close Line Box",tools:["lineBox"],trigger:{type:"click"},isValid(t,e,n){return e.polygon.points.length===2&&e.lineBox!==null&&e.slowState.boxMode===!1},commit(t,e,n){return e.lineBox?{isOpen:!1,points:e.lineBox,tool:"pen"}:{}}},YI={type:"close-shape",label:"Close shape",tools:["pen","line"],trigger:{type:"click"},isValid(t,e,n){if(!e.isOpen||e.slowState.boxMode||e.selectedPoints.length!==1||e.polygon.points.length<2||n.Meta)return!1;let i=e.selectedPoints[0];if(i===0){let a=e.polygon.points[e.polygon.points.length-1];return aa(t[0],a)<n.proximity}if(i===e.polygon.points.length-1){let a=e.polygon.points[0];return aa(t[0],a)<n.proximity}return!1},commit(t,e,n){let i=e.selectedPoints[0];if(e.line=null,i===0)return{selectedPoints:[],points:e.polygon.points.slice(0).reverse(),isOpen:!1};if(i===e.polygon.points.length-1)return{selectedPoints:[],isOpen:!1}}},Xj={type:"close-shape-line",label:"Close shape line",trigger:{type:"click"},tools:["pen"],isValid(t,e,n){return!e.isOpen||e.slowState.boxMode||e.polygon.points.length<3||!e.closestLinePoint||e.closestLineDistance>=n.proximity*2?!1:(e.closestLineIndex===0||(e.closestLineIndex,e.polygon.points.length-2),!0)},commit(t,e,n){return YI.commit(t,e,n)}},QI={type:"split-line",label:"Split line",tools:["pen"],isValid(t,e,n){let i=e.slowState.currentTool==="line";return n.Meta||i?!1:!!(e.closestLinePoint&&e.closestLineDistance<n.proximity)},transition(t,e,n){let i=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3,e.closestLineIndex),i=xh(i,e));let a=e.transitionOrigin||i,[o,s]=i,u=e.polygon.points,d=[],p=o-a[0],f=s-a[1];for(let m=0;m<u.length;m++)if(d.push(u[m]),e.closestLineIndex===m){let g=e.closestLinePoint;d.push([g[0]+p,g[1]+f])}e.transitionPoints=d},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{selectedPoints:[e.closestLineIndex+1],points:i}}},Kj={type:"cut-line",label:"Add new point",tools:["pen"],trigger:{type:"click"},modifiers:{Shift:"Cut line"},isValid(t,e,n){return e.slowState.boxMode?!1:QI.isValid(t,e,n)},commit(t,e,n){if(n.Shift&&!e.isOpen){let o=e.polygon.points,s=[],u=[],d=!1;for(let p=0;p<o.length;p++)d?u.push(o[p]):s.push(o[p]),e.closestLineIndex===p&&(d=!0);return{isOpen:!0,points:[...u,...s]}}let i=e.polygon.points,a=[];for(let o=0;o<i.length;o++)if(a.push(i[o]),e.closestLineIndex===o){let s=e.closestLinePoint;a.push(s)}return{points:a}}},Zj={type:"delete-point",label:"Delete point",tools:["pointer","pen","lineBox","line"],trigger:{type:"key",key:"Backspace"},isValid(t,e,n){return e.selectedPoints.length>0&&e.slowState.canDelete},commit(t,e,n){let i=e.polygon.points.filter((a,o)=>!e.selectedPoints.includes(o));return{isOpen:i.length<3,selectedPoints:[],points:i,tool:e.slowState.boxMode?"box":void 0}}},Jj={type:"deselect-bounding-box",label:"Deselect bounding box",tools:["pointer"],trigger:{type:"click"},isValid(t,e,n){return e.slowState.canDeselect&&!e.isOpen&&!e.slowState.boxMode&&e.selectedPoints.length>2&&e.selectedPoints.length===e.polygon.points.length},commit(t,e,n){return e.slowState.lastCreationTool==="pen"?{selectedPoints:[],tool:"pen"}:{selectedPoints:[]}}},Wj={type:"deselect-draw",label:"Deselect draw",trigger:{type:"key",key:"Escape"},tools:["pointer","pen","box","lineBox","stamp","hand","pencil"],isValid(t,e,n){return e.slowState.canDeselect},commit(t,e,n){return{selectedPoints:[]}}},$j={type:"deselect-points",label:"Deselect points",trigger:{type:"click"},tools:["pointer","pen"],isValid(t,e,n){return e.selectedPoints.length>0&&e.slowState.canDeselect},commit(t,e,n){return{selectedPoints:[]}}},tq={type:"draw-shape",label:"Draw shape",tools:["pen","pencil"],modifiers:{Alt:"Freehand drawing"},isValid(t,e,n){let i=n.Alt||e.slowState.currentTool==="pencil";return!!(e.isOpen&&(e.line||e.polygon.points.length===0)&&i)},start(t,e,n){e.transitionDraw=[]},transition(t,e,n){let i=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3),i=xh(i,e)),e.transitionDraw.push(i)},commit(t,e,n){let i=e.slowState.currentTool==="pencil"?e.scale*1.5:e.scale*3,a=GI(e.transitionDraw,i);if(e.transitionDraw=[],a.length===0)return;let o=!0,s=3,u=e.selectedPoints[0],d=[...e.polygon.points,...a],p=d[0],f=d[d.length-1];u===0&&(p=d[d.length-1],f=d[0],d=[...a,...e.polygon.points.slice(0).reverse()]);let m=[d.length-1],g=aa(p,f)/n.proximity<s,v="";return g&&(o=!1,v="pointer",m=d.map((S,E)=>E)),{isOpen:o,tool:v||void 0,points:d,selectedPoints:m}}},eq={type:"move-line",label:"Move line",tools:["line"],isValid(t,e,n){return e.slowState.currentTool!=="line"||e.slowState.boxMode?!1:!!(e.closestLinePoint&&e.closestLineDistance<n.proximity)},transition(t,e){let n=e.transitionOrigin,i=e.polygon.boundingBox,a=t[0],[o,s]=FI(a[0]-n[0],a[1]-n[1],i,e.slowState.bounds);e.transitionPoints=e.polygon.points.map(u=>u.length===6?[u[0]+o,u[1]+s,u[2],u[3],u[4],u[5]]:[u[0]+o,u[1]+s])},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},nq={type:"move-point",label:"Move point",tools:["pointer","pen","line"],modifiers:{Shift:"Constrain to axis"},isValid(t,e,n){if(n.Alt)return!1;let i=n.proximity,[a,o]=t[0],s=e.polygon.points,u=e.selectedPoints;for(let d=0;d<s.length;d++){let p=s[d],f=p[0]-a,m=p[1]-o;if(f*f+m*m<i*i)return!0}return!1},start(t,e,n){if(e.slowState.boxMode)return yh.start?yh.start(t,e,n):void 0;if(e.selectedPoints.length<2){let[i,a,o]=kj(e.polygon,t[0]);return{selectedPoints:[o]}}if(e.slowState.closestPoint&&!e.selectedPoints.includes(e.slowState.closestPoint))return{selectedPoints:[e.slowState.closestPoint]}},transition(t,e,n){if(e.slowState.boxMode)return yh.transition(t,e,n);let i=e.transitionOrigin,a=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(a,e,3),a=xh(a,e));let[o,s]=a,u=e.polygon.points,d=e.selectedPoints;if(e.slowState.closestPoint){let g=e.slowState.closestPoint;d.indexOf(g)===-1&&(d=[g])}let p=[],f=o-i[0],m=s-i[1];n.Shift&&!e.slowState.snapEnabled&&(Math.abs(f)>Math.abs(m)?m=0:f=0);for(let g=0;g<u.length;g++){let v=d.indexOf(g)!==-1,S=u[g];v?S.length===6?p.push([S[0]+f,S[1]+m,S[2],S[3],S[4],S[5]]):p.push([S[0]+f,S[1]+m]):p.push(S)}e.transitionPoints=p},commit(t,e,n){if(e.slowState.boxMode)return yh.commit(t,e,n);let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},XI={type:"move-bounding-box",label:"Move bounding box",tools:["pointer"],isValid(t,e,n){let i=e.polygon.points.length,a=i===e.selectedPoints.length,o=e.polygon.boundingBox;if(i<1||!a||!o)return!1;let s=t[0],u=s[0],d=s[1];return u>=o.x&&u<=o.x+o.width&&d>=o.y&&d<=o.y+o.height},transition(t,e,n){let i=e.slowState.bounds,a=e.transitionOrigin,o=e.polygon.boundingBox,s=t[0],[u,d]=FI(s[0]-a[0],s[1]-a[1],o,i);e.transitionPoints=e.polygon.points.map(p=>p.length===6?[p[0]+u,p[1]+d,p[2],p[3],p[4],p[5]]:[p[0]+u,p[1]+d]),o&&(e.transitionBoundingBox={x:o.x+u,y:o.y+d,width:o.width,height:o.height})},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},KI={type:"move-shape",label:"Move shape",tools:["pointer","hand","box"],isValid(t,e,n){return n.Shift?!1:e.slowState.pointerInsideShape&&(!e.isOpen||e.line===null)},transition(t,e,n){XI.transition(t,e,n)},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},iq={type:"nudge-right",label:"Nudge right",trigger:{type:"key",key:"ArrowRight"},tools:["pointer","pen"],modifiers:{Shift:"Nudge right more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>o.includes(u)?[s[0]+(n.Shift?4*i:1),s[1]]:s)}}},aq={type:"nudge-left",label:"Nudge left",trigger:{type:"key",key:"ArrowLeft"},tools:["pointer","pen"],modifiers:{Shift:"Nudge left more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>o.includes(u)?[Math.max(0,s[0]-(n.Shift?4*i:1)),s[1]]:s)}}},oq={type:"nudge-up",label:"Nudge up",trigger:{type:"key",key:"ArrowUp"},tools:["pointer","pen"],modifiers:{Shift:"Nudge up more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>{if(o.includes(u)){let d=s[0],p=Math.max(0,s[1]-(n.Shift?4*i:1));return s.length===6?[d,p,s[2],s[3],s[4],s[5]]:[d,p]}return s})}}},rq={type:"nudge-down",label:"Nudge down",trigger:{type:"key",key:"ArrowDown"},tools:["pointer","pen"],modifiers:{Shift:"Nudge down more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>{let d=s[0],p=Math.max(0,s[1]+(n.Shift?4*i:1));return o.includes(u)?s.length===6?[d,p,s[2],s[3],s[4],s[5]]:[d,p]:s})}}},lq={type:"select-multiple-points",label:"Drag to select multiple points",tools:["pointer","pen"],isValid(t,e,n){return e.slowState.currentTool==="line"&&e.polygon.points.length>=2?!0:!(e.slowState.boxMode||e.line)},transition(t,e,n){if(!e.transitionOrigin)return;let i=e.transitionOrigin[0],a=e.transitionOrigin[1],o=t[0][0],s=t[0][1],u=Math.min(i,o),d=Math.min(a,s),p=Math.abs(i-o),f=Math.abs(a-s);e.selectionBox={x:u,y:d,width:p,height:f}},commit(t,e,n){if(e.selectionBox){let i=[];n.Shift&&i.push(...e.selectedPoints);for(let a=0;a<e.polygon.points.length;a++){let o=e.polygon.points[a];o[0]>=e.selectionBox.x&&o[0]<=e.selectionBox.x+e.selectionBox.width&&o[1]>=e.selectionBox.y&&o[1]<=e.selectionBox.y+e.selectionBox.height&&i.push(a)}return{selectedPoints:i}}}},UI=10,sq={type:"select-point",label:"Select point",trigger:{type:"click"},tools:["pointer","pen","line"],modifiers:{Shift:"Add to selection"},isValid(t,e,n){let i=-1;if(e.selectedPoints.length===1){let u=e.selectedPoints[0];u===0&&(i=e.polygon.points.length-1),u===e.polygon.points.length-1&&(i=0)}if(e.slowState.boxMode)return!1;let[a,o]=t[0],s=e.polygon.points;for(let u=0;u<s.length;u++){let d=s[u],p=d[0]-a,f=d[1]-o;if(p*p+f*f<UI*UI)return i!==u}return!1},commit(t,e,n){let i=n.Shift?[...e.selectedPoints]:[],[a,o]=t[0],s=e.polygon.points.map((u,d)=>{let p=u[0]-a,f=u[1]-o;return[p*p+f*f,d]}).sort((u,d)=>u[0]-d[0])[0];if(e.line=null,!i.includes(s[1]))i.push(s[1]);else if(n.Shift)return{selectedPoints:i.filter(u=>u!==s[1])};return{selectedPoints:i}}},uq={type:"select-shape",label:"Select shape",trigger:{type:"click"},tools:["pointer","pen"],isValid(t,e,n){return KI.isValid(t,e,n)},commit(t,e,n){return{tool:"pointer",selectedPoints:e.polygon.points.map((i,a)=>a)}}},cq={type:"snap",label:"Smart snap",tools:["pointer","pen","line"],modifiers:{Shift:"Disable snapping"},isValid(t,e,n){return n.Shift||!e.slowState.snapEnabled||e.slowState.transitioning&&e.slowState.transitionIntentType!=="snap"||!e.slowState.snapToPoints&&!e.slowState.snapToLines&&!e.slowState.snapToIntersections?!1:vl(t[0],e,0)!==null},start(t,e,n){vl(t[0],e,5)},transition(t,e,n){let i=vl(t[0],e,5);i&&(e.pointer=i)},commit(t,e,n){jI(e)}},fq={type:"stamp-fixed-size-shape",label:"Stamp fixed size shape",trigger:{type:"click"},tools:["stamp"],isValid(t,e,n){return e.slowState.selectedStamp!==null},commit(t,e,n){let i=t[0],a=e.slowState.selectedStamp;if(!i||!a)return;let o=i[0],s=i[1],u=[],d=Math.min(...a.points.map(E=>E[0])),p=Math.min(...a.points.map(E=>E[1])),f=Math.max(...a.points.map(E=>E[0]))-d,m=Math.max(...a.points.map(E=>E[1]))-p,g=4*n.proximity,v=f/g;m/g>v&&(v=m/g),o-=f/v/2,s-=m/v/2;let S=0;for(let E of a.points)u.push([o+(E[0]-d)/v,s+(E[1]-p)/v]),S++;return{isOpen:!1,points:u}}},ZI={id:"square",open:!1,points:[[0,0],[0,100],[100,100],[100,0]]},dq={id:"triangle",open:!1,points:[[50,0],[0,100],[100,100]]},hq={id:"pentagon",open:!1,points:[[0,0],[0,100],[100,100],[100,0],[50,-50]]},pq={id:"hexagon",open:!1,points:[[50,0],[0,25],[0,75],[50,100],[100,75],[100,25]]},mq={id:"circle",open:!1,points:[[948.949,571.107],[954.545,500],[954.545,500],[948.949,428.893],[948.949,428.893],[932.298,359.538],[932.298,359.538],[905.003,293.641],[905.003,293.641],[867.735,232.825],[867.735,232.825],[821.412,178.588],[821.412,178.588],[767.175,132.265],[767.175,132.265],[706.359,94.997],[706.359,94.997],[640.462,67.702],[640.462,67.702],[571.107,51.051],[571.107,51.051],[500,45.455],[500,45.455],[428.893,51.051],[428.893,51.051],[359.538,67.702],[359.538,67.702],[293.641,94.997],[293.641,94.997],[232.825,132.265],[232.825,132.265],[178.588,178.588],[178.588,178.588],[132.265,232.825],[132.265,232.825],[94.997,293.641],[94.997,293.641],[67.702,359.538],[67.702,359.538],[51.051,428.893],[51.051,428.893],[45.455,500],[45.455,500],[51.051,571.107],[51.051,571.107],[67.702,640.462],[67.702,640.462],[94.997,706.359],[94.997,706.359],[132.265,767.175],[132.265,767.175],[178.588,821.412],[178.588,821.412],[232.825,867.735],[232.825,867.735],[293.641,905.003],[293.641,905.003],[359.538,932.298],[359.538,932.298],[428.893,948.949],[428.893,948.949],[500,954.545],[500,954.545],[571.107,948.949],[571.107,948.949],[640.462,932.298],[640.462,932.298],[706.359,905.003],[706.359,905.003],[767.175,867.735],[767.175,867.735],[821.412,821.412],[821.412,821.412],[867.735,767.175],[867.735,767.175],[905.003,706.359],[905.003,706.359],[932.298,640.462],[932.298,640.462],[948.949,571.107]]},gq={type:"stamp-shape",label:"Stamp shape",tools:["stamp","box"],modifiers:{Shift:"Maintain aspect ratio"},isValid(t,e,n){return!(e.polygon.points.length&&!e.slowState.canDelete)},start(t,e,n){let i=t[0],a=e.slowState.selectedStamp||ZI;if(i){let o=i[0],s=i[1],u=[],d=Math.min(...a.points.map(E=>E[0])),p=Math.min(...a.points.map(E=>E[1])),f=Math.max(...a.points.map(E=>E[0]))-d,m=Math.max(...a.points.map(E=>E[1]))-p,g=f/32;m/32>g&&(g=m/32);let v=[],S=0;for(let E of a.points)v.push(S),u.push([o+(E[0]-d)/g,s+(E[1]-p)/g]),S++;return e.transitionPoints=u,{isOpen:!1,selectedPoints:v,points:u}}},transition(t,e,n){if(!e.transitionOrigin)return;let i=e.polygon.boundingBox,a=[i.x,i.y],o=e.transitionOrigin||t[0],[s,u]=t[0],d=e.transitionOrigin[0],p=e.transitionOrigin[1],f=t[0][0],m=t[0][1],g=f-d-32,v=m-p-32;if(n.Shift||e.slowState.fixedAspectRatio){let x=i.width/i.height;Math.abs(i.width/g)>Math.abs(i.height/v)?v=g/x:g=v*x}let S=(i.width+g)/i.width,E=(i.height+v)/i.height,C=[];for(let x of e.polygon.points){let A=x[0]-a[0],T=x[1]-a[1],M=A*S,z=T*E,D=M+a[0],I=z+a[1];if(x.length===6){C.push([D,I,x[2],x[3],x[4],x[5]]);continue}C.push([D,I])}e.transitionPoints=C;let R={points:C,iedges:null,boundingBox:null,bezierLines:[],isBezier:!1};uv(R),e.transitionBoundingBox=R.boundingBox},commit(t,e,n){return{isOpen:!1,points:e.transitionPoints,tool:"pointer"}}},yq={type:"toggle-snap",label:"Toggle snapping",trigger:{type:"key",key:"`"},tools:["pointer","pen","line","lineBox","box","stamp"],modifiers:{},isValid(t,e,n){return!0},commit(t,e,n){let i=!e.slowState.snapEnabled;return e.slowState.snapEnabled=i,i||(e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null),{}}};function JI(t={}){let e=t.proximityThreshold||10,n=t.closestLinePointFill||{selected:"#fff",unselected:"rgba(255, 255, 255, .5)"},i=t.closestLinePointStroke||{selected:"#000",unselected:"rgba(0, 0, 0, .5)"};function a(S,E,C){S&&E.transitionBoundingBox&&C.transitioning&&(S.setAttribute("x",""+E.transitionBoundingBox.x),S.setAttribute("y",""+E.transitionBoundingBox.y),S.setAttribute("width",""+E.transitionBoundingBox.width),S.setAttribute("height",""+E.transitionBoundingBox.height))}function o(S,E,C){S&&E.polygon.boundingBox&&C.showBoundingBox&&(S.setAttribute("x",""+E.polygon.boundingBox.x),S.setAttribute("y",""+E.polygon.boundingBox.y),S.setAttribute("width",""+E.polygon.boundingBox.width),S.setAttribute("height",""+E.polygon.boundingBox.height))}function s(S,E,C){if(S&&E.polygon.boundingBox&&C.showBoundingBox){let R=E.transitionBoundingBox||E.polygon.boundingBox;R.rotation?(S.style.transformOrigin=`${R.x+R.width/2}px ${R.y+R.height/2}px`,S.style.transform=`rotate(${Math.round(100*R.rotation*(180/Math.PI))/100}deg)`):S.style.transform="",S.setAttribute("points",[[R.x,R.y],[R.x+R.width,R.y],[R.x+R.width,R.y+R.height],[R.x,R.y+R.height]].map(x=>x.join(",")).join(" "))}}function u(S,E,C){S&&E.transitionPoints&&C.transitioning&&S.setAttribute("points",E.transitionPoints.map(R=>R.join(",")).join(" "))}function d(S,E,C){S&&E.closestLinePoint&&C.hasClosestLine&&(S.setAttribute("cx",""+E.closestLinePoint[0]),S.setAttribute("cy",""+E.closestLinePoint[1]),S.setAttribute("fill",E.closestLineDistance<e?n.selected:n.unselected),S.setAttribute("stroke",E.closestLineDistance<e?i.selected:i.unselected))}function p(S,E,C){S&&E.closestLinePoint&&C.hasClosestLine&&S.setAttribute("transform",`translate(${E.closestLinePoint[0]}, ${E.closestLinePoint[1]})`)}function f(S,E,C){S&&E.selectionBox&&C.transitionIntentType==="select-multiple-points"&&(S.setAttribute("x",""+E.selectionBox.x),S.setAttribute("y",""+E.selectionBox.y),S.setAttribute("width",""+E.selectionBox.width),S.setAttribute("height",""+E.selectionBox.height))}function m(S,E,C){if(S&&E.pointer&&E.selectedPoints.length)if(C.actionIntentType==="close-shape"||C.actionIntentType==="close-shape-line"){let R=E.polygon.points[0],x=E.polygon.points[E.polygon.points.length-1];R&&x&&S.setAttribute("points",`${R.join(",")} ${x.join(",")}`)}else if(E.line)S.setAttribute("points",`${E.line[0].join(",")} ${E.line[1].join(",")}`);else{let R=E.polygon.points[E.selectedPoints[0]];R&&R.length&&S.setAttribute("points",`${E.pointer[0]},${E.pointer[1]} ${R.join(",")}`)}}function g(S,E,C,R=3){S&&E.transitionDraw.length&&S.setAttribute("points",GI(E.transitionDraw,R*E.scale).map(x=>x.join(",")).join(" "))}function v(S,E){S&&E.lineBox&&S.setAttribute("points",E.lineBox.map(C=>C.join(",")).join(" "))}return{updateTransitionBoundingBox:a,updateClosestLinePointTransform:p,updateBoundingBoxPolygon:s,updateBoundingBox:o,updateTransitionShape:u,updateClosestLinePoint:d,updateSelectBox:f,updatePointLine:m,updateDrawPreview:g,updateLineBox:v}}var vq=typeof window<"u"?window.requestAnimationFrame:t=>setTimeout(t,16),Sq=typeof window<"u"?window.cancelAnimationFrame:t=>clearTimeout(t);function VI(t){return!["hand","pointer"].includes(t)}var Sh=[yh,nq,eq,QI,KI,XI,tq,lq,gq,cq],bq=Sh.length,vh=[Qj,fq,YI,sq,Xj,Kj,qj,uq,Jj,$j],jc=[Wj,iq,aq,oq,rq,Zj,yq],bh={};Sh.forEach(t=>{bh[t.type]=t});vh.forEach(t=>{bh[t.type]=t});jc.forEach(t=>{bh[t.type]=t});var sv=10;function WI(t,e){let n=t?.fixedAspectRatio||!1,i=t?.tool||"pointer",a=t?.keyboardShortcutMapping||{},o=typeof t?.keyboardShortcutsEnabled=="boolean"?t.keyboardShortcutsEnabled:!0,s=t?.enabledTools||["pointer","pen","box","lineBox","stamp","hand","line","pencil"],u=t?.emitter||Lj(),d={V:"pointer",P:"pen",B:"box",L:"lineBox",S:"stamp",H:"hand",N:"line",D:"pencil",...t?.toolMap||{}},p={shapeId:t?.id||null,noShape:t===null,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:sv},bounds:t?.bounds||null,showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},pointerInsideShape:!1,closestPoint:null,selectedStamp:null,transitionModifiers:null,boxMode:!1,bezierLines:[],fixedAspectRatio:n,isToolSwitchingLocked:!1,canDeselect:!1,canDelete:!0,cursor:"default",enabledTools:s,currentTool:i,lastCreationTool:VI(i)?i:null,snapEnabled:!1,snapToPoints:!0,snapToLines:!0,snapToIntersections:!0,snapToGrid:!1,snapToParallel:!0},f={isOpen:typeof t?.open=="boolean"?t.open:!1,polygon:{points:t?.points||[],iedges:null,boundingBox:null,isBezier:null,bezierLines:[]},scale:1,selectedPoints:[],pointer:null,line:null,lineBox:null,transitionPoints:null,transitionOrigin:null,transitionBoundingBox:null,closestLinePoint:null,closestLineDistance:0,closestLineIndex:-1,transitionRotate:!1,transitionDirection:null,transitionBezierLine:null,selectionBox:null,slowState:p,transitionDraw:[],isPanning:!1,panStart:null,snapTargets:[],activeSnapGuides:[],snapThreshold:15,isSnapping:!1,snapPoint:null},m={startTime:0,time:0,shouldUpdate:!1,nextSlowState:null,renderFunc:()=>{},setStateFunc:()=>{},animationFrame:0,actionIntent:null,transitionIntent:null,undoStack:[],undoStackPointer:-1},g={isPressed:!1,isClicking:!1,lastPress:null,pressTimeout:0,noTransition:!1};function v(P){kI(P),P.isBezier&&S({bezierLines:P.bezierLines}),uv(P),m.shouldUpdate=!0}v(f.polygon);function S(P){let Q=Object.keys(P);if(Q.length===0)return;let dt=m.nextSlowState||f.slowState,ht=!1;if(Q.length===1&&Q[0]==="validIntentKeys"&&dt.validIntentKeys){let vt=Object.keys(dt.validIntentKeys),j=Object.keys(P.validIntentKeys||{});if(vt.length===j.length){for(let L of vt)dt.validIntentKeys[L]!==P.validIntentKeys[L]&&(ht=!0);if(!ht)return}}if(P.selectedPoints){let vt=Array.from(dt.selectedPoints||[]),j=Array.from(P.selectedPoints||[]);if(vt.length===j.length){for(let L of vt)if(!j.includes(L)){ht=!0;break}}else ht=!0;ht||delete P.selectedPoints}if(!(Q.length===1&&Q.includes("hasClosestLine")&&P.hasClosestLine===dt.hasClosestLine)){if(m.nextSlowState){m.nextSlowState={...m.nextSlowState,...P};return}m.nextSlowState={...f.slowState,...P}}}function E(P,Q=!1){m.time+=P,C(),R(),q(),I(),D(),U(),it(),W(),z(),m.renderFunc(f,f.slowState,P),!Q&&(m.animationFrame=vq(E))}function C(){if(m.shouldUpdate&&(e({id:f.slowState.shapeId||void 0,open:f.isOpen,points:f.polygon.points}),m.shouldUpdate=!1),m.nextSlowState&&m.nextSlowState!==f.slowState){let P=Object.keys(f.slowState),Q={},dt={},ht=!1;for(let vt of P){let j=f.slowState[vt],L=m.nextSlowState[vt];j!==L?(ht=!0,Q[vt]=L,dt[vt]=L):Q[vt]=j}ht&&(f.slowState=Q,m.nextSlowState=null,m.setStateFunc(f.slowState),t?.customSetState?.(dt))}}function R(){let P=!f.slowState.noShape&&f.polygon.points.length>2&&f.selectedPoints.length===f.polygon.points.length&&!f.isOpen&&(f.slowState.currentTool==="pointer"||f.slowState.currentTool==="box");f.slowState.showBoundingBox!==P&&S({showBoundingBox:P})}function x(P){f.selectedPoints=P.selectedPoints,S({selectedPoints:P.selectedPoints}),f.polygon.points=P.points,f.polygon.boundingBox=null,f.polygon.iedges=null,f.polygon.isBezier=null,v(f.polygon),S({closestPoint:null}),(P.isOpen===!0||P.isOpen===!1)&&(f.isOpen=P.isOpen,m.shouldUpdate=!0)}function A(){if(m.undoStackPointer===-1)return;m.undoStackPointer--;let P=m.undoStack[m.undoStackPointer];x(P)}function T(){if(m.undoStackPointer===m.undoStack.length-1)return;m.undoStackPointer++;let P=m.undoStack[m.undoStackPointer];x(P)}function M(P){m.undoStackPointer++,m.undoStack[m.undoStackPointer]=P,m.undoStack.length>15&&(m.undoStack.shift(),m.undoStackPointer--)}function z(){S({boxMode:Gj(f.polygon.points||[])})}function D(){if(!f.pointer||f.slowState.noShape){S({pointerInsideShape:!1});return}let[P,Q]=f.pointer,dt=f.polygon.points,ht=f.polygon.boundingBox;if(!ht){S({pointerInsideShape:!1});return}if(P<ht.x||P>ht.x+ht.width||Q<ht.y||Q>ht.height+ht.y){S({pointerInsideShape:!1});return}let vt=!1;for(let j=0,L=dt.length-1;j<dt.length;L=j++)dt[j][1]>Q!=dt[L][1]>Q&&P<(dt[L][0]-dt[j][0])*(Q-dt[j][1])/(dt[L][1]-dt[j][1])+dt[j][0]&&(vt=!vt);S({pointerInsideShape:vt})}function I(){if(f.slowState.noShape||!f.pointer||f.slowState.transitioning||!f.polygon.points||f.polygon.points.length===0)return;let[P,Q]=f.pointer,dt=f.polygon.points.map((ht,vt)=>{let j=ht[0]-P,L=ht[1]-Q;return[j*j+L*L,vt]}).sort((ht,vt)=>ht[0]-vt[0]);dt.length&&S({closestPoint:dt[0][1]})}function q(){if(!f.pointer||f.slowState.transitioning)return;if(f.slowState.noShape){f.closestLineIndex=-1,f.closestLinePoint=null,f.closestLineDistance=0;return}let[P,Q,dt,ht]=HI(f.polygon,f.pointer),vt=ee()*ee();Q<vt&&(!f.isOpen||f.slowState.currentTool==="line"||f.polygon.points.length-1!==ht)?(f.closestLinePoint=P,f.closestLineDistance=Math.sqrt(Q),f.closestLineIndex=ht,S({hasClosestLine:!0})):(f.closestLinePoint=null,f.closestLineDistance=0,f.closestLineIndex=-1,S({hasClosestLine:!1}))}function B(P,Q=null){let dt={},ht=Q||(f.pointer?[f.pointer[0],f.pointer[1]]:null);for(let vt of[...Sh,...vh,...jc])vt.tools.includes(P)&&(ht&&vt.isValid([ht],f,K())?dt[vt.type]=vt.label:ht||(dt[vt.type]=vt.label));return dt}function U(){if(f.slowState.transitioning||f.slowState.noShape||!f.pointer||!Array.isArray(f.pointer)||f.pointer.length<2||typeof f.pointer[0]!="number"||typeof f.pointer[1]!="number")return;let P=f.slowState.currentTool;Mn(P);let Q=!1;for(let vt=0;vt<bq;vt++){let j=Sh[vt];if(j.tools.includes(P)&&j.isValid(g.isPressed?[g.lastPress]:[f.pointer],f,K())){if(m.transitionIntent===j){Q=!0;break}S({transitionIntentType:j.type}),m.transitionIntent=j,Q=!0;break}}Q||m.transitionIntent&&(S({transitionIntentType:null}),m.transitionIntent=null);let dt=!1;for(let vt=0;vt<vh.length;vt++){let j=vh[vt];if(j.tools.includes(P)&&ut(j)){if(m.actionIntent===j){dt=!0;break}S({actionIntentType:j.type}),m.actionIntent=j,dt=!0;break}}dt||m.actionIntent&&(m.actionIntent=null,S({actionIntentType:null}));let ht={};for(let vt=0;vt<jc.length;vt++){let j=jc[vt];j.trigger.type==="key"&&ut(j)&&(ht[j.trigger.key]=j.label)}S({validIntentKeys:ht})}function W(){let P=f.pointer;if(f.lineBox=null,!P||lt()||f.polygon.points.length!==2)return;let[Q,dt]=P,[ht,vt]=f.polygon.points[0],[j,L]=f.polygon.points[1],[$,at,Nt,Lt]=HI(f.polygon,P);if(!$)return;let jt=Q-$[0],G=dt-$[1],Z=[ht,vt],mt=[j,L],yt=[mt[0]+jt,mt[1]+G],pt=[Z[0]+jt,Z[1]+G];f.lineBox=[Z,mt,yt,pt]}function it(){let P=f.pointer;if(!P||!lt()||f.slowState.noShape)return;let Q=f.selectedPoints[0],dt=P;if(f.slowState.snapEnabled&&!f.slowState.modifiers.Shift?(vl(P,f,5),dt=xh(P,f)):jI(f),f.line=[f.polygon.points[Q],dt],f.slowState.modifiers.Shift){let ht=0;if(f.polygon.points.length>1){let G=f.selectedPoints[0];G===0?ht=Math.atan2(f.polygon.points[1][1]-f.polygon.points[0][1],f.polygon.points[1][0]-f.polygon.points[0][0]):ht=Math.atan2(f.polygon.points[G-1][1]-f.polygon.points[G][1],f.polygon.points[G-1][0]-f.polygon.points[G][0])}let vt=P[0]-f.polygon.points[Q][0],j=P[1]-f.polygon.points[Q][1],L=Math.atan2(j,vt),$=Math.PI/4,at=Math.round((L-ht)/$)*$+ht,Nt=aa(f.polygon.points[Q],P),Lt=Math.cos(at)*Nt+f.polygon.points[Q][0],jt=Math.sin(at)*Nt+f.polygon.points[Q][1];f.line[1]=[Lt,jt]}}function K(){return f.slowState.modifiers}function tt(P){let Q=P.commit([g.lastPress],f,K());u.emit("polygons.end-transition",{transitionIntent:P.type,response:Q}),Q&&(Q.tool&&$t(Q.tool),Q.selectedPoints&&(f.selectedPoints=Q.selectedPoints,S({selectedPoints:Q.selectedPoints})),Q.points&&(f.polygon.points=Q.points,f.polygon.boundingBox=null,f.polygon.iedges=null,f.polygon.isBezier=null,f.transitionBoundingBox=null,v(f.polygon),S({closestPoint:null,selectedStamp:null})),(Q.isOpen===!0||Q.isOpen===!1)&&(f.isOpen=Q.isOpen,m.shouldUpdate=!0),M({isOpen:f.isOpen,points:f.polygon.points,selectedPoints:f.selectedPoints}),Mn(f.slowState.currentTool)),S({transitionModifiers:null})}function ut(P){if(!P.tools.includes(f.slowState.currentTool))return!1;let Q=[];return f.pointer&&Array.isArray(f.pointer)&&f.pointer.length>=2&&typeof f.pointer[0]=="number"&&typeof f.pointer[1]=="number"&&(Q=[f.pointer]),P.isValid(Q,f,K())}function xt(P){P==="Delete"&&(P="Backspace");let Q=f.slowState.currentTool;for(let dt of jc)if(!(dt.trigger.type!=="key"||dt.trigger.key!==P)&&dt.tools.includes(Q)&&ut(dt))return tt(dt),!0;return!1}function lt(){let P=f.polygon.points,Q=f.pointer;if(!f.isOpen||!P.length||f.selectedPoints.length!==1||!Q)return!1;let dt=f.selectedPoints[0],ht=dt===0,vt=dt===P.length-1;return ht||vt}let ft={reset(){S({modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:sv}})},getForType(P){let Q={};if(!P)return Q;let dt=bh[P];return!dt||!dt.modifiers?Q:dt.modifiers},set(P){P!=="Shift"&&P!=="Alt"&&P!=="Meta"||S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,[P]:!0}})},unset(P){P!=="Shift"&&P!=="Alt"&&P!=="Meta"||S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,[P]:!1}})}},Rt={set(P){let Q=P?P.id==="square"||P.id==="rectangle":!1;S({selectedStamp:P,boxMode:Q})},clear(){Rt.set(null)},square(){Rt.set(ZI)},triangle(){Rt.set(dq)},pentagon(){Rt.set(hq)},hexagon(){Rt.set(pq)},circle(){Rt.set(mq)}},X={down(P){let Q=a[P]||P;if(Q==="Shift"||Q==="Alt"||Q==="Meta")return ft.set(Q),!0;if(Q==="Control")return ft.set("Meta"),!0;if(Q==="z"&&f.slowState.modifiers.Meta)return f.slowState.modifiers.Shift?T():A(),!0;if(!f.slowState.modifiers.Meta&&!f.slowState.modifiers.Alt&&!f.slowState.transitioning){if(!o)return;let dt=Q.toUpperCase();if(d[dt]){let ht=d[dt];return $t(ht),!0}}return xt(Q)},up(P){let Q=a[P]||P;if(Q==="Control"){ft.unset("Meta");return}Q!=="Shift"&&Q!=="Alt"&&Q!=="Meta"||ft.unset(Q)}};function Ct(P){f.scale=P,S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,proximity:P*sv}})}function ee(){return sv*f.scale}let ce={set:P=>{m.renderFunc=P},start:(P,Q)=>{m.startTime=performance.now(),m.time=0,P&&(m.renderFunc=P),Q&&(Q(f.slowState),m.setStateFunc=Q),E(0)},stop:()=>{m.animationFrame&&(Sq(m.animationFrame),m.animationFrame=0)},step:(P=16)=>{E(P)}};function le(P){if(!P||!Array.isArray(P)||P.length===0)return;let Q=P[0];if(!(!Q||!Q[0]&&Q[0]!==0||!Q[1]&&Q[1]!==0)&&!(Number.isNaN(Q[0])||Number.isNaN(Q[1]))){if(f.pointer=Q,f.slowState.transitioning)m.transitionIntent&&(m.transitionIntent.transition(P,f,K()),u.emit("polygons.transition",{transitionIntent:m.transitionIntent.type}));else if(g.isPressed&&!g.noTransition&&g.lastPress&&f.pointer&&(!g.isClicking||aa(g.lastPress,f.pointer)>10))if(clearTimeout(g.pressTimeout),g.isClicking=!1,m.transitionIntent){if(f.transitionOrigin=g.lastPress,S({transitioning:!0}),m.transitionIntent.start){let dt=m.transitionIntent.start([g.lastPress],f,K());dt&&(dt.selectedPoints&&(f.selectedPoints=dt.selectedPoints,S({selectedPoints:dt.selectedPoints})),(dt.isOpen===!0||dt.isOpen===!1)&&(f.isOpen=dt.isOpen,m.shouldUpdate=!0),dt.points&&(f.polygon.points=dt.points,f.polygon.boundingBox=null,f.polygon.iedges=null,v(f.polygon),S({closestPoint:null})))}S({transitionModifiers:m.transitionIntent.modifiers||null}),u.emit("polygons.start-transition",{transitionIntent:m.transitionIntent.type}),m.transitionIntent.transition([g.lastPress],f,K()),u.emit("polygons.transition",{transitionIntent:m.transitionIntent.type})}else g.noTransition=!0}}function pe(){f.pointer=null,f.slowState.transitioning||(g.isPressed=!1,g.isClicking=!1,g.lastPress=null,S({transitionIntentType:null,actionIntentType:null}),m.transitionIntent=null,m.actionIntent=null)}function De(){g.isClicking=!0,g.isPressed=!0,g.lastPress=f.pointer,g.pressTimeout=setTimeout(()=>{g.isPressed&&(g.isClicking=!1)},250)}function ne(){f.slowState.transitioning?(S({transitioning:!1}),m.transitionIntent&&tt(m.transitionIntent),m.transitionIntent=null,S({transitionIntentType:null})):g.isClicking&&(m.actionIntent&&tt(m.actionIntent),m.actionIntent=null,S({actionIntentType:null})),g.isPressed=!1,g.isClicking=!1,g.lastPress=null,g.noTransition=!1,clearTimeout(g.pressTimeout)}function Di(P){f.polygon.points=P?.points||[],f.polygon.boundingBox=null,f.polygon.iedges=null,f.isOpen=P?.open||!1,v(f.polygon),m.nextSlowState=null,m.undoStack=[],m.undoStackPointer=-1,f.lineBox=null,f.line=null,g.pressTimeout&&clearTimeout(g.pressTimeout),S({noShape:P===null,shapeId:P?.id||null,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:f.slowState.modifiers.proximity},showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},selectedStamp:null,closestPoint:null,pointerInsideShape:!1,boxMode:!1,fixedAspectRatio:n}),$t(i),C()}function ri(P){if(!P)return"";let Q=bh[P];return!Q||!Q.modifiers?Q.label||"":f.slowState.modifiers.Shift&&Q.modifiers.Shift?Q.modifiers.Shift:f.slowState.modifiers.Alt&&Q.modifiers.Alt?Q.modifiers.Alt:f.slowState.modifiers.Meta&&Q.modifiers.Meta?Q.modifiers.Meta:Q.label}function me(){S({fixedAspectRatio:!0})}function ge(){S({fixedAspectRatio:!1})}function $t(P){if(f.slowState.isToolSwitchingLocked||f.slowState.currentTool===P||!f.slowState.enabledTools.includes(P))return;let Q=f.slowState.lastCreationTool;if(VI(P)){if(f.slowState.isToolSwitchingLocked)return;Q=P}f.slowState.transitioning&&(f.transitionPoints=null,f.transitionBoundingBox=null,f.transitionDraw=[],f.line=null,f.lineBox=null,S({transitioning:!1}));let dt=B(P),ht=f.selectedPoints,vt=f.isOpen,j=f.slowState.selectedStamp;switch(P){case"pointer":f.polygon.points.length>0&&!vt&&ht.length===0&&(ht=f.polygon.points.map((L,$)=>$));break;case"pen":f.polygon.points.length===0?(ht=[],vt=!0):vt&&ht.length>1&&(ht=[ht[ht.length-1]]);break;case"line":f.polygon.points.length===0?(ht=[],vt=!0):vt?ht.length>1&&(ht=[ht[ht.length-1]]):(ht=[f.polygon.points.length-1],vt=!0);break;case"pencil":ht=[],f.polygon.points.length===0&&(vt=!0);break;case"box":ht=f.polygon.points.map((L,$)=>$),vt=!1,Rt.square();break;case"lineBox":ht=[],f.polygon.points.length===0&&(vt=!0);break;case"stamp":ht=[],vt=!1,j||Rt.square();break;case"hand":ht=[];break;default:ht=[];break}S({validIntentKeys:dt}),S({currentTool:P,selectedPoints:ht,lastCreationTool:Q,boxMode:P==="box"}),f.selectedPoints=ht,f.isOpen=vt,Mn(P),U()}function Mn(P){let Q=P==="box"||P==="pointer"&&f.polygon.points.length>2&&f.selectedPoints.length===f.polygon.points.length&&!f.isOpen;S({showBoundingBox:Q})}function St(P){f.slowState.currentTool===P&&$t("pointer")}function It(P){f.slowState.currentTool===P?$t("pointer"):$t(P)}let te={undo:A,redo:T,get canUndo(){return m.undoStackPointer>-1},get canRedo(){return m.undoStackPointer<m.undoStack.length-1}},Hn={setTool:$t,unsetTool:St,toggleTool:It,updateVisibility:Mn,setCanDelete(P){S({canDelete:P})},setCanDeselect(P){tn(),S({canDeselect:P})},lockToolSwitching(){S({isToolSwitchingLocked:!0})},unlockToolSwitching(){S({isToolSwitchingLocked:!1})},toggleToolSwitching(){S({isToolSwitchingLocked:!f.slowState.isToolSwitchingLocked})},shortcuts:Object.fromEntries(Object.entries(d).map(([P,Q])=>[Q,P])),getValidIntents(P){let Q=P||f.slowState.currentTool;return{actions:vh.filter(dt=>dt.tools.includes(Q)),transitions:Sh.filter(dt=>dt.tools.includes(Q)),keys:jc.filter(dt=>dt.tools.includes(Q)),all:B(Q),current:f.slowState.validIntentKeys}},getToolInfo(){return{name:f.slowState.currentTool,cursor:f.slowState.cursor,selectionCount:f.selectedPoints.length,pointCount:f.polygon.points.length,isOpen:f.isOpen}},smartSelect(){f.polygon.points.length!==0&&(f.isOpen?(f.selectedPoints=[f.polygon.points.length-1],S({selectedPoints:f.selectedPoints})):(f.selectedPoints=f.polygon.points.map((P,Q)=>Q),S({selectedPoints:f.selectedPoints})))},clearSelection(){f.selectedPoints=[],S({selectedPoints:[]})},autoSelect(){f.selectedPoints.length===f.polygon.points.length&&!f.isOpen?$t("pointer"):f.isOpen||f.selectedPoints.length>0?$t(f.slowState.lastCreationTool||"pen"):$t("pointer")}},Zn={enabled:()=>f.slowState.snapEnabled,enable:()=>S({snapEnabled:!0}),disable:()=>S({snapEnabled:!1}),toggle:()=>S({snapEnabled:!f.slowState.snapEnabled}),pointsEnabled:()=>f.slowState.snapToPoints,enablePoints:()=>S({snapToPoints:!0}),disablePoints:()=>S({snapToPoints:!1}),togglePoints:()=>S({snapToPoints:!f.slowState.snapToPoints}),linesEnabled:()=>f.slowState.snapToLines,enableLines:()=>S({snapToLines:!0}),disableLines:()=>S({snapToLines:!1}),toggleLines:()=>S({snapToLines:!f.slowState.snapToLines}),intersectionsEnabled:()=>f.slowState.snapToIntersections,enableIntersections:()=>S({snapToIntersections:!0}),disableIntersections:()=>S({snapToIntersections:!1}),toggleIntersections:()=>S({snapToIntersections:!f.slowState.snapToIntersections}),gridEnabled:()=>f.slowState.snapToGrid,enableGrid:()=>S({snapToGrid:!0}),disableGrid:()=>S({snapToGrid:!1}),toggleGrid:()=>S({snapToGrid:!f.slowState.snapToGrid}),parallelEnabled:()=>f.slowState.snapToParallel,enableParallel:()=>S({snapToParallel:!0}),disableParallel:()=>S({snapToParallel:!1}),toggleParallel:()=>S({snapToParallel:!f.slowState.snapToParallel}),getThreshold:()=>f.snapThreshold*f.slowState.modifiers.proximity,setThreshold:P=>{f.snapThreshold=Math.max(5,Math.min(50,P))},isActive:()=>f.isSnapping,getSnapPoint:()=>f.snapPoint,getActiveGuides:()=>f.activeSnapGuides,getTargets:()=>f.snapTargets};function tn(){S({selectedPoints:f.polygon.points.map((P,Q)=>Q)})}function bn(P){S({bounds:P})}return $t(i),{state:f,setState:S,emitter:u,modifiers:ft,stamps:Rt,history:te,key:X,setScale:Ct,clock:ce,pointer:le,blur:pe,pointerDown:De,pointerUp:ne,setShape:Di,label:ri,tools:Hn,snap:Zn,selectAllPoints:tn,setBounds:bn,lockAspectRatio:me,unlockAspectRatio:ge}}function qc(t){let e=Math.max(...t.map(o=>o[0])),n=Math.min(...t.map(o=>o[0])),i=Math.max(...t.map(o=>o[1])),a=Math.min(...t.map(o=>o[1]));for(let o of t)if(o[0]!==n&&o[0]!==e&&o[1]!==a&&o[1]!==i||o[0]<n||o[0]>e||o[1]<a||o[1]>i)return!1;return!0}function Sl(t){if(!t)return null;if(t.points.length>2){let e=Math.min(...t.points.map(o=>o[0])),n=Math.min(...t.points.map(o=>o[1])),i=Math.max(0,...t.points.map(o=>o[0])),a=Math.max(0,...t.points.map(o=>o[1]));return{x:e,y:n,width:i-e,height:a-n}}return null}function $I(t){return t.type==="SvgSelector"}function xq(t){return t.type==="BoxSelector"}function cv(t,e){if($I(t)){let s=t.svgShape==="polyline";if(!t.points||t.points.length===0)return null;if(e){let{width:u,height:d}=e;return{type:"SvgSelector",value:`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${u} ${d}" width="${u}" height="${d}"><${s?"polygon":"polyline"} points="${t.points.map(f=>f.join(",")).join(" ")}" /></svg>`}}return{type:"SvgSelector",value:`<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'><g><path d='M${t.points.map(u=>u.join(",")).join(" ")}${s?" Z":""}' /></g></svg>`}}if(!t.spatial)return null;let{x:n,y:i,width:a,height:o}=t.spatial||{};return!a||!o?null:{type:"FragmentSelector",value:`xywh=${~~n},${~~i},${~~a},${~~o}`}}function Fc(t,e){if(!t||!t.points.length)return null;if(qc(t.points)){let n=t.points.map(d=>d[0]),i=t.points.map(d=>d[1]),a=Math.min(...n),o=Math.min(...i),s=Math.max(...n)-a,u=Math.max(...i)-o;return cv({type:"BoxSelector",spatial:{x:a,y:o,width:s,height:u}},e)}return cv({type:"SvgSelector",points:t.points,svgShape:t.open?"polygon":"polyline"},e)}function Eh(t){if(t.type==="polygon"&&!t.noBox&&t.points&&qc(t.points)){let n=Sl({open:!1,points:t.points||[]});return Eh({...t,type:"box",selector:n})}if(t.type==="polygon"||t.type==="draw")return{polygon:{points:t.points||[],open:t.open||!1},requestType:t.type,boundingBox:Sl({points:t.points||[],open:!1}),metadata:{},arguments:t.arguments||{},target:Fc({points:t.points||[],open:!1},t.bounds)};let e=t.selector;if(e){let n=[[e.x,e.y],[e.x+e.width,e.y],[e.x+e.width,e.y+e.height],[e.x,e.y+e.height]];return{polygon:{points:n,open:!1},requestType:t.type,boundingBox:e,metadata:{},target:Fc({points:n,open:!1},t.bounds),arguments:t.arguments||{}}}return{polygon:{points:[],open:!1},requestType:t.type,boundingBox:null,target:null,metadata:{},arguments:t.arguments||{}}}var Eq={shapeId:null,noShape:!0,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,lastCreationTool:null,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:0},bounds:null,showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},pointerInsideShape:!1,closestPoint:null,transitionModifiers:null,selectedStamp:null,bezierLines:[],boxMode:!1,fixedAspectRatio:!1,cursor:"",enabledTools:["pointer","pen","box","lineBox","stamp","hand","line","pencil"],canDelete:!0,canDeselect:!0,isToolSwitchingLocked:!1,currentTool:"box",snapEnabled:!1,snapToPoints:!1,snapToLines:!1,snapToIntersections:!1,snapToGrid:!1,snapToParallel:!1};function vR({events:t,enabledTools:e,keyboardShortcutMapping:n,keyboardShortcutsEnabled:i=!1,debug:a}){let o=Yi((u,d)=>{let p=null,m=WI({emitter:t,keyboardShortcutsEnabled:i,keyboardShortcutMapping:n,enabledTools:e,customSetState:g=>{a&&console.log("partial state",g),u(v=>({polygonState:{...v.polygonState,...g}}))}},g=>{u(v=>v.tool.requestId?{polygon:{...g,id:v.tool.requestId}}:{polygon:{id:void 0,points:[],open:!0}}),t.emit("atlas.polygon-update",g)});return{mode:"explore",tool:{enabled:!1,requestId:null,canvasId:null},runtime:null,requestType:null,requests:{},history:m.history,polygon:null,validRequestIds:[],metadata:{},stableViewport:null,canvasRelativePositions:{},canvasViewports:{},polygons:m,polygonState:Eq,setMetadata:(g,v)=>{let S=v||d().tool.requestId;S&&u(E=>({metadata:{...E.metadata,[S]:{...E.metadata[S]||{},...g}}}))},setToolCanvasId:g=>{u(v=>({tool:{...v.tool,canvasId:g}}))},switchTool:{pointer(){u({mode:"sketch"}),s.tools.setTool("pointer")},hand(){u({mode:"explore"}),s.tools.setTool("hand")},draw(){u({mode:"sketch"}),s.tools.setTool("pencil")},pen(){u({mode:"sketch"}),s.tools.setTool("pen")},line(){u({mode:"sketch"}),s.tools.setTool("line")},lineBox(){u({mode:"sketch"}),s.tools.setTool("lineBox")},box(){u({mode:"sketch"}),s.tools.setTool("box")},triangle(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.triangle()},hexagon(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.hexagon()},circle(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.circle()},remove(){let g=d();g.tool.requestId&&(m.setShape({points:[],open:!0}),g.cancelRequest(g.tool.requestId))}},reset:()=>{let g=d();g.tool.requestId&&g.cancelRequest(g.tool.requestId)},setPolygonState:g=>u({polygonState:typeof g=="function"?g(d().polygonState):g}),getRequestId:()=>{let g=Math.random().toString(36).slice(2);return u(v=>({validRequestIds:[...v.validRequestIds,g]})),{requestId:g,clear:()=>{d().tool.requestId,u(v=>({tool:v.tool.requestId===g?{enabled:!1,requestId:null,canvasId:null}:v.tool,validRequestIds:v.validRequestIds.filter(S=>S!==g)}))}}},cancelRequest:g=>{let v=g||d().tool.requestId;v&&(u(S=>({mode:"explore",tool:S.tool.requestId===v?{enabled:!1,requestId:null,canvasId:null}:S.tool,validRequestIds:S.validRequestIds.filter(E=>E!==v)})),t.emit("atlas.request-cancelled",{id:v}))},requestAnnotation:async(g,v)=>{let E={...d().requests,[v.requestId]:g},C=Eh(g);a&&console.log("requestAnnotation",{response:C,request:g});try{let{points:R=[],open:x=!0}=C.polygon||{},{requestId:A,canvasId:T=null,toolId:M}=v,z=M,D=d(),I=D.validRequestIds.includes(A),q=C.requestType;return a&&console.log("setting points",{requestType:q,points:R,open:x}),!I||D.tool.enabled?null:(m.setShape({id:A,points:R,open:x}),q==="polygon"&&(z=z||"pen",m.tools.setTool("pen")),q==="draw"&&(z=z||"draw",m.tools.setTool("pencil")),q==="box"&&(z=z||"box",m.tools.setTool("box")),q==="target"?(z=z||"box",m.tools.setTool("box"),m.lockAspectRatio(),m.tools.lockToolSwitching(),m.tools.setCanDeselect(!1),m.tools.setCanDelete(!1)):(m.tools.unlockToolSwitching(),m.tools.setCanDeselect(!0),m.tools.setCanDelete(!0)),g.bounds&&m.setBounds(g.bounds),t.emit("atlas.annotation-request",{id:A}),u({polygon:{id:A,points:R,open:x},mode:"sketch",requestType:q,tool:{enabled:!0,requestId:A,canvasId:T},requests:E}),z?D.switchTool[z]?.():R.length===0&&D.switchTool.box(),(typeof g.selectByDefault>"u"&&R.length&&q==="box"||g.selectByDefault)&&D.switchTool.pointer(),new Promise(B=>{let U=it=>{it.id===A&&(u(K=>({mode:"explore",tool:{requestId:null,enabled:!1,canvasId:null},requests:Object.fromEntries(Object.entries(K.requests).filter(([tt])=>tt!==A))})),t.off("atlas.request-cancelled",U),t.off("atlas.annotation-completed",W),B(null))},W=it=>{it.id===A&&(u(K=>({mode:"explore",tool:{requestId:null,enabled:!1,canvasId:null},requests:Object.fromEntries(Object.entries(K.requests).filter(([tt])=>tt!==A))})),t.off("atlas.annotation-completed",W),t.off("atlas.request-cancelled",U),B(it))};t.on("atlas.request-cancelled",U),t.on("atlas.annotation-completed",W)}))}catch(R){return console.error(R),null}},completeRequest:g=>{let v=d().tool.requestId;if(typeof g=="string"&&g&&g!==v)return;let S=v?d().requests[v]?.arguments||{}:{},E=v?d().metadata[v]||{}:{},C=d().polygonState.bounds,R=d().polygon;R&&(t.emit("atlas.annotation-completed",{id:R.id,polygon:R,requestType:d().requestType,target:Fc(R,C),canvasId:d().tool.canvasId,boundingBox:Sl(R),metadata:E,arguments:{...S}}),m.setShape(null))},setAtlasRuntime:g=>{p=g,t.emit("atlas.ready",{runtime:g}),s.setScale(1/p._lastGoodScale),p.world.addLayoutSubscriber((v,S)=>{(v==="event-activation"||v==="zoom-to"||v==="go-home")&&p?._lastGoodScale&&!Number.isNaN(p._lastGoodScale)&&s.setScale(1/p._lastGoodScale)}),u({runtime:p})},clearAtlasRuntime:()=>{p=null,u({stableViewport:null,runtime:null})},setCanvasRelativePosition:(g,v)=>{u(S=>({canvasRelativePositions:{...S.canvasRelativePositions,[g]:v}}))},clearCanvasRelativePosition:g=>{u(v=>{let S={...v.canvasRelativePositions};return delete S[g],{canvasRelativePositions:S}})},changeMode:g=>{u({mode:g})},nudgeLeft:()=>{},nudgeRight:()=>{},nudgeUp:()=>{},nudgeDown:()=>{},zoomIn:()=>{p?.world?.zoomIn()},zoomOut:()=>{p?.world?.zoomOut()},goHome:()=>{p?.world?.goHome()}}}),s=o.getState().polygons;return t.on("atlas.annotation-request",()=>{s.clock.start((u,d,p)=>{t.emit("atlas.polygon-render",{state:u,slowState:d,dt:p})},u=>{typeof u=="object"?o.setState({polygonState:u}):o.setState(d=>({polygonState:u(d.polygonState)}))})}),t.on("atlas.annotation-completed",()=>{s.clock.stop()}),t.on("atlas.request-cancelled",()=>{s.clock.stop()}),o}var bR=_(Y(),1),Ls=(0,ar.createContext)(null);function Ie(){return(0,ar.useContext)(Ls)}var SR={};function xR(t="atlas"){return SR[t]?.getState()}function Gc({children:t,name:e="atlas",existing:n,atlasStoreConfig:i}){let a=gR(),o=(0,ar.useContext)(Ls),s=(0,ar.useMemo)(()=>n||o||vR({events:a,...i||{}}),[a,n,o]),u=bt(s,d=>d.mode);return(0,ar.useEffect)(()=>{let d=e;return d&&(SR[d]=s),()=>{d&&delete SR[d]}},[s,e]),(0,bR.jsx)(Ls.Provider,{value:s,children:(0,bR.jsx)(Vc.Provider,{value:u||"explore",children:t})})}var rD=_(nt(),1);var kc=_(nt(),1),tD=_(Y(),1);function ER(){return{default:{backgroundColor:"rgba(0,0,0,0)",borderWidth:"2px",borderColor:"rgba(252,0,98, .5)"},highlighted:{backgroundColor:"rgba(75, 103, 225, 0.4)",borderWidth:"1px",borderColor:"rgba(75,103,225,0.99)"},hidden:{borderWidth:"0px",borderColor:"rgba(0,0,0,0)",backgroundColor:"rgba(0,0,0,0)",hidden:!0}}}var RR=(0,kc.createContext)(ER());RR.displayName="AnnotationStyle";function Rh(){return(0,kc.useContext)(RR)}function Rq({theme:t,children:e}){let n=(0,kc.useMemo)(()=>t||ER(),[t]);return(0,tD.jsx)(RR.Provider,{value:n,children:e})}var nD=_(nt(),1);var Yc=_(nt(),1);var eD=Fm()((t,e)=>{let n=new Map,i=()=>Math.random().toString(36).substring(2,15),a=o=>{let s=n.get(o);if(s)return s;let u=i();return n.set(o,u),u};return{stylesheets:{},loading:[],errors:[],async parseStylesheet(o){let{loadStylesheet:s,setStylesheet:u}=e();if(!o)return null;if(typeof o=="string")return o.startsWith("http")?{id:o,type:"CssStylesheet",value:await s(o)}:{id:u(o),type:"CssStylesheet",value:o};if(o.type==="CssStylesheet"){let d=o.id;if("value"in o){let p=Array.isArray(o.value)?o.value.join(`
85
+ `}),i]})}),iv=()=>(0,st.useContext)(ev),av=()=>{let t=iv();return t?t.runtime:void 0};var DI=(t,e=[])=>{let n=av();(0,st.useEffect)(()=>n?n.registerHook("useFrame",t):()=>{},e)},ov=()=>{let t=iv();return t&&t.canvas?t.canvas:void 0};function _j(){return(0,st.useContext)(nv)}async function zj(t,e,n){if(st.version.startsWith("18.")||st.version.startsWith("19.")){let i=await Promise.resolve().then(()=>_(eI(),1)),a=i.default?i.default.createRoot:i.createRoot;n.current||(n.current=a(t)),n.current.render(e)}else if(typeof ReactDOM<"u"){let{render:i,unmountComponentAtNode:a}=ReactDOM;i(e,t),n.current={unmount(){a(t)}}}else{let i=await Promise.resolve().then(()=>_(ds(),1)),a=i.default?i.default.render:i.render,o=i.default?i.default.unmountComponentAtNode:i.unmountComponentAtNode;a(e,t),n.current={unmount(){o(t)}}}}var _i=st.default.forwardRef(({children:t,...e},n)=>{let i=(0,st.useRef)(),a=av(),o=(0,st.useRef)(0),s=(0,st.useRef)(),u=(0,st.useRef)();return DI(()=>{if(e.relative){let d=i.current;if(d&&a){let p=a.getScaleFactor();o.current!==p&&(o.current=p,d.style.transformOrigin="0 0",d.style.transform=`scale(${1/o.current})`,d.style.width=`${o.current*100}%`,d.style.height=`${o.current*100}%`,i.current&&s.current?.__owner.value?.rotation&&(d.style.transform=`scale(${1/o.current}) translate(50%, 50%) rotate(${s.current?.__owner.value?.rotation||0}deg) translate(-50%, -50%)`))}}},[e.relative]),(0,st.useLayoutEffect)(()=>{let d=s.current;n&&d&&(typeof n=="function"?n(d):n.current=d);async function p(){if(d&&d.__host){let f=e.relative?(0,Ce.jsx)("div",{ref:i,children:t}):t;await zj(d.__host.element,f,u)}}d&&d.__host?p():d&&(d.__onCreate=p)},[n,t,s,e.relative]),(0,st.useLayoutEffect)(()=>()=>{u.current&&setTimeout(()=>{u.current.unmount()},0)},[]),(0,Ce.jsx)("box",{html:!0,...e,ref:s})});_i.displayName="HTMLPortal";var pI=(t,e,n=[])=>{let i=av();(0,st.useEffect)(()=>{if(i){let a=e,o=Uc[t];i.world.activatedEvents.push(o);let s=o.slice(2).toLowerCase();return i.world.addEventListener(s,a),()=>{i.world.removeEventListener(s,a)}}return()=>{}},[i?i.world:void 0,t,...n])};function mI(t,e){if(t===0)return[0,1];if(e===0)return[1,0];let n=Math.abs(t)/Math.abs(e);return[n,1-n]}function Nj(){let t=(0,st.useRef)({ctrl:!1,shift:!1,alt:!1});return(0,st.useLayoutEffect)(()=>{function e(i){i.key==="Shift"&&(t.current.shift=!0),i.key==="Control"&&(t.current.ctrl=!0),i.key==="Alt"&&(t.current.alt=!0)}function n(i){i.key==="Shift"&&(t.current.shift=!1),i.key==="Control"&&(t.current.ctrl=!1),i.key==="Alt"&&(t.current.alt=!1)}return window.addEventListener("keydown",e),window.addEventListener("keyup",n),()=>{window.removeEventListener("keydown",e),window.removeEventListener("keyup",n)}},[]),t}var LI=(t,e)=>{let n=hj(),i=av(),a=_j(),o=(0,st.useRef)(),s=(0,st.useRef)(null),u=(0,st.useRef)(),[d,p]=(0,st.useState)(!1),f=(0,st.useRef)(!1),m=(0,st.useRef)({north:0,south:0,east:0,west:0}),g=Nj(),v=(0,st.useCallback)(x=>A=>{if(f.current=!0,p(!0),a&&i){let{top:T,left:M}=a,z=i.viewerToWorld(A.pageX-M,A.pageY-T);u.current={x:z.x,y:z.y},o.current=x}},[a,i]),S=(0,st.useMemo)(()=>t.width/t.height,[t.width,t.height]),E=(0,st.useCallback)(x=>{if(!Math.abs(x.north-x.south+(x.east-x.west)))return;let A=-x.west+x.east,T=-x.north+x.south,M=t.width+A,z=t.height+T;if(M/z>=S){let D=M-z*S,[I,q]=mI(x.east,x.west);x.west+=D*q,x.east-=D*I}else{let D=z-M/S,[I,q]=mI(x.north,x.south);x.north+=D*I,x.south-=D*q}},[t.width,t.height,S]);DI(()=>{u&&i&&i.updateNextFrame()}),(0,st.useEffect)(()=>{i&&i.updateNextFrame()},[i,d]);let C=(0,st.useCallback)(x=>{if(!i||!a||i.mode!=="sketch")return;let{top:A,left:T}=a,M=i.viewerToWorld(x.pageX-T,x.pageY-A),z=s.current,D=!t.maintainAspectRatio&&g.current.alt,I=!D&&g.current.shift&&o.current?.indexOf("-")!==-1;if((o.current==="translate"||o.current==="east"||o.current==="north-east"||o.current==="south-east")&&(m.current.east=M.x-(u.current?u.current.x:0),D&&(m.current.west=-m.current.east)),(o.current==="translate"||o.current==="west"||o.current==="north-west"||o.current==="south-west")&&(m.current.west=M.x-(u.current?u.current.x:0),D&&(m.current.east=-m.current.west)),(o.current==="translate"||o.current==="north"||o.current==="north-east"||o.current==="north-west")&&(m.current.north=M.y-(u.current?u.current.y:0),D&&(m.current.south=-m.current.north)),(o.current==="translate"||o.current==="south"||o.current==="south-west"||o.current==="south-east")&&(m.current.south=M.y-(u.current?u.current.y:0),D&&(m.current.north=-m.current.south)),(t.maintainAspectRatio||I)&&E(m.current),z){let q=m.current.west,B=m.current.north,U=t.width+m.current.east,W=t.height+m.current.south;z.points[1]=Math.min(q,U),z.points[2]=Math.min(B,W),z.points[3]=Math.max(q,U),z.points[4]=Math.max(B,W),i.updateNextFrame()}},[i,t.width,t.height,t.maintainAspectRatio,a]);pI("mousemove",C,[t.width,t.height,a]),pI("pointermove",C,[t.width,t.height,a]);let R=(0,st.useRef)();return(0,st.useEffect)(()=>{R.current=()=>{if(f.current){let x=m.current.west,A=m.current.north,T=t.width+m.current.east,M=t.height+m.current.south,z=Math.min(x,T),D=Math.min(A,M),I=Math.max(x,T),q=Math.max(A,M),B={x:(t.x||0)+z,y:(t.y||0)+D,width:I-z||1,height:q-D||1};t.maintainAspectRatio,e(B),o.current=void 0,u.current=void 0,m.current.east=0,m.current.west=0,m.current.north=0,m.current.south=0,f.current=!1,p(!1)}}},[e,t.height,t.width,t.x,t.y]),(0,st.useEffect)(()=>{let x=()=>{R.current&&R.current()};return window.addEventListener("pointerup",x),window.addEventListener("touchend",x),()=>{window.removeEventListener("pointerup",x),window.removeEventListener("touchend",x)}},[]),{portalRef:s,mode:n,mouseEvent:v,onPointerMoveCallback:C,isEditing:d}};function gh(t,e){return t?e?{...t,...e||{},":hover":t[":hover"]?Object.assign(t[":hover"]||{},e[":hover"]||{}):e[":hover"],":active":t[":active"]?Object.assign(t[":active"]||{},e[":active"]||{}):e[":hover"]}:t:e}var ar=_(nt(),1);function ka(t){return{all:t=t||new Map,on:function(e,n){var i=t.get(e);i?i.push(n):t.set(e,[n])},off:function(e,n){var i=t.get(e);i&&(n?i.splice(i.indexOf(n)>>>0,1):t.set(e,[]))},emit:function(e,n){var i=t.get(e);i&&i.slice().map(function(a){a(n)}),(i=t.get("*"))&&i.slice().map(function(a){a(e,n)})}}}var Is=_(nt(),1),PI=_(Y(),1),Ij=ka(),rv=(0,Is.createContext)(Ij);function Dj({children:t,emitter:e}){return(0,PI.jsx)(rv.Provider,{value:e,children:t})}function gR(){return(0,Is.useContext)(rv)}function Ds(t,e,n=[]){let i=(0,Is.useContext)(rv);(0,Is.useEffect)(()=>{let a=o=>{e(o)};return i.on(t,a),()=>{i.off(t,a)}},[i,t,...n])}function Lj(t){return{all:t=t||new Map,on:function(e,n){var i=t.get(e);i?i.push(n):t.set(e,[n])},off:function(e,n){var i=t.get(e);i&&(n?i.splice(i.indexOf(n)>>>0,1):t.set(e,[]))},emit:function(e,n){var i=t.get(e);i&&i.slice().map(function(a){a(n)}),(i=t.get("*"))&&i.slice().map(function(a){a(e,n)})}}}function Pj(t,e,n){let[i,a]=t,[o,s]=e,[u,d]=n,p=u-o,f=d-s,m=Math.sqrt(p*p+f*f);if(m===0)return{distance:Math.sqrt((i-o)**2+(a-s)**2),closestPoint:[o,s]};let g=Math.max(0,Math.min(1,((i-o)*p+(a-s)*f)/(m*m))),v=o+g*p,S=s+g*f,E=[v,S];return{distance:Math.sqrt((i-v)**2+(a-S)**2),closestPoint:E}}function Bj(t,e,n,i){let[a,o]=t,[s,u]=e,[d,p]=n,[f,m]=i,g=(a-s)*(p-m)-(o-u)*(d-f);if(Math.abs(g)<1e-10)return null;let v=((a-d)*(p-m)-(o-p)*(d-f))/g,S=-((a-s)*(o-p)-(o-u)*(a-d))/g;if(v>=0&&v<=1&&S>=0&&S<=1){let E=a+v*(s-a),C=o+v*(u-o);return[E,C]}return null}function Hj(t,e,n){let i=n||e.snapThreshold||15,a=[],[o,s]=t,u=e.polygon.points,d=e.slowState.transitionIntentType==="move-point";if(e.slowState.snapToPoints)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=u[p];if(f[0]===o&&f[1]===s)continue;let m=f[0]-o,g=f[1]-s,v=Math.sqrt(m*m+g*g);v<=i&&a.push({type:"point",point:[f[0],f[1]],source:{pointIndex:p,polygon:e.polygon},distance:v})}if(e.slowState.snapToLines)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=(p+1)%u.length;if(p===f)continue;let m=u[p],g=u[f],{distance:v,closestPoint:S}=Pj(t,m,g);v<=i&&a.push({type:"line",point:S,source:{lineIndex:p,polygon:e.polygon},distance:v})}if(e.slowState.snapToIntersections&&u.length>=4)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=(p+1)%u.length,m=u[p],g=u[f];for(let v=p+2;v<u.length;v++){if(d&&e.selectedPoints.includes(v))continue;let S=(v+1)%u.length;if(v===f||S===p)continue;let E=u[v],C=u[S],R=Bj(m,g,E,C);if(R){let[x,A]=R,T=Math.sqrt((o-x)**2+(s-A)**2);T<=i&&a.push({type:"intersection",point:R,source:{lineIndex:p,polygon:e.polygon},distance:T})}}}if(e.slowState.snapToParallel)for(let p=0;p<u.length;p++){if(d&&e.selectedPoints.includes(p))continue;let f=u[p],[m,g]=f,v=Math.abs(s-g);v<=i&&a.push({type:"parallel",point:[o,g],source:{pointIndex:p,polygon:e.polygon},distance:v});let S=Math.abs(o-m);S<=i&&a.push({type:"parallel",point:[m,s],source:{pointIndex:p,polygon:e.polygon},distance:S})}return a.sort((p,f)=>p.distance-f.distance)}function Uj(t){return t.sort((e,n)=>{let i=e.type==="point"?0:e.type==="intersection"?1:e.type==="parallel"?2:3,a=n.type==="point"?0:n.type==="intersection"?1:n.type==="parallel"?2:3;return i!==a?i-a:e.distance-n.distance})}function Vj(t,e){let n=[];for(let i of t)switch(i.type){case"point":n.push({type:"point",points:[i.point],target:i});break;case"line":if(i.source?.lineIndex!==void 0){let a=e.polygon.points,o=i.source.lineIndex,s=(o+1)%a.length,u=a[o],d=a[s];n.push({type:"line",points:[u,d,i.point],target:i})}else n.push({type:"point",points:[i.point],target:i});break;case"intersection":n.push({type:"cross",points:[i.point],target:i});break;case"parallel":if(i.source?.pointIndex!==void 0){let a=e.polygon.points[i.source.pointIndex];n.push({type:"parallel-line",points:[a,i.point],target:i})}break}return n}function vl(t,e,n=5,i){if(!e.slowState.snapEnabled)return e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null,null;let a=Hj(t,e,void 0);if(a.length===0)return e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null,null;let o=Uj(a);return e.snapTargets=o,e.activeSnapGuides=Vj(o.slice(0,n),e),e.isSnapping=!0,e.snapPoint=o[0].point,e.snapPoint}function jI(t){t.snapTargets=[],t.activeSnapGuides=[],t.isSnapping=!1,t.snapPoint=null}function jj(t){return t.isSnapping&&t.snapPoint!==null}function xh(t,e){return jj(e)&&e.snapPoint?e.snapPoint:t}var qj={type:"add-open-point",label:"Add point",tools:["pen","line","lineBox"],trigger:{type:"click"},isValid(t,e,n){if(!e.isOpen)return!1;if(e.polygon.points.length===0)return!0;if(e.selectedPoints.length!==1||e.polygon.points.length>=2&&(e.slowState.currentTool==="line"||e.slowState.currentTool==="lineBox"))return!1;if(e.slowState.bounds){let{x:a,y:o,width:s,height:u}=e.slowState.bounds,[d,p]=t[0];if(d<a||d>a+s||p<o||p>o+u)return!1}let i=e.selectedPoints[0];return i===0||i===e.polygon.points.length-1},commit(t,e,n){let i=e.line?e.line[1]:t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3),i=xh(i,e));let a=e.polygon.points;if(a.length===0)return{selectedPoints:[0],points:[i]};n.Shift;let o=e.selectedPoints[0],s=e.slowState.currentTool==="line"||e.slowState.currentTool==="lineBox";if(o===0)return~~a[0][0]===~~i[0]&&~~a[0][1]===~~i[1]?{selectedPoints:[0],points:a}:{selectedPoints:s?[]:[0],points:[i,...a]};let u=a[a.length-1];return~~u[0]===~~i[0]&&~~u[1]===~~i[1]?{selectedPoints:[a.length-1],points:a}:(e.line=null,{selectedPoints:s?[]:[a.length],points:[...a,i]})}};function BI(t,e){return Math.abs(t-e)}function aa(t,e){let n=BI(t[0],e[0]),i=BI(t[1],e[1]);return Math.sqrt(n**2+i**2)}function yR(t,e){return[t[0]-e[0],t[1]-e[1]]}function Fj(t,e){return t[0]*e[0]+t[1]*e[1]}function qI(t){return t[0]*t[0]+t[1]*t[1]}function lv(t,e){return qI(yR(t,e))}function FI(t,e,n,i){return i&&n&&(t<0?t=Math.max(t,-i.x-n.x):t>0&&(t=Math.min(t,i.x+i.width-n.x-n.width)),e<0?e=Math.max(e,-i.y-n.y):e>0&&(e=Math.min(e,i.y+i.height-n.y-n.height))),[t,e]}function GI(t,e){let n,i,a,o;var s=(S,E,C,R,x)=>{if(R<=C+1)return;let A=C,T=0,M=S*S;n=[E[C],E[R]],i=yR(n[1],n[0]);let z=qI(i),D,I,q,B,U;for(let W=C+1;W<R;W++)D=yR(E[W],n[0]),B=Fj(D,i),B<=0?U=lv(E[W],n[0]):z<=B?U=lv(E[W],n[1]):(q=B/z,I=[n[0][0]+q*i[0],n[0][1]+q*i[1]],U=lv(E[W],I)),!(U<=T)&&(A=W,T=U);T>M&&(x[A]=1,s(S,E,C,A,x),s(S,E,A,R,x))};let u=t.length,d=[],p,f,m,g,v=e*e;for(a=[],o=[],a[0]=t[0],p=f=1,g=0;p<u;p++)lv(t[p],t[g])<v||(a[f++]=t[p],g=p);for(g<u-1&&(a[f++]=t[u-1]),o[0]=o[f-1]=1,s(e,a,0,f-1,o),p=m=0;p<f;p++)o[p]&&(d[m++]=a[p]);return d}function Gj(t){if(t.length!==4)return!1;let e=1e-10,n=Math.min(...t.map(u=>u[0])),i=Math.max(...t.map(u=>u[0])),a=Math.min(...t.map(u=>u[1])),o=Math.max(...t.map(u=>u[1]));for(let u of t){let d=Math.abs(u[0]-n)<e||Math.abs(u[0]-i)<e,p=Math.abs(u[1]-a)<e||Math.abs(u[1]-o)<e;if(!d||!p)return!1}let s=[[n,a],[n,o],[i,a],[i,o]];for(let u of s)if(!t.some(d=>Math.abs(d[0]-u[0])<e&&Math.abs(d[1]-u[1])<e))return!1;return!0}function kI(t){let e=t.points.length,n=!1,i=[];if(e>1){t.iedges=[];let a=t.points[e-1];for(let o=0;o<e;o++){let s=a;if(a=t.points[o],a.length===6){let p=a[2],f=a[3],m=a[4],g=a[5];i.push([o,[a[0]+p,a[1]+f],[a[0]+m,a[1]+g]]),n=!0}let u=[a[0]-s[0],a[1]-s[1]],d=u[0]*u[0]+u[1]*u[1];d>0?t.iedges.push([u[0]/d,u[1]/d]):t.iedges.push([0,0])}}else e===1?t.iedges=[[0,0]]:t.iedges=null;t.isBezier=n,t.bezierLines=i}function kj(t,e){let n=1/0,i=null,a=-1,o=t.points.length;if(o<=1)return[null,n,a];let s=t.points[o-1];for(let u=0;u<o;u++){let d=s;s=t.points[u];let p=d[0]-e[0],f=d[1]-e[1],m=p*p+f*f;m<n&&(i=d,n=m,a=u===0?o-1:u-1)}return[i,n,a]}function uv(t){if(t.points.length>2){let e=Math.min(...t.points.map(o=>o[0])),n=Math.min(...t.points.map(o=>o[1])),i=Math.max(0,...t.points.map(o=>o[0])),a=Math.max(0,...t.points.map(o=>o[1]));t.boundingBox={x:e,y:n,width:i-e,height:a-n};return}t.boundingBox=null}function Yj(t){return t[0]*t[0]+t[1]*t[1]}function HI(t,e){let n=1/0,i=null,a=-1,o=[],s=t.points.length;if(s<=1)return[null,n,o,a];if(t.iedges===null&&(kI(t),!t.iedges))return[null,n,o,a];let u=t.points[s-1];for(let d=0;d<s;d++){let p=u;u=t.points[d];let f=(e[0]-p[0])*t.iedges[d][0]+(e[1]-p[1])*t.iedges[d][1],m;f<=0?m=p:f<1?m=[(1-f)*p[0]+f*u[0],(1-f)*p[1]+f*u[1]]:m=u;let g=Yj([m[0]-e[0],m[1]-e[1]]);g<n&&(i=m,n=g,o=[p,u],a=d===0?s-1:d-1)}return[i,n,o,a]}var yh={type:"bounding-box-corners",label:"Drag to resize",tools:["pointer","box"],modifiers:{Shift:"Maintain aspect ratio",Alt:"Scale from center",Meta:"Rotate"},isValid(t,e,n){if(e.transitionDirection=null,e.transitionRotate=!1,e.isOpen||!e.slowState.boxMode&&e.selectedPoints.length!==e.polygon.points.length||!e.polygon.boundingBox)return!1;let i=n.proximity*.5,a=n.proximity*5,o=t[0],s=e.polygon.boundingBox,u=s.x+i,d=s.x+s.width-i*2,p=s.y+i,f=s.y+s.height-i*2;if(o[0]>u&&o[0]<d&&o[1]>p&&o[1]<f)return!1;let m=[s.x+s.width,s.y+s.height],g=[s.x,s.y],v=[s.x+s.width,s.y],S=[s.x,s.y+s.height],E=["ne","nw","se","sw"],C={ne:v,nw:g,se:m,sw:S},R=[aa(t[0],v),aa(t[0],g),aa(t[0],m),aa(t[0],S)],x=Math.min(...R),A=R.indexOf(x),T=A===-1?null:E[A],M=T?C[T]:null;if(R[0]<a||R[1]<a||R[2]<a||R[3]<a){if(T){let[z,D]=o;(n.Meta||z>=M[0]-n.proximity*4&&D>=M[1]-n.proximity*4&&z<=M[0]+n.proximity*4&&D<=M[1]+n.proximity*4&&!(z>=M[0]-n.proximity*2&&D>=M[1]-n.proximity*2&&z<=M[0]+n.proximity*2&&D<=M[1]+n.proximity*2))&&(e.slowState.boxMode||(e.transitionRotate=!0)),e.transitionDirection=T}return!0}return!1},start(t,e,n){let i=t[0],a=e.polygon.boundingBox,o=[a.x+a.width,a.y+a.height],s=[a.x,a.y],u=[a.x+a.width,a.y],d=[a.x,a.y+a.height],p=["ne","nw","se","sw"],f=[aa(t[0],u),aa(t[0],s),aa(t[0],o),aa(t[0],d)],m=Math.min(...f),g=f.indexOf(m);e.transitionDirection=g!==-1?p[g]:null},transition(t,e,n){let i=e.polygon.boundingBox,a=e.transitionOrigin||t[0],[o,s]=t[0];if((n.Meta||e.transitionRotate)&&!e.slowState.boxMode){let x=[i.x+i.width/2,i.y+i.height/2],A=Math.atan2(a[1]-x[1],a[0]-x[0]),T=Math.atan2(s-x[1],o-x[0]),M=n.Shift,z=Math.PI/(n.Alt?4:12),D=T-A;M&&(D=Math.round(D/z)*z);let I=Math.cos(D),q=Math.sin(D),B=[];for(let U of e.polygon.points){let W=U[0]-x[0],it=U[1]-x[1],K=W*I-it*q,tt=W*q+it*I,ut=K+x[0],xt=tt+x[1];B.push(U.length===6?[ut,xt,U[2],U[3],U[4],U[5]]:[ut,xt])}e.transitionPoints=B,uv({points:B,iedges:null,boundingBox:null,isBezier:!1,bezierLines:[]}),e.transitionBoundingBox=e.polygon.boundingBox?{...e.polygon.boundingBox,rotation:D}:null;return}let u=e.transitionDirection,d=u.includes("e")?1:-1,p=u.includes("s")?1:-1,f=!!n.Alt,m;if(f)m=[i.x+i.width/2,i.y+i.height/2];else switch(u){case"se":m=[i.x,i.y];break;case"sw":m=[i.x+i.width,i.y];break;case"ne":m=[i.x,i.y+i.height];break;case"nw":m=[i.x+i.width,i.y+i.height];break}let g=f?i.width/2:i.width,v=f?i.height/2:i.height,S,E;if(n.Shift||e.slowState.fixedAspectRatio){let x=d*g,A=p*v,T=o-m[0],M=s-m[1],z=x*x+A*A,D=z>0?(T*x+M*A)/z:1;D=Math.max(D,.01),S=D,E=D}else{let x=0,A=0;switch(u){case"se":x=o-a[0],A=s-a[1];break;case"sw":x=a[0]-o,A=s-a[1];break;case"ne":x=o-a[0],A=a[1]-s;break;case"nw":x=a[0]-o,A=a[1]-s;break}f&&(x*=2,A*=2),S=(i.width+x)/i.width,E=(i.height+A)/i.height}let C=[];for(let x of e.polygon.points){let A=x[0]-m[0],T=x[1]-m[1],M=A*S,z=T*E,D=M+m[0],I=z+m[1];C.push(x.length===6?[D,I,x[2],x[3],x[4],x[5]]:[D,I])}e.transitionPoints=C;let R={points:C,iedges:null,boundingBox:null,bezierLines:[],isBezier:!1};uv(R),e.transitionBoundingBox=R.boundingBox},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,e.transitionRotate=!1,{points:i}}},Qj={type:"close-line-box",label:"Close Line Box",tools:["lineBox"],trigger:{type:"click"},isValid(t,e,n){return e.polygon.points.length===2&&e.lineBox!==null&&e.slowState.boxMode===!1},commit(t,e,n){return e.lineBox?{isOpen:!1,points:e.lineBox,tool:"pen"}:{}}},YI={type:"close-shape",label:"Close shape",tools:["pen","line"],trigger:{type:"click"},isValid(t,e,n){if(!e.isOpen||e.slowState.boxMode||e.selectedPoints.length!==1||e.polygon.points.length<2||n.Meta)return!1;let i=e.selectedPoints[0];if(i===0){let a=e.polygon.points[e.polygon.points.length-1];return aa(t[0],a)<n.proximity}if(i===e.polygon.points.length-1){let a=e.polygon.points[0];return aa(t[0],a)<n.proximity}return!1},commit(t,e,n){let i=e.selectedPoints[0];if(e.line=null,i===0)return{selectedPoints:[],points:e.polygon.points.slice(0).reverse(),isOpen:!1};if(i===e.polygon.points.length-1)return{selectedPoints:[],isOpen:!1}}},Xj={type:"close-shape-line",label:"Close shape line",trigger:{type:"click"},tools:["pen"],isValid(t,e,n){return!e.isOpen||e.slowState.boxMode||e.polygon.points.length<3||!e.closestLinePoint||e.closestLineDistance>=n.proximity*2?!1:(e.closestLineIndex===0||(e.closestLineIndex,e.polygon.points.length-2),!0)},commit(t,e,n){return YI.commit(t,e,n)}},QI={type:"split-line",label:"Split line",tools:["pen"],isValid(t,e,n){let i=e.slowState.currentTool==="line";return n.Meta||i?!1:!!(e.closestLinePoint&&e.closestLineDistance<n.proximity)},transition(t,e,n){let i=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3,e.closestLineIndex),i=xh(i,e));let a=e.transitionOrigin||i,[o,s]=i,u=e.polygon.points,d=[],p=o-a[0],f=s-a[1];for(let m=0;m<u.length;m++)if(d.push(u[m]),e.closestLineIndex===m){let g=e.closestLinePoint;d.push([g[0]+p,g[1]+f])}e.transitionPoints=d},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{selectedPoints:[e.closestLineIndex+1],points:i}}},Kj={type:"cut-line",label:"Add new point",tools:["pen"],trigger:{type:"click"},modifiers:{Shift:"Cut line"},isValid(t,e,n){return e.slowState.boxMode?!1:QI.isValid(t,e,n)},commit(t,e,n){if(n.Shift&&!e.isOpen){let o=e.polygon.points,s=[],u=[],d=!1;for(let p=0;p<o.length;p++)d?u.push(o[p]):s.push(o[p]),e.closestLineIndex===p&&(d=!0);return{isOpen:!0,points:[...u,...s]}}let i=e.polygon.points,a=[];for(let o=0;o<i.length;o++)if(a.push(i[o]),e.closestLineIndex===o){let s=e.closestLinePoint;a.push(s)}return{points:a}}},Zj={type:"delete-point",label:"Delete point",tools:["pointer","pen","lineBox","line"],trigger:{type:"key",key:"Backspace"},isValid(t,e,n){return e.selectedPoints.length>0&&e.slowState.canDelete},commit(t,e,n){let i=e.polygon.points.filter((a,o)=>!e.selectedPoints.includes(o));return{isOpen:i.length<3,selectedPoints:[],points:i,tool:e.slowState.boxMode?"box":void 0}}},Jj={type:"deselect-bounding-box",label:"Deselect bounding box",tools:["pointer"],trigger:{type:"click"},isValid(t,e,n){return e.slowState.canDeselect&&!e.isOpen&&!e.slowState.boxMode&&e.selectedPoints.length>2&&e.selectedPoints.length===e.polygon.points.length},commit(t,e,n){return e.slowState.lastCreationTool==="pen"?{selectedPoints:[],tool:"pen"}:{selectedPoints:[]}}},Wj={type:"deselect-draw",label:"Deselect draw",trigger:{type:"key",key:"Escape"},tools:["pointer","pen","box","lineBox","stamp","hand","pencil"],isValid(t,e,n){return e.slowState.canDeselect},commit(t,e,n){return{selectedPoints:[]}}},$j={type:"deselect-points",label:"Deselect points",trigger:{type:"click"},tools:["pointer","pen"],isValid(t,e,n){return e.selectedPoints.length>0&&e.slowState.canDeselect},commit(t,e,n){return{selectedPoints:[]}}},tq={type:"draw-shape",label:"Draw shape",tools:["pen","pencil"],modifiers:{Alt:"Freehand drawing"},isValid(t,e,n){let i=n.Alt||e.slowState.currentTool==="pencil";return!!(e.isOpen&&(e.line||e.polygon.points.length===0)&&i)},start(t,e,n){e.transitionDraw=[]},transition(t,e,n){let i=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(i,e,3),i=xh(i,e)),e.transitionDraw.push(i)},commit(t,e,n){let i=e.slowState.currentTool==="pencil"?e.scale*1.5:e.scale*3,a=GI(e.transitionDraw,i);if(e.transitionDraw=[],a.length===0)return;let o=!0,s=3,u=e.selectedPoints[0],d=[...e.polygon.points,...a],p=d[0],f=d[d.length-1];u===0&&(p=d[d.length-1],f=d[0],d=[...a,...e.polygon.points.slice(0).reverse()]);let m=[d.length-1],g=aa(p,f)/n.proximity<s,v="";return g&&(o=!1,v="pointer",m=d.map((S,E)=>E)),{isOpen:o,tool:v||void 0,points:d,selectedPoints:m}}},eq={type:"move-line",label:"Move line",tools:["line"],isValid(t,e,n){return e.slowState.currentTool!=="line"||e.slowState.boxMode?!1:!!(e.closestLinePoint&&e.closestLineDistance<n.proximity)},transition(t,e){let n=e.transitionOrigin,i=e.polygon.boundingBox,a=t[0],[o,s]=FI(a[0]-n[0],a[1]-n[1],i,e.slowState.bounds);e.transitionPoints=e.polygon.points.map(u=>u.length===6?[u[0]+o,u[1]+s,u[2],u[3],u[4],u[5]]:[u[0]+o,u[1]+s])},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},nq={type:"move-point",label:"Move point",tools:["pointer","pen","line"],modifiers:{Shift:"Constrain to axis"},isValid(t,e,n){if(n.Alt)return!1;let i=n.proximity,[a,o]=t[0],s=e.polygon.points,u=e.selectedPoints;for(let d=0;d<s.length;d++){let p=s[d],f=p[0]-a,m=p[1]-o;if(f*f+m*m<i*i)return!0}return!1},start(t,e,n){if(e.slowState.boxMode)return yh.start?yh.start(t,e,n):void 0;if(e.selectedPoints.length<2){let[i,a,o]=kj(e.polygon,t[0]);return{selectedPoints:[o]}}if(e.slowState.closestPoint&&!e.selectedPoints.includes(e.slowState.closestPoint))return{selectedPoints:[e.slowState.closestPoint]}},transition(t,e,n){if(e.slowState.boxMode)return yh.transition(t,e,n);let i=e.transitionOrigin,a=t[0];!n.Shift&&e.slowState.snapEnabled&&(vl(a,e,3),a=xh(a,e));let[o,s]=a,u=e.polygon.points,d=e.selectedPoints;if(e.slowState.closestPoint){let g=e.slowState.closestPoint;d.indexOf(g)===-1&&(d=[g])}let p=[],f=o-i[0],m=s-i[1];n.Shift&&!e.slowState.snapEnabled&&(Math.abs(f)>Math.abs(m)?m=0:f=0);for(let g=0;g<u.length;g++){let v=d.indexOf(g)!==-1,S=u[g];v?S.length===6?p.push([S[0]+f,S[1]+m,S[2],S[3],S[4],S[5]]):p.push([S[0]+f,S[1]+m]):p.push(S)}e.transitionPoints=p},commit(t,e,n){if(e.slowState.boxMode)return yh.commit(t,e,n);let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},XI={type:"move-bounding-box",label:"Move bounding box",tools:["pointer"],isValid(t,e,n){let i=e.polygon.points.length,a=i===e.selectedPoints.length,o=e.polygon.boundingBox;if(i<1||!a||!o)return!1;let s=t[0],u=s[0],d=s[1];return u>=o.x&&u<=o.x+o.width&&d>=o.y&&d<=o.y+o.height},transition(t,e,n){let i=e.slowState.bounds,a=e.transitionOrigin,o=e.polygon.boundingBox,s=t[0],[u,d]=FI(s[0]-a[0],s[1]-a[1],o,i);e.transitionPoints=e.polygon.points.map(p=>p.length===6?[p[0]+u,p[1]+d,p[2],p[3],p[4],p[5]]:[p[0]+u,p[1]+d]),o&&(e.transitionBoundingBox={x:o.x+u,y:o.y+d,width:o.width,height:o.height})},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},KI={type:"move-shape",label:"Move shape",tools:["pointer","hand","box"],isValid(t,e,n){return n.Shift?!1:e.slowState.pointerInsideShape&&(!e.isOpen||e.line===null)},transition(t,e,n){XI.transition(t,e,n)},commit(t,e,n){let i=e.transitionPoints;return e.transitionPoints=null,e.transitionBoundingBox=null,{points:i}}},iq={type:"nudge-right",label:"Nudge right",trigger:{type:"key",key:"ArrowRight"},tools:["pointer","pen"],modifiers:{Shift:"Nudge right more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>o.includes(u)?[s[0]+(n.Shift?4*i:1),s[1]]:s)}}},aq={type:"nudge-left",label:"Nudge left",trigger:{type:"key",key:"ArrowLeft"},tools:["pointer","pen"],modifiers:{Shift:"Nudge left more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>o.includes(u)?[Math.max(0,s[0]-(n.Shift?4*i:1)),s[1]]:s)}}},oq={type:"nudge-up",label:"Nudge up",trigger:{type:"key",key:"ArrowUp"},tools:["pointer","pen"],modifiers:{Shift:"Nudge up more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>{if(o.includes(u)){let d=s[0],p=Math.max(0,s[1]-(n.Shift?4*i:1));return s.length===6?[d,p,s[2],s[3],s[4],s[5]]:[d,p]}return s})}}},rq={type:"nudge-down",label:"Nudge down",trigger:{type:"key",key:"ArrowDown"},tools:["pointer","pen"],modifiers:{Shift:"Nudge down more"},isValid(t,e,n){return e.selectedPoints.length>0},commit(t,e,n){let i=n.proximity*.3,a=e.polygon.points,o=e.selectedPoints;return{points:a.map((s,u)=>{let d=s[0],p=Math.max(0,s[1]+(n.Shift?4*i:1));return o.includes(u)?s.length===6?[d,p,s[2],s[3],s[4],s[5]]:[d,p]:s})}}},lq={type:"select-multiple-points",label:"Drag to select multiple points",tools:["pointer","pen"],isValid(t,e,n){return e.slowState.currentTool==="line"&&e.polygon.points.length>=2?!0:!(e.slowState.boxMode||e.line)},transition(t,e,n){if(!e.transitionOrigin)return;let i=e.transitionOrigin[0],a=e.transitionOrigin[1],o=t[0][0],s=t[0][1],u=Math.min(i,o),d=Math.min(a,s),p=Math.abs(i-o),f=Math.abs(a-s);e.selectionBox={x:u,y:d,width:p,height:f}},commit(t,e,n){if(e.selectionBox){let i=[];n.Shift&&i.push(...e.selectedPoints);for(let a=0;a<e.polygon.points.length;a++){let o=e.polygon.points[a];o[0]>=e.selectionBox.x&&o[0]<=e.selectionBox.x+e.selectionBox.width&&o[1]>=e.selectionBox.y&&o[1]<=e.selectionBox.y+e.selectionBox.height&&i.push(a)}return{selectedPoints:i}}}},UI=10,sq={type:"select-point",label:"Select point",trigger:{type:"click"},tools:["pointer","pen","line"],modifiers:{Shift:"Add to selection"},isValid(t,e,n){let i=-1;if(e.selectedPoints.length===1){let u=e.selectedPoints[0];u===0&&(i=e.polygon.points.length-1),u===e.polygon.points.length-1&&(i=0)}if(e.slowState.boxMode)return!1;let[a,o]=t[0],s=e.polygon.points;for(let u=0;u<s.length;u++){let d=s[u],p=d[0]-a,f=d[1]-o;if(p*p+f*f<UI*UI)return i!==u}return!1},commit(t,e,n){let i=n.Shift?[...e.selectedPoints]:[],[a,o]=t[0],s=e.polygon.points.map((u,d)=>{let p=u[0]-a,f=u[1]-o;return[p*p+f*f,d]}).sort((u,d)=>u[0]-d[0])[0];if(e.line=null,!i.includes(s[1]))i.push(s[1]);else if(n.Shift)return{selectedPoints:i.filter(u=>u!==s[1])};return{selectedPoints:i}}},uq={type:"select-shape",label:"Select shape",trigger:{type:"click"},tools:["pointer","pen"],isValid(t,e,n){return KI.isValid(t,e,n)},commit(t,e,n){return{tool:"pointer",selectedPoints:e.polygon.points.map((i,a)=>a)}}},cq={type:"snap",label:"Smart snap",tools:["pointer","pen","line"],modifiers:{Shift:"Disable snapping"},isValid(t,e,n){return n.Shift||!e.slowState.snapEnabled||e.slowState.transitioning&&e.slowState.transitionIntentType!=="snap"||!e.slowState.snapToPoints&&!e.slowState.snapToLines&&!e.slowState.snapToIntersections?!1:vl(t[0],e,0)!==null},start(t,e,n){vl(t[0],e,5)},transition(t,e,n){let i=vl(t[0],e,5);i&&(e.pointer=i)},commit(t,e,n){jI(e)}},fq={type:"stamp-fixed-size-shape",label:"Stamp fixed size shape",trigger:{type:"click"},tools:["stamp"],isValid(t,e,n){return e.slowState.selectedStamp!==null},commit(t,e,n){let i=t[0],a=e.slowState.selectedStamp;if(!i||!a)return;let o=i[0],s=i[1],u=[],d=Math.min(...a.points.map(E=>E[0])),p=Math.min(...a.points.map(E=>E[1])),f=Math.max(...a.points.map(E=>E[0]))-d,m=Math.max(...a.points.map(E=>E[1]))-p,g=4*n.proximity,v=f/g;m/g>v&&(v=m/g),o-=f/v/2,s-=m/v/2;let S=0;for(let E of a.points)u.push([o+(E[0]-d)/v,s+(E[1]-p)/v]),S++;return{isOpen:!1,points:u}}},ZI={id:"square",open:!1,points:[[0,0],[0,100],[100,100],[100,0]]},dq={id:"triangle",open:!1,points:[[50,0],[0,100],[100,100]]},hq={id:"pentagon",open:!1,points:[[0,0],[0,100],[100,100],[100,0],[50,-50]]},pq={id:"hexagon",open:!1,points:[[50,0],[0,25],[0,75],[50,100],[100,75],[100,25]]},mq={id:"circle",open:!1,points:[[948.949,571.107],[954.545,500],[954.545,500],[948.949,428.893],[948.949,428.893],[932.298,359.538],[932.298,359.538],[905.003,293.641],[905.003,293.641],[867.735,232.825],[867.735,232.825],[821.412,178.588],[821.412,178.588],[767.175,132.265],[767.175,132.265],[706.359,94.997],[706.359,94.997],[640.462,67.702],[640.462,67.702],[571.107,51.051],[571.107,51.051],[500,45.455],[500,45.455],[428.893,51.051],[428.893,51.051],[359.538,67.702],[359.538,67.702],[293.641,94.997],[293.641,94.997],[232.825,132.265],[232.825,132.265],[178.588,178.588],[178.588,178.588],[132.265,232.825],[132.265,232.825],[94.997,293.641],[94.997,293.641],[67.702,359.538],[67.702,359.538],[51.051,428.893],[51.051,428.893],[45.455,500],[45.455,500],[51.051,571.107],[51.051,571.107],[67.702,640.462],[67.702,640.462],[94.997,706.359],[94.997,706.359],[132.265,767.175],[132.265,767.175],[178.588,821.412],[178.588,821.412],[232.825,867.735],[232.825,867.735],[293.641,905.003],[293.641,905.003],[359.538,932.298],[359.538,932.298],[428.893,948.949],[428.893,948.949],[500,954.545],[500,954.545],[571.107,948.949],[571.107,948.949],[640.462,932.298],[640.462,932.298],[706.359,905.003],[706.359,905.003],[767.175,867.735],[767.175,867.735],[821.412,821.412],[821.412,821.412],[867.735,767.175],[867.735,767.175],[905.003,706.359],[905.003,706.359],[932.298,640.462],[932.298,640.462],[948.949,571.107]]},gq={type:"stamp-shape",label:"Stamp shape",tools:["stamp","box"],modifiers:{Shift:"Maintain aspect ratio"},isValid(t,e,n){return!(e.polygon.points.length&&!e.slowState.canDelete)},start(t,e,n){let i=t[0],a=e.slowState.selectedStamp||ZI;if(i){let o=i[0],s=i[1],u=[],d=Math.min(...a.points.map(E=>E[0])),p=Math.min(...a.points.map(E=>E[1])),f=Math.max(...a.points.map(E=>E[0]))-d,m=Math.max(...a.points.map(E=>E[1]))-p,g=f/32;m/32>g&&(g=m/32);let v=[],S=0;for(let E of a.points)v.push(S),u.push([o+(E[0]-d)/g,s+(E[1]-p)/g]),S++;return e.transitionPoints=u,{isOpen:!1,selectedPoints:v,points:u}}},transition(t,e,n){if(!e.transitionOrigin)return;let i=e.polygon.boundingBox,a=[i.x,i.y],o=e.transitionOrigin||t[0],[s,u]=t[0],d=e.transitionOrigin[0],p=e.transitionOrigin[1],f=t[0][0],m=t[0][1],g=f-d-32,v=m-p-32;if(n.Shift||e.slowState.fixedAspectRatio){let x=i.width/i.height;Math.abs(i.width/g)>Math.abs(i.height/v)?v=g/x:g=v*x}let S=(i.width+g)/i.width,E=(i.height+v)/i.height,C=[];for(let x of e.polygon.points){let A=x[0]-a[0],T=x[1]-a[1],M=A*S,z=T*E,D=M+a[0],I=z+a[1];if(x.length===6){C.push([D,I,x[2],x[3],x[4],x[5]]);continue}C.push([D,I])}e.transitionPoints=C;let R={points:C,iedges:null,boundingBox:null,bezierLines:[],isBezier:!1};uv(R),e.transitionBoundingBox=R.boundingBox},commit(t,e,n){return{isOpen:!1,points:e.transitionPoints,tool:"pointer"}}},yq={type:"toggle-snap",label:"Toggle snapping",trigger:{type:"key",key:"`"},tools:["pointer","pen","line","lineBox","box","stamp"],modifiers:{},isValid(t,e,n){return!0},commit(t,e,n){let i=!e.slowState.snapEnabled;return e.slowState.snapEnabled=i,i||(e.snapTargets=[],e.activeSnapGuides=[],e.isSnapping=!1,e.snapPoint=null),{}}};function JI(t={}){let e=t.proximityThreshold||10,n=t.closestLinePointFill||{selected:"#fff",unselected:"rgba(255, 255, 255, .5)"},i=t.closestLinePointStroke||{selected:"#000",unselected:"rgba(0, 0, 0, .5)"};function a(S,E,C){S&&E.transitionBoundingBox&&C.transitioning&&(S.setAttribute("x",""+E.transitionBoundingBox.x),S.setAttribute("y",""+E.transitionBoundingBox.y),S.setAttribute("width",""+E.transitionBoundingBox.width),S.setAttribute("height",""+E.transitionBoundingBox.height))}function o(S,E,C){S&&E.polygon.boundingBox&&C.showBoundingBox&&(S.setAttribute("x",""+E.polygon.boundingBox.x),S.setAttribute("y",""+E.polygon.boundingBox.y),S.setAttribute("width",""+E.polygon.boundingBox.width),S.setAttribute("height",""+E.polygon.boundingBox.height))}function s(S,E,C){if(S&&E.polygon.boundingBox&&C.showBoundingBox){let R=E.transitionBoundingBox||E.polygon.boundingBox;R.rotation?(S.style.transformOrigin=`${R.x+R.width/2}px ${R.y+R.height/2}px`,S.style.transform=`rotate(${Math.round(100*R.rotation*(180/Math.PI))/100}deg)`):S.style.transform="",S.setAttribute("points",[[R.x,R.y],[R.x+R.width,R.y],[R.x+R.width,R.y+R.height],[R.x,R.y+R.height]].map(x=>x.join(",")).join(" "))}}function u(S,E,C){S&&E.transitionPoints&&C.transitioning&&S.setAttribute("points",E.transitionPoints.map(R=>R.join(",")).join(" "))}function d(S,E,C){S&&E.closestLinePoint&&C.hasClosestLine&&(S.setAttribute("cx",""+E.closestLinePoint[0]),S.setAttribute("cy",""+E.closestLinePoint[1]),S.setAttribute("fill",E.closestLineDistance<e?n.selected:n.unselected),S.setAttribute("stroke",E.closestLineDistance<e?i.selected:i.unselected))}function p(S,E,C){S&&E.closestLinePoint&&C.hasClosestLine&&S.setAttribute("transform",`translate(${E.closestLinePoint[0]}, ${E.closestLinePoint[1]})`)}function f(S,E,C){S&&E.selectionBox&&C.transitionIntentType==="select-multiple-points"&&(S.setAttribute("x",""+E.selectionBox.x),S.setAttribute("y",""+E.selectionBox.y),S.setAttribute("width",""+E.selectionBox.width),S.setAttribute("height",""+E.selectionBox.height))}function m(S,E,C){if(S&&E.pointer&&E.selectedPoints.length)if(C.actionIntentType==="close-shape"||C.actionIntentType==="close-shape-line"){let R=E.polygon.points[0],x=E.polygon.points[E.polygon.points.length-1];R&&x&&S.setAttribute("points",`${R.join(",")} ${x.join(",")}`)}else if(E.line)S.setAttribute("points",`${E.line[0].join(",")} ${E.line[1].join(",")}`);else{let R=E.polygon.points[E.selectedPoints[0]];R&&R.length&&S.setAttribute("points",`${E.pointer[0]},${E.pointer[1]} ${R.join(",")}`)}}function g(S,E,C,R=3){S&&E.transitionDraw.length&&S.setAttribute("points",GI(E.transitionDraw,R*E.scale).map(x=>x.join(",")).join(" "))}function v(S,E){S&&E.lineBox&&S.setAttribute("points",E.lineBox.map(C=>C.join(",")).join(" "))}return{updateTransitionBoundingBox:a,updateClosestLinePointTransform:p,updateBoundingBoxPolygon:s,updateBoundingBox:o,updateTransitionShape:u,updateClosestLinePoint:d,updateSelectBox:f,updatePointLine:m,updateDrawPreview:g,updateLineBox:v}}var vq=typeof window<"u"?window.requestAnimationFrame:t=>setTimeout(t,16),Sq=typeof window<"u"?window.cancelAnimationFrame:t=>clearTimeout(t);function VI(t){return!["hand","pointer"].includes(t)}var Sh=[yh,nq,eq,QI,KI,XI,tq,lq,gq,cq],bq=Sh.length,vh=[Qj,fq,YI,sq,Xj,Kj,qj,uq,Jj,$j],jc=[Wj,iq,aq,oq,rq,Zj,yq],bh={};Sh.forEach(t=>{bh[t.type]=t});vh.forEach(t=>{bh[t.type]=t});jc.forEach(t=>{bh[t.type]=t});var sv=10;function WI(t,e){let n=t?.fixedAspectRatio||!1,i=t?.tool||"pointer",a=t?.keyboardShortcutMapping||{},o=typeof t?.keyboardShortcutsEnabled=="boolean"?t.keyboardShortcutsEnabled:!0,s=t?.enabledTools||["pointer","pen","box","lineBox","stamp","hand","line","pencil"],u=t?.emitter||Lj(),d={V:"pointer",P:"pen",B:"box",L:"lineBox",S:"stamp",H:"hand",N:"line",D:"pencil",...t?.toolMap||{}},p={shapeId:t?.id||null,noShape:t===null,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:sv},bounds:t?.bounds||null,showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},pointerInsideShape:!1,closestPoint:null,selectedStamp:null,transitionModifiers:null,boxMode:!1,bezierLines:[],fixedAspectRatio:n,isToolSwitchingLocked:!1,canDeselect:!1,canDelete:!0,cursor:"default",enabledTools:s,currentTool:i,lastCreationTool:VI(i)?i:null,snapEnabled:!1,snapToPoints:!0,snapToLines:!0,snapToIntersections:!0,snapToGrid:!1,snapToParallel:!0},f={isOpen:typeof t?.open=="boolean"?t.open:!1,polygon:{points:t?.points||[],iedges:null,boundingBox:null,isBezier:null,bezierLines:[]},scale:1,selectedPoints:[],pointer:null,line:null,lineBox:null,transitionPoints:null,transitionOrigin:null,transitionBoundingBox:null,closestLinePoint:null,closestLineDistance:0,closestLineIndex:-1,transitionRotate:!1,transitionDirection:null,transitionBezierLine:null,selectionBox:null,slowState:p,transitionDraw:[],isPanning:!1,panStart:null,snapTargets:[],activeSnapGuides:[],snapThreshold:15,isSnapping:!1,snapPoint:null},m={startTime:0,time:0,shouldUpdate:!1,nextSlowState:null,renderFunc:()=>{},setStateFunc:()=>{},animationFrame:0,actionIntent:null,transitionIntent:null,undoStack:[],undoStackPointer:-1},g={isPressed:!1,isClicking:!1,lastPress:null,pressTimeout:0,noTransition:!1};function v(P){kI(P),P.isBezier&&S({bezierLines:P.bezierLines}),uv(P),m.shouldUpdate=!0}v(f.polygon);function S(P){let Q=Object.keys(P);if(Q.length===0)return;let dt=m.nextSlowState||f.slowState,ht=!1;if(Q.length===1&&Q[0]==="validIntentKeys"&&dt.validIntentKeys){let vt=Object.keys(dt.validIntentKeys),j=Object.keys(P.validIntentKeys||{});if(vt.length===j.length){for(let L of vt)dt.validIntentKeys[L]!==P.validIntentKeys[L]&&(ht=!0);if(!ht)return}}if(P.selectedPoints){let vt=Array.from(dt.selectedPoints||[]),j=Array.from(P.selectedPoints||[]);if(vt.length===j.length){for(let L of vt)if(!j.includes(L)){ht=!0;break}}else ht=!0;ht||delete P.selectedPoints}if(!(Q.length===1&&Q.includes("hasClosestLine")&&P.hasClosestLine===dt.hasClosestLine)){if(m.nextSlowState){m.nextSlowState={...m.nextSlowState,...P};return}m.nextSlowState={...f.slowState,...P}}}function E(P,Q=!1){m.time+=P,C(),R(),q(),I(),D(),U(),it(),W(),z(),m.renderFunc(f,f.slowState,P),!Q&&(m.animationFrame=vq(E))}function C(){if(m.shouldUpdate&&(e({id:f.slowState.shapeId||void 0,open:f.isOpen,points:f.polygon.points}),m.shouldUpdate=!1),m.nextSlowState&&m.nextSlowState!==f.slowState){let P=Object.keys(f.slowState),Q={},dt={},ht=!1;for(let vt of P){let j=f.slowState[vt],L=m.nextSlowState[vt];j!==L?(ht=!0,Q[vt]=L,dt[vt]=L):Q[vt]=j}ht&&(f.slowState=Q,m.nextSlowState=null,m.setStateFunc(f.slowState),t?.customSetState?.(dt))}}function R(){let P=!f.slowState.noShape&&f.polygon.points.length>2&&f.selectedPoints.length===f.polygon.points.length&&!f.isOpen&&(f.slowState.currentTool==="pointer"||f.slowState.currentTool==="box");f.slowState.showBoundingBox!==P&&S({showBoundingBox:P})}function x(P){f.selectedPoints=P.selectedPoints,S({selectedPoints:P.selectedPoints}),f.polygon.points=P.points,f.polygon.boundingBox=null,f.polygon.iedges=null,f.polygon.isBezier=null,v(f.polygon),S({closestPoint:null}),(P.isOpen===!0||P.isOpen===!1)&&(f.isOpen=P.isOpen,m.shouldUpdate=!0)}function A(){if(m.undoStackPointer===-1)return;m.undoStackPointer--;let P=m.undoStack[m.undoStackPointer];x(P)}function T(){if(m.undoStackPointer===m.undoStack.length-1)return;m.undoStackPointer++;let P=m.undoStack[m.undoStackPointer];x(P)}function M(P){m.undoStackPointer++,m.undoStack[m.undoStackPointer]=P,m.undoStack.length>15&&(m.undoStack.shift(),m.undoStackPointer--)}function z(){S({boxMode:Gj(f.polygon.points||[])})}function D(){if(!f.pointer||f.slowState.noShape){S({pointerInsideShape:!1});return}let[P,Q]=f.pointer,dt=f.polygon.points,ht=f.polygon.boundingBox;if(!ht){S({pointerInsideShape:!1});return}if(P<ht.x||P>ht.x+ht.width||Q<ht.y||Q>ht.height+ht.y){S({pointerInsideShape:!1});return}let vt=!1;for(let j=0,L=dt.length-1;j<dt.length;L=j++)dt[j][1]>Q!=dt[L][1]>Q&&P<(dt[L][0]-dt[j][0])*(Q-dt[j][1])/(dt[L][1]-dt[j][1])+dt[j][0]&&(vt=!vt);S({pointerInsideShape:vt})}function I(){if(f.slowState.noShape||!f.pointer||f.slowState.transitioning||!f.polygon.points||f.polygon.points.length===0)return;let[P,Q]=f.pointer,dt=f.polygon.points.map((ht,vt)=>{let j=ht[0]-P,L=ht[1]-Q;return[j*j+L*L,vt]}).sort((ht,vt)=>ht[0]-vt[0]);dt.length&&S({closestPoint:dt[0][1]})}function q(){if(!f.pointer||f.slowState.transitioning)return;if(f.slowState.noShape){f.closestLineIndex=-1,f.closestLinePoint=null,f.closestLineDistance=0;return}let[P,Q,dt,ht]=HI(f.polygon,f.pointer),vt=ee()*ee();Q<vt&&(!f.isOpen||f.slowState.currentTool==="line"||f.polygon.points.length-1!==ht)?(f.closestLinePoint=P,f.closestLineDistance=Math.sqrt(Q),f.closestLineIndex=ht,S({hasClosestLine:!0})):(f.closestLinePoint=null,f.closestLineDistance=0,f.closestLineIndex=-1,S({hasClosestLine:!1}))}function B(P,Q=null){let dt={},ht=Q||(f.pointer?[f.pointer[0],f.pointer[1]]:null);for(let vt of[...Sh,...vh,...jc])vt.tools.includes(P)&&(ht&&vt.isValid([ht],f,K())?dt[vt.type]=vt.label:ht||(dt[vt.type]=vt.label));return dt}function U(){if(f.slowState.transitioning||f.slowState.noShape||!f.pointer||!Array.isArray(f.pointer)||f.pointer.length<2||typeof f.pointer[0]!="number"||typeof f.pointer[1]!="number")return;let P=f.slowState.currentTool;Mn(P);let Q=!1;for(let vt=0;vt<bq;vt++){let j=Sh[vt];if(j.tools.includes(P)&&j.isValid(g.isPressed?[g.lastPress]:[f.pointer],f,K())){if(m.transitionIntent===j){Q=!0;break}S({transitionIntentType:j.type}),m.transitionIntent=j,Q=!0;break}}Q||m.transitionIntent&&(S({transitionIntentType:null}),m.transitionIntent=null);let dt=!1;for(let vt=0;vt<vh.length;vt++){let j=vh[vt];if(j.tools.includes(P)&&ut(j)){if(m.actionIntent===j){dt=!0;break}S({actionIntentType:j.type}),m.actionIntent=j,dt=!0;break}}dt||m.actionIntent&&(m.actionIntent=null,S({actionIntentType:null}));let ht={};for(let vt=0;vt<jc.length;vt++){let j=jc[vt];j.trigger.type==="key"&&ut(j)&&(ht[j.trigger.key]=j.label)}S({validIntentKeys:ht})}function W(){let P=f.pointer;if(f.lineBox=null,!P||lt()||f.polygon.points.length!==2)return;let[Q,dt]=P,[ht,vt]=f.polygon.points[0],[j,L]=f.polygon.points[1],[$,at,Nt,Lt]=HI(f.polygon,P);if(!$)return;let jt=Q-$[0],G=dt-$[1],Z=[ht,vt],mt=[j,L],yt=[mt[0]+jt,mt[1]+G],pt=[Z[0]+jt,Z[1]+G];f.lineBox=[Z,mt,yt,pt]}function it(){let P=f.pointer;if(!P||!lt()||f.slowState.noShape)return;let Q=f.selectedPoints[0],dt=P;if(f.slowState.snapEnabled&&!f.slowState.modifiers.Shift?(vl(P,f,5),dt=xh(P,f)):jI(f),f.line=[f.polygon.points[Q],dt],f.slowState.modifiers.Shift){let ht=0;if(f.polygon.points.length>1){let G=f.selectedPoints[0];G===0?ht=Math.atan2(f.polygon.points[1][1]-f.polygon.points[0][1],f.polygon.points[1][0]-f.polygon.points[0][0]):ht=Math.atan2(f.polygon.points[G-1][1]-f.polygon.points[G][1],f.polygon.points[G-1][0]-f.polygon.points[G][0])}let vt=P[0]-f.polygon.points[Q][0],j=P[1]-f.polygon.points[Q][1],L=Math.atan2(j,vt),$=Math.PI/4,at=Math.round((L-ht)/$)*$+ht,Nt=aa(f.polygon.points[Q],P),Lt=Math.cos(at)*Nt+f.polygon.points[Q][0],jt=Math.sin(at)*Nt+f.polygon.points[Q][1];f.line[1]=[Lt,jt]}}function K(){return f.slowState.modifiers}function tt(P){let Q=P.commit([g.lastPress],f,K());u.emit("polygons.end-transition",{transitionIntent:P.type,response:Q}),Q&&(Q.tool&&$t(Q.tool),Q.selectedPoints&&(f.selectedPoints=Q.selectedPoints,S({selectedPoints:Q.selectedPoints})),Q.points&&(f.polygon.points=Q.points,f.polygon.boundingBox=null,f.polygon.iedges=null,f.polygon.isBezier=null,f.transitionBoundingBox=null,v(f.polygon),S({closestPoint:null,selectedStamp:null})),(Q.isOpen===!0||Q.isOpen===!1)&&(f.isOpen=Q.isOpen,m.shouldUpdate=!0),M({isOpen:f.isOpen,points:f.polygon.points,selectedPoints:f.selectedPoints}),Mn(f.slowState.currentTool)),S({transitionModifiers:null})}function ut(P){if(!P.tools.includes(f.slowState.currentTool))return!1;let Q=[];return f.pointer&&Array.isArray(f.pointer)&&f.pointer.length>=2&&typeof f.pointer[0]=="number"&&typeof f.pointer[1]=="number"&&(Q=[f.pointer]),P.isValid(Q,f,K())}function xt(P){P==="Delete"&&(P="Backspace");let Q=f.slowState.currentTool;for(let dt of jc)if(!(dt.trigger.type!=="key"||dt.trigger.key!==P)&&dt.tools.includes(Q)&&ut(dt))return tt(dt),!0;return!1}function lt(){let P=f.polygon.points,Q=f.pointer;if(!f.isOpen||!P.length||f.selectedPoints.length!==1||!Q)return!1;let dt=f.selectedPoints[0],ht=dt===0,vt=dt===P.length-1;return ht||vt}let ft={reset(){S({modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:sv}})},getForType(P){let Q={};if(!P)return Q;let dt=bh[P];return!dt||!dt.modifiers?Q:dt.modifiers},set(P){P!=="Shift"&&P!=="Alt"&&P!=="Meta"||S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,[P]:!0}})},unset(P){P!=="Shift"&&P!=="Alt"&&P!=="Meta"||S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,[P]:!1}})}},Rt={set(P){let Q=P?P.id==="square"||P.id==="rectangle":!1;S({selectedStamp:P,boxMode:Q})},clear(){Rt.set(null)},square(){Rt.set(ZI)},triangle(){Rt.set(dq)},pentagon(){Rt.set(hq)},hexagon(){Rt.set(pq)},circle(){Rt.set(mq)}},X={down(P){let Q=a[P]||P;if(Q==="Shift"||Q==="Alt"||Q==="Meta")return ft.set(Q),!0;if(Q==="Control")return ft.set("Meta"),!0;if(Q==="z"&&f.slowState.modifiers.Meta)return f.slowState.modifiers.Shift?T():A(),!0;if(!f.slowState.modifiers.Meta&&!f.slowState.modifiers.Alt&&!f.slowState.transitioning){if(!o)return;let dt=Q.toUpperCase();if(d[dt]){let ht=d[dt];return $t(ht),!0}}return xt(Q)},up(P){let Q=a[P]||P;if(Q==="Control"){ft.unset("Meta");return}Q!=="Shift"&&Q!=="Alt"&&Q!=="Meta"||ft.unset(Q)}};function Ct(P){f.scale=P,S({modifiers:{...m.nextSlowState?.modifiers||f.slowState.modifiers,proximity:P*sv}})}function ee(){return sv*f.scale}let ce={set:P=>{m.renderFunc=P},start:(P,Q)=>{m.startTime=performance.now(),m.time=0,P&&(m.renderFunc=P),Q&&(Q(f.slowState),m.setStateFunc=Q),E(0)},stop:()=>{m.animationFrame&&(Sq(m.animationFrame),m.animationFrame=0)},step:(P=16)=>{E(P)}};function le(P){if(!P||!Array.isArray(P)||P.length===0)return;let Q=P[0];if(!(!Q||!Q[0]&&Q[0]!==0||!Q[1]&&Q[1]!==0)&&!(Number.isNaN(Q[0])||Number.isNaN(Q[1]))){if(f.pointer=Q,f.slowState.transitioning)m.transitionIntent&&(m.transitionIntent.transition(P,f,K()),u.emit("polygons.transition",{transitionIntent:m.transitionIntent.type}));else if(g.isPressed&&!g.noTransition&&g.lastPress&&f.pointer&&(!g.isClicking||aa(g.lastPress,f.pointer)>10))if(clearTimeout(g.pressTimeout),g.isClicking=!1,m.transitionIntent){if(f.transitionOrigin=g.lastPress,S({transitioning:!0}),m.transitionIntent.start){let dt=m.transitionIntent.start([g.lastPress],f,K());dt&&(dt.selectedPoints&&(f.selectedPoints=dt.selectedPoints,S({selectedPoints:dt.selectedPoints})),(dt.isOpen===!0||dt.isOpen===!1)&&(f.isOpen=dt.isOpen,m.shouldUpdate=!0),dt.points&&(f.polygon.points=dt.points,f.polygon.boundingBox=null,f.polygon.iedges=null,v(f.polygon),S({closestPoint:null})))}S({transitionModifiers:m.transitionIntent.modifiers||null}),u.emit("polygons.start-transition",{transitionIntent:m.transitionIntent.type}),m.transitionIntent.transition([g.lastPress],f,K()),u.emit("polygons.transition",{transitionIntent:m.transitionIntent.type})}else g.noTransition=!0}}function pe(){f.pointer=null,f.slowState.transitioning||(g.isPressed=!1,g.isClicking=!1,g.lastPress=null,S({transitionIntentType:null,actionIntentType:null}),m.transitionIntent=null,m.actionIntent=null)}function De(){g.isClicking=!0,g.isPressed=!0,g.lastPress=f.pointer,g.pressTimeout=setTimeout(()=>{g.isPressed&&(g.isClicking=!1)},250)}function ne(){f.slowState.transitioning?(S({transitioning:!1}),m.transitionIntent&&tt(m.transitionIntent),m.transitionIntent=null,S({transitionIntentType:null})):g.isClicking&&(m.actionIntent&&tt(m.actionIntent),m.actionIntent=null,S({actionIntentType:null})),g.isPressed=!1,g.isClicking=!1,g.lastPress=null,g.noTransition=!1,clearTimeout(g.pressTimeout)}function Di(P){f.polygon.points=P?.points||[],f.polygon.boundingBox=null,f.polygon.iedges=null,f.isOpen=P?.open||!1,v(f.polygon),m.nextSlowState=null,m.undoStack=[],m.undoStackPointer=-1,f.lineBox=null,f.line=null,g.pressTimeout&&clearTimeout(g.pressTimeout),S({noShape:P===null,shapeId:P?.id||null,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:f.slowState.modifiers.proximity},showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},selectedStamp:null,closestPoint:null,pointerInsideShape:!1,boxMode:!1,fixedAspectRatio:n}),$t(i),C()}function ri(P){if(!P)return"";let Q=bh[P];return!Q||!Q.modifiers?Q.label||"":f.slowState.modifiers.Shift&&Q.modifiers.Shift?Q.modifiers.Shift:f.slowState.modifiers.Alt&&Q.modifiers.Alt?Q.modifiers.Alt:f.slowState.modifiers.Meta&&Q.modifiers.Meta?Q.modifiers.Meta:Q.label}function me(){S({fixedAspectRatio:!0})}function ge(){S({fixedAspectRatio:!1})}function $t(P){if(f.slowState.isToolSwitchingLocked||f.slowState.currentTool===P||!f.slowState.enabledTools.includes(P))return;let Q=f.slowState.lastCreationTool;if(VI(P)){if(f.slowState.isToolSwitchingLocked)return;Q=P}f.slowState.transitioning&&(f.transitionPoints=null,f.transitionBoundingBox=null,f.transitionDraw=[],f.line=null,f.lineBox=null,S({transitioning:!1}));let dt=B(P),ht=f.selectedPoints,vt=f.isOpen,j=f.slowState.selectedStamp;switch(P){case"pointer":f.polygon.points.length>0&&!vt&&ht.length===0&&(ht=f.polygon.points.map((L,$)=>$));break;case"pen":f.polygon.points.length===0?(ht=[],vt=!0):vt&&ht.length>1&&(ht=[ht[ht.length-1]]);break;case"line":f.polygon.points.length===0?(ht=[],vt=!0):vt?ht.length>1&&(ht=[ht[ht.length-1]]):(ht=[f.polygon.points.length-1],vt=!0);break;case"pencil":ht=[],f.polygon.points.length===0&&(vt=!0);break;case"box":ht=f.polygon.points.map((L,$)=>$),vt=!1,Rt.square();break;case"lineBox":ht=[],f.polygon.points.length===0&&(vt=!0);break;case"stamp":ht=[],vt=!1,j||Rt.square();break;case"hand":ht=[];break;default:ht=[];break}S({validIntentKeys:dt}),S({currentTool:P,selectedPoints:ht,lastCreationTool:Q,boxMode:P==="box"}),f.selectedPoints=ht,f.isOpen=vt,Mn(P),U()}function Mn(P){let Q=P==="box"||P==="pointer"&&f.polygon.points.length>2&&f.selectedPoints.length===f.polygon.points.length&&!f.isOpen;S({showBoundingBox:Q})}function St(P){f.slowState.currentTool===P&&$t("pointer")}function It(P){f.slowState.currentTool===P?$t("pointer"):$t(P)}let te={undo:A,redo:T,get canUndo(){return m.undoStackPointer>-1},get canRedo(){return m.undoStackPointer<m.undoStack.length-1}},Hn={setTool:$t,unsetTool:St,toggleTool:It,updateVisibility:Mn,setCanDelete(P){S({canDelete:P})},setCanDeselect(P){tn(),S({canDeselect:P})},lockToolSwitching(){S({isToolSwitchingLocked:!0})},unlockToolSwitching(){S({isToolSwitchingLocked:!1})},toggleToolSwitching(){S({isToolSwitchingLocked:!f.slowState.isToolSwitchingLocked})},shortcuts:Object.fromEntries(Object.entries(d).map(([P,Q])=>[Q,P])),getValidIntents(P){let Q=P||f.slowState.currentTool;return{actions:vh.filter(dt=>dt.tools.includes(Q)),transitions:Sh.filter(dt=>dt.tools.includes(Q)),keys:jc.filter(dt=>dt.tools.includes(Q)),all:B(Q),current:f.slowState.validIntentKeys}},getToolInfo(){return{name:f.slowState.currentTool,cursor:f.slowState.cursor,selectionCount:f.selectedPoints.length,pointCount:f.polygon.points.length,isOpen:f.isOpen}},smartSelect(){f.polygon.points.length!==0&&(f.isOpen?(f.selectedPoints=[f.polygon.points.length-1],S({selectedPoints:f.selectedPoints})):(f.selectedPoints=f.polygon.points.map((P,Q)=>Q),S({selectedPoints:f.selectedPoints})))},clearSelection(){f.selectedPoints=[],S({selectedPoints:[]})},autoSelect(){f.selectedPoints.length===f.polygon.points.length&&!f.isOpen?$t("pointer"):f.isOpen||f.selectedPoints.length>0?$t(f.slowState.lastCreationTool||"pen"):$t("pointer")}},Zn={enabled:()=>f.slowState.snapEnabled,enable:()=>S({snapEnabled:!0}),disable:()=>S({snapEnabled:!1}),toggle:()=>S({snapEnabled:!f.slowState.snapEnabled}),pointsEnabled:()=>f.slowState.snapToPoints,enablePoints:()=>S({snapToPoints:!0}),disablePoints:()=>S({snapToPoints:!1}),togglePoints:()=>S({snapToPoints:!f.slowState.snapToPoints}),linesEnabled:()=>f.slowState.snapToLines,enableLines:()=>S({snapToLines:!0}),disableLines:()=>S({snapToLines:!1}),toggleLines:()=>S({snapToLines:!f.slowState.snapToLines}),intersectionsEnabled:()=>f.slowState.snapToIntersections,enableIntersections:()=>S({snapToIntersections:!0}),disableIntersections:()=>S({snapToIntersections:!1}),toggleIntersections:()=>S({snapToIntersections:!f.slowState.snapToIntersections}),gridEnabled:()=>f.slowState.snapToGrid,enableGrid:()=>S({snapToGrid:!0}),disableGrid:()=>S({snapToGrid:!1}),toggleGrid:()=>S({snapToGrid:!f.slowState.snapToGrid}),parallelEnabled:()=>f.slowState.snapToParallel,enableParallel:()=>S({snapToParallel:!0}),disableParallel:()=>S({snapToParallel:!1}),toggleParallel:()=>S({snapToParallel:!f.slowState.snapToParallel}),getThreshold:()=>f.snapThreshold*f.slowState.modifiers.proximity,setThreshold:P=>{f.snapThreshold=Math.max(5,Math.min(50,P))},isActive:()=>f.isSnapping,getSnapPoint:()=>f.snapPoint,getActiveGuides:()=>f.activeSnapGuides,getTargets:()=>f.snapTargets};function tn(){S({selectedPoints:f.polygon.points.map((P,Q)=>Q)})}function bn(P){S({bounds:P})}return $t(i),{state:f,setState:S,emitter:u,modifiers:ft,stamps:Rt,history:te,key:X,setScale:Ct,clock:ce,pointer:le,blur:pe,pointerDown:De,pointerUp:ne,setShape:Di,label:ri,tools:Hn,snap:Zn,selectAllPoints:tn,setBounds:bn,lockAspectRatio:me,unlockAspectRatio:ge}}function qc(t){let e=Math.max(...t.map(o=>o[0])),n=Math.min(...t.map(o=>o[0])),i=Math.max(...t.map(o=>o[1])),a=Math.min(...t.map(o=>o[1]));for(let o of t)if(o[0]!==n&&o[0]!==e&&o[1]!==a&&o[1]!==i||o[0]<n||o[0]>e||o[1]<a||o[1]>i)return!1;return!0}function Sl(t){if(!t)return null;if(t.points.length>2){let e=Math.min(...t.points.map(o=>o[0])),n=Math.min(...t.points.map(o=>o[1])),i=Math.max(0,...t.points.map(o=>o[0])),a=Math.max(0,...t.points.map(o=>o[1]));return{x:e,y:n,width:i-e,height:a-n}}return null}function $I(t){return t.type==="SvgSelector"}function xq(t){return t.type==="BoxSelector"}function cv(t,e){if($I(t)){let s=t.svgShape==="polyline";if(!t.points||t.points.length===0)return null;if(e){let{width:u,height:d}=e;return{type:"SvgSelector",value:`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${u} ${d}" width="${u}" height="${d}"><${s?"polygon":"polyline"} points="${t.points.map(f=>f.join(",")).join(" ")}" /></svg>`}}return{type:"SvgSelector",value:`<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'><g><path d='M${t.points.map(u=>u.join(",")).join(" ")}${s?" Z":""}' /></g></svg>`}}if(!t.spatial)return null;let{x:n,y:i,width:a,height:o}=t.spatial||{};return!a||!o?null:{type:"FragmentSelector",value:`xywh=${~~n},${~~i},${~~a},${~~o}`}}function Fc(t,e){if(!t||!t.points.length)return null;if(qc(t.points)){let n=t.points.map(d=>d[0]),i=t.points.map(d=>d[1]),a=Math.min(...n),o=Math.min(...i),s=Math.max(...n)-a,u=Math.max(...i)-o;return cv({type:"BoxSelector",spatial:{x:a,y:o,width:s,height:u}},e)}return cv({type:"SvgSelector",points:t.points,svgShape:t.open?"polygon":"polyline"},e)}function Eh(t){if(t.type==="polygon"&&!t.noBox&&t.points&&qc(t.points)){let n=Sl({open:!1,points:t.points||[]});return Eh({...t,type:"box",selector:n})}if(t.type==="polygon"||t.type==="draw")return{polygon:{points:t.points||[],open:t.open||!1},requestType:t.type,boundingBox:Sl({points:t.points||[],open:!1}),metadata:{},arguments:t.arguments||{},target:Fc({points:t.points||[],open:!1},t.bounds)};let e=t.selector;if(e){let n=[[e.x,e.y],[e.x+e.width,e.y],[e.x+e.width,e.y+e.height],[e.x,e.y+e.height]];return{polygon:{points:n,open:!1},requestType:t.type,boundingBox:e,metadata:{},target:Fc({points:n,open:!1},t.bounds),arguments:t.arguments||{}}}return{polygon:{points:[],open:!1},requestType:t.type,boundingBox:null,target:null,metadata:{},arguments:t.arguments||{}}}var Eq={shapeId:null,noShape:!0,transitioning:!1,actionIntentType:null,transitionIntentType:null,selectedPoints:[],hasClosestLine:!1,lastCreationTool:null,modifiers:{Alt:!1,Shift:!1,Meta:!1,proximity:0},bounds:null,showBoundingBox:!1,currentModifiers:{},validIntentKeys:{},pointerInsideShape:!1,closestPoint:null,transitionModifiers:null,selectedStamp:null,bezierLines:[],boxMode:!1,fixedAspectRatio:!1,cursor:"",enabledTools:["pointer","pen","box","lineBox","stamp","hand","line","pencil"],canDelete:!0,canDeselect:!0,isToolSwitchingLocked:!1,currentTool:"box",snapEnabled:!1,snapToPoints:!1,snapToLines:!1,snapToIntersections:!1,snapToGrid:!1,snapToParallel:!1};function vR({events:t,enabledTools:e,keyboardShortcutMapping:n,keyboardShortcutsEnabled:i=!1,debug:a}){let o=Yi((u,d)=>{let p=null,m=WI({emitter:t,keyboardShortcutsEnabled:i,keyboardShortcutMapping:n,enabledTools:e,customSetState:g=>{a&&console.log("partial state",g),u(v=>({polygonState:{...v.polygonState,...g}}))}},g=>{u(v=>v.tool.requestId?{polygon:{...g,id:v.tool.requestId}}:{polygon:{id:void 0,points:[],open:!0}}),t.emit("atlas.polygon-update",g)});return{mode:"explore",tool:{enabled:!1,requestId:null,canvasId:null},runtime:null,requestType:null,requests:{},history:m.history,polygon:null,validRequestIds:[],metadata:{},stableViewport:null,canvasRelativePositions:{},canvasViewports:{},polygons:m,polygonState:Eq,setMetadata:(g,v)=>{let S=v||d().tool.requestId;S&&u(E=>({metadata:{...E.metadata,[S]:{...E.metadata[S]||{},...g}}}))},setToolCanvasId:g=>{u(v=>({tool:{...v.tool,canvasId:g}}))},switchTool:{pointer(){u({mode:"sketch"}),s.tools.setTool("pointer")},hand(){u({mode:"explore"}),s.tools.setTool("hand")},draw(){u({mode:"sketch"}),s.tools.setTool("pencil")},pen(){u({mode:"sketch"}),s.tools.setTool("pen")},line(){u({mode:"sketch"}),s.tools.setTool("line")},lineBox(){u({mode:"sketch"}),s.tools.setTool("lineBox")},box(){u({mode:"sketch"}),s.tools.setTool("box")},triangle(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.triangle()},hexagon(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.hexagon()},circle(){u({mode:"sketch"}),s.tools.setTool("stamp"),s.stamps.circle()},remove(){let g=d();g.tool.requestId&&(m.setShape({points:[],open:!0}),g.cancelRequest(g.tool.requestId))}},reset:()=>{let g=d();g.tool.requestId&&g.cancelRequest(g.tool.requestId)},setPolygonState:g=>u({polygonState:typeof g=="function"?g(d().polygonState):g}),getRequestId:()=>{let g=Math.random().toString(36).slice(2);return u(v=>({validRequestIds:[...v.validRequestIds,g]})),{requestId:g,clear:()=>{d().tool.requestId,u(v=>({tool:v.tool.requestId===g?{enabled:!1,requestId:null,canvasId:null}:v.tool,validRequestIds:v.validRequestIds.filter(S=>S!==g)}))}}},cancelRequest:g=>{let v=g||d().tool.requestId;v&&(u(S=>({mode:"explore",tool:S.tool.requestId===v?{enabled:!1,requestId:null,canvasId:null}:S.tool,validRequestIds:S.validRequestIds.filter(E=>E!==v)})),t.emit("atlas.request-cancelled",{id:v}))},requestAnnotation:async(g,v)=>{let E={...d().requests,[v.requestId]:g},C=Eh(g);a&&console.log("requestAnnotation",{response:C,request:g});try{let{points:R=[],open:x=!0}=C.polygon||{},{requestId:A,canvasId:T=null,toolId:M}=v,z=M,D=d(),I=D.validRequestIds.includes(A),q=C.requestType;return a&&console.log("setting points",{requestType:q,points:R,open:x}),!I||D.tool.enabled?null:(m.setShape({id:A,points:R,open:x}),q==="polygon"&&(z=z||"pen",m.tools.setTool("pen")),q==="draw"&&(z=z||"draw",m.tools.setTool("pencil")),q==="box"&&(z=z||"box",m.tools.setTool("box")),q==="target"?(z=z||"box",m.tools.setTool("box"),m.lockAspectRatio(),m.tools.lockToolSwitching(),m.tools.setCanDeselect(!1),m.tools.setCanDelete(!1)):(m.tools.unlockToolSwitching(),m.tools.setCanDeselect(!0),m.tools.setCanDelete(!0)),g.bounds&&m.setBounds(g.bounds),t.emit("atlas.annotation-request",{id:A}),u({polygon:{id:A,points:R,open:x},mode:"sketch",requestType:q,tool:{enabled:!0,requestId:A,canvasId:T},requests:E}),z?D.switchTool[z]?.():R.length===0&&D.switchTool.box(),(typeof g.selectByDefault>"u"&&R.length&&q==="box"||g.selectByDefault)&&D.switchTool.pointer(),new Promise(B=>{let U=it=>{it.id===A&&(u(K=>({mode:"explore",tool:{requestId:null,enabled:!1,canvasId:null},requests:Object.fromEntries(Object.entries(K.requests).filter(([tt])=>tt!==A))})),t.off("atlas.request-cancelled",U),t.off("atlas.annotation-completed",W),B(null))},W=it=>{it.id===A&&(u(K=>({mode:"explore",tool:{requestId:null,enabled:!1,canvasId:null},requests:Object.fromEntries(Object.entries(K.requests).filter(([tt])=>tt!==A))})),t.off("atlas.annotation-completed",W),t.off("atlas.request-cancelled",U),B(it))};t.on("atlas.request-cancelled",U),t.on("atlas.annotation-completed",W)}))}catch(R){return console.error(R),null}},completeRequest:g=>{let v=d().tool.requestId;if(typeof g=="string"&&g&&g!==v)return;let S=v?d().requests[v]?.arguments||{}:{},E=v?d().metadata[v]||{}:{},C=d().polygonState.bounds,R=d().polygon;R&&(t.emit("atlas.annotation-completed",{id:R.id,polygon:R,requestType:d().requestType,target:Fc(R,C),canvasId:d().tool.canvasId,boundingBox:Sl(R),metadata:E,arguments:{...S}}),m.setShape(null))},setAtlasRuntime:g=>{p=g,t.emit("atlas.ready",{runtime:g}),s.setScale(1/p._lastGoodScale),p.world.addLayoutSubscriber((v,S)=>{(v==="event-activation"||v==="zoom-to"||v==="go-home")&&p?._lastGoodScale&&!Number.isNaN(p._lastGoodScale)&&s.setScale(1/p._lastGoodScale)}),u({runtime:p})},clearAtlasRuntime:()=>{p=null,u({stableViewport:null,runtime:null})},setCanvasRelativePosition:(g,v)=>{u(S=>({canvasRelativePositions:{...S.canvasRelativePositions,[g]:v}}))},clearCanvasRelativePosition:g=>{u(v=>{let S={...v.canvasRelativePositions};return delete S[g],{canvasRelativePositions:S}})},changeMode:g=>{u({mode:g})},nudgeLeft:g=>{if(!p)return;let v=p.getViewport(),S=g??v.width*.1;p.world.gotoRegion({x:v.x-S,y:v.y,width:v.width,height:v.height,nudge:!0})},nudgeRight:g=>{if(!p)return;let v=p.getViewport(),S=g??v.width*.1;p.world.gotoRegion({x:v.x+S,y:v.y,width:v.width,height:v.height,nudge:!0})},nudgeUp:g=>{if(!p)return;let v=p.getViewport(),S=g??v.height*.1;p.world.gotoRegion({x:v.x,y:v.y-S,width:v.width,height:v.height,nudge:!0})},nudgeDown:g=>{if(!p)return;let v=p.getViewport(),S=g??v.height*.1;p.world.gotoRegion({x:v.x,y:v.y+S,width:v.width,height:v.height,nudge:!0})},zoomIn:()=>{p?.world?.zoomIn()},zoomOut:()=>{p?.world?.zoomOut()},goHome:()=>{p?.world?.goHome()},goToRegion:g=>{p?.world?.gotoRegion(g)},getRuntime:()=>p}}),s=o.getState().polygons;return t.on("atlas.annotation-request",()=>{s.clock.start((u,d,p)=>{t.emit("atlas.polygon-render",{state:u,slowState:d,dt:p})},u=>{typeof u=="object"?o.setState({polygonState:u}):o.setState(d=>({polygonState:u(d.polygonState)}))})}),t.on("atlas.annotation-completed",()=>{s.clock.stop()}),t.on("atlas.request-cancelled",()=>{s.clock.stop()}),o}var bR=_(Y(),1),Ls=(0,ar.createContext)(null);function Ie(){return(0,ar.useContext)(Ls)}var SR={};function xR(t="atlas"){return SR[t]?.getState()}function Gc({children:t,name:e="atlas",existing:n,atlasStoreConfig:i}){let a=gR(),o=(0,ar.useContext)(Ls),s=(0,ar.useMemo)(()=>n||o||vR({events:a,...i||{}}),[a,n,o]),u=bt(s,d=>d.mode);return(0,ar.useEffect)(()=>{let d=e;return d&&(SR[d]=s),()=>{d&&delete SR[d]}},[s,e]),(0,bR.jsx)(Ls.Provider,{value:s,children:(0,bR.jsx)(Vc.Provider,{value:u||"explore",children:t})})}var rD=_(nt(),1);var kc=_(nt(),1),tD=_(Y(),1);function ER(){return{default:{backgroundColor:"rgba(0,0,0,0)",borderWidth:"2px",borderColor:"rgba(252,0,98, .5)"},highlighted:{backgroundColor:"rgba(75, 103, 225, 0.4)",borderWidth:"1px",borderColor:"rgba(75,103,225,0.99)"},hidden:{borderWidth:"0px",borderColor:"rgba(0,0,0,0)",backgroundColor:"rgba(0,0,0,0)",hidden:!0}}}var RR=(0,kc.createContext)(ER());RR.displayName="AnnotationStyle";function Rh(){return(0,kc.useContext)(RR)}function Rq({theme:t,children:e}){let n=(0,kc.useMemo)(()=>t||ER(),[t]);return(0,tD.jsx)(RR.Provider,{value:n,children:e})}var nD=_(nt(),1);var Yc=_(nt(),1);var eD=Fm()((t,e)=>{let n=new Map,i=()=>Math.random().toString(36).substring(2,15),a=o=>{let s=n.get(o);if(s)return s;let u=i();return n.set(o,u),u};return{stylesheets:{},loading:[],errors:[],async parseStylesheet(o){let{loadStylesheet:s,setStylesheet:u}=e();if(!o)return null;if(typeof o=="string")return o.startsWith("http")?{id:o,type:"CssStylesheet",value:await s(o)}:{id:u(o),type:"CssStylesheet",value:o};if(o.type==="CssStylesheet"){let d=o.id;if("value"in o){let p=Array.isArray(o.value)?o.value.join(`
86
86
  `):o.value;if(p)return{id:u(p,d),type:"CssStylesheet",value:p}}if("id"in o&&o.id){let p=await s(o.id);return{id:o.id,type:"CssStylesheet",value:p}}}return null},async loadStylesheet(o){if(e().loading.includes(o))return"";try{let u=await fetch(o);if(!u.ok)throw new Error(`Failed to load stylesheet from ${o}`);let d=await u.text();return t(p=>({stylesheets:{...p.stylesheets,[o]:d}})),d}catch(u){return t(d=>({errors:[...d.errors,{url:o,error:u}]})),""}},setStylesheet(o,s){let u=s||a(o);return t(d=>({stylesheets:{...d.stylesheets,[u]:o}})),u}}});function AR(t){let[e,n]=(0,Yc.useState)(""),{loading:i,errors:a,stylesheets:o,parseStylesheet:s}=eD();return(0,Yc.useEffect)(()=>{t&&s(t).then(d=>{d?.id&&n(d.id)})},[t,s]),[(0,Yc.useMemo)(()=>{let d={};return e&&o[e]&&(d[e]=o[e]),d},[e,o]),{id:e,stylesheets:o,loading:i,errors:a}]}function Ah(t={},e=[]){let{id:n,selector:i}=t,a=ti(),o=Vt(),s=n||a.annotation,u=re(f=>s?f.iiif.entities.Annotation[s]:void 0,[s]),[d]=AR(u?.stylesheet),p=re(f=>u&&u.body?u.body.map(m=>m?m.type==="SpecificResource"?{...m,source:o.get(m)}:m?f.iiif.entities[m.type][m.id]:null:null).filter(Boolean):[],[u]);return(0,nD.useMemo)(()=>{if(!u)return;let f={...u,body:p,target:an(u.target,{typeMap:o.getState().iiif.mapping,loadedStylesheets:d})};return i?i(f):f},[u,i,p,d,...e])}var iD=_(nt(),1);function zt(t={},e=[]){let{id:n,selector:i}=t,a=ti(),o=n||a.canvas,s=re(u=>o?u.iiif.entities.Canvas[o]:void 0,[o]);return(0,iD.useMemo)(()=>{if(s)return i?i(s):s},[s,i,...e])}var TR=_(nt(),1);function Th(t=Bo){return{addEventListener(e,n,i,a){if(e)return t.setMetaValue([e.id,"eventManager",n],o=>{let s=o||[];for(let u of s)if(u.callback===i)return s;return[...s,{callback:i,scope:a}]}),i},removeEventListener(e,n,i){e&&t.setMetaValue([e.id,"eventManager",n],a=>(a||[]).filter(o=>o.callback!==i))},getListenersAsProps(e,n){let i=typeof e=="string"?{id:e}:e;if(!i||!i.id)return{};let a=t.getResourceMeta(i.id,"eventManager"),o={};if(a&&i)for(let s of Object.keys(a))o[s]=u=>{let d=t.get(i);for(let{callback:p,scope:f}of a[s]||[])(!f||n&&f.indexOf(n)!==-1)&&p(u,d)};return o}}}function Ch(t,e){let n=Vt(),i=(0,TR.useMemo)(()=>Th(n),[n]),a=re(()=>t&&t.id?n.getResourceMeta(t.id,"eventManager"):null,[t]);return(0,TR.useMemo)(()=>t?i.getListenersAsProps(t,e):{},[a,t,n,e])}var aD=_(nt(),1);function bl(t=Bo){return{applyStyles(e,n,i){let a=typeof e=="string"?e:e.id;return t.setMetaValue([a,"styles",n],i)},getAppliedStyles(e){let n=typeof e=="string"?e:e.id;return t.getResourceMeta(n,"styles")}}}function Ps(t,e){let n=Vt(),i=(0,aD.useMemo)(()=>bl(n),[n]);return re(()=>{if(!t)return null;let a=i.getAppliedStyles(t.id);return a?e?a[e]:a:void 0},[t,e])}var oD=_(nt(),1);var go=_(nt(),1),yn=_(Y(),1);function CR({handleSize:t,resizable:e,onSave:n,children:i,maintainAspectRatio:a,disableCardinalControls:o,aspectRatio:s,...u}){let d=typeof t>"u"?a?12:8:t,{portalRef:p,mode:f,mouseEvent:m,isEditing:g}=LI({x:u.x||0,y:u.y||0,width:u.width,height:u.height,aspectRatio:s,maintainAspectRatio:a},n),v=(0,go.useMemo)(()=>m("translate"),[m]),S=(0,go.useMemo)(()=>m("east"),[m]),E=(0,go.useMemo)(()=>m("west"),[m]),C=(0,go.useMemo)(()=>m("south"),[m]),R=(0,go.useMemo)(()=>m("north"),[m]),x=(0,go.useMemo)(()=>m("south-east"),[m]),A=(0,go.useMemo)(()=>m("south-west"),[m]),T=(0,go.useMemo)(()=>m("north-east"),[m]),M=(0,go.useMemo)(()=>m("north-west"),[m]),z=f==="sketch",D={zIndex:999,boxShadow:"0px 2px 3px 0 rgba(0,0,0,0.2)",outline:"2px solid rgba(155,155,155,.7)",borderRadius:a||o?"50%":2,position:"absolute",background:"#fff",pointerEvents:g?"none":z?"initial":"none"};return(0,yn.jsx)(yn.Fragment,{children:(0,yn.jsxs)("world-object",{...u,children:[i,z&&e?(0,yn.jsx)(_i,{ref:p,target:{x:0,y:0,height:u.height,width:u.width},relative:!0,interactive:!1,children:z&&e?(0,yn.jsxs)(yn.Fragment,{children:[(0,yn.jsx)("div",{onMouseDown:v,onTouchStart:v,style:{display:"block",width:"100%",height:"100%",cursor:"move",border:"1px solid rgba(255,255,255, .7)",outline:"2px solid rgba(155,155,155, .7)",boxSizing:"border-box",pointerEvents:g?"none":z?"initial":"none"}}),a?null:(0,yn.jsxs)(yn.Fragment,{children:[(0,yn.jsx)("div",{title:"east",onTouchStart:S,onMouseDown:S,style:{...D,cursor:"e-resize",height:d*2,width:d,right:0,top:"50%",opacity:o?0:1,transform:`translate(${d/2}px, -${d}px)`}}),(0,yn.jsx)("div",{title:"west",onMouseDown:E,style:{...D,cursor:"w-resize",position:"absolute",height:d*2,width:d,left:0,top:"50%",opacity:o?0:1,transform:`translate(-${d/2}px, -${d}px)`}}),(0,yn.jsx)("div",{title:"north",onMouseDown:R,style:{...D,cursor:"n-resize",position:"absolute",height:d,width:d*2,left:"50%",top:0,opacity:o?0:1,transform:`translate(-${d}px, -${d/2}px)`}}),(0,yn.jsx)("div",{title:"south",onMouseDown:C,style:{...D,cursor:"s-resize",position:"absolute",height:d,width:d*2,left:"50%",bottom:0,opacity:o?0:1,transform:`translate(-${d}px, ${d/2}px)`}})]}),(0,yn.jsx)("div",{title:"north-east",onMouseDown:T,style:{...D,cursor:"ne-resize",position:"absolute",height:d,width:d,right:0,top:0,transform:`translate(${d/2}px, -${d/2}px)`}}),(0,yn.jsx)("div",{title:"south-east",onMouseDown:x,style:{...D,cursor:"se-resize",position:"absolute",height:d,width:d,bottom:0,right:0,transform:`translate(${d/2}px, ${d/2}px)`}}),(0,yn.jsx)("div",{title:"south-west",onMouseDown:A,style:{...D,cursor:"sw-resize",position:"absolute",height:d,width:d,bottom:0,left:0,transform:`translate(-${d/2}px, ${d/2}px)`}}),(0,yn.jsx)("div",{title:"north-west",onMouseDown:M,style:{...D,cursor:"nw-resize",position:"absolute",height:d,width:d,top:0,left:0,transform:`translate(-${d/2}px, -${d/2}px)`}})]}):null}):null]})})}var wR=_(Y(),1);function MR({id:t,interactive:e,region:n,onClick:i,onHover:a,onSave:o,aspectRatio:s,maintainAspectRatio:u,disableCardinalControls:d,isEditing:p,style:f={backgroundColor:"rgba(0,0,0,.5)"}}){let m=(0,oD.useCallback)(g=>{p&&o?.({id:n.id,x:n.x,y:n.y,height:n.height,width:n.width,...g})},[o,p,n.id,n.x,n.y,n.height,n.width]);return(0,wR.jsx)(CR,{id:t,x:n.x,y:n.y,width:n.width,aspectRatio:s,height:n.height,resizable:p,onSave:m,maintainAspectRatio:u,disableCardinalControls:d,children:(0,wR.jsx)("box",{interactive:e,onClick:g=>{g.preventDefault(),g.stopPropagation(),i?.(n)},relativeStyle:!0,onMouseEnter:()=>{a?.(n)},target:{x:0,y:0,width:n.width,height:n.height},style:f})})}var lD=_(Y(),1),Bs=({id:t,style:e,className:n,interactive:i,targetId:a,ignoreTargetId:o})=>{let s=Ah({id:t}),u=Ps(s,"atlas"),d=Ps(s,"html"),p=Ch(s,["atlas"]),f=zt(),m=Rh(),g=(0,rD.useMemo)(()=>gh(gh(gh(gh(e,u),s?.motivation?.includes("highlighting")?m.highlighted:m.default),s?.target?.selector?.boxStyle||{})),[e,u,m,s?.motivation,s?.target]),v=a||f?.id;return f&&s&&s.target&&s.target.selector&&(s.target.selector.type==="BoxSelector"||s.target.selector.type==="SvgSelector")&&s.target.source&&(o?!0:s.target.source.id===v||s.target.source===v)?(0,lD.jsx)(MR,{id:s.id,isEditing:!0,region:s.target.selector.spatial,style:g,className:d?.className||n,interactive:!!(d?.href||i),href:d?.href||null,title:d?.title||null,hrefTarget:d?.target||null,onClick:()=>{},...p}):null};var uD=_(nt(),1);var sD=_(nt(),1);function OR(t={},e=[]){let{id:n,selector:i}=t,a=ti(),o=n||a.annotationPage,s=re(u=>o?u.iiif.entities.AnnotationPage[o]:void 0,[o]);return(0,sD.useMemo)(()=>{if(s)return i?i(s):s},[s,...e])}var _R=_(Y(),1),xl=({className:t,page:e,targetId:n,ignoreTargetId:i})=>{let a=OR({id:e.id})||e,o=Ps(a,"atlas"),s=Ps(a,"html");return re(u=>a.id?u.iiif.entities.AnnotationPage[a.id]:null,[]),(0,_R.jsx)(uD.Fragment,{children:a.items?.map(u=>(0,_R.jsx)(Bs,{id:u.id,style:o,className:s?.className||t,targetId:n,ignoreTargetId:i},u.id))})};var El=_(nt(),1),fv=_(Y(),1),cD=(0,El.createContext)(null),fD=(0,El.createContext)(null),dD=(0,El.createContext)(null);function Aq(){let t=(0,El.useContext)(cD);if(!t)throw new Error("Ctx not found");return t}function Tq(){let t=(0,El.useContext)(fD);if(!t)throw new Error("Ctx not found");return t}function Cq(){let t=(0,El.useContext)(dD);if(!t)throw new Error("Ctx not found");return t}function wh({actions:t,state:e,children:n,currentTime:i,progress:a,element:o}){return(0,fv.jsx)(dD.Provider,{value:{currentTime:i,progress:a,element:o},children:(0,fv.jsx)(fD.Provider,{value:t,children:(0,fv.jsx)(cD.Provider,{value:e,children:n})})})}var mD=_(nt(),1);function hD(t,e,n=[],i=!1,a=[]){if(a.length&&(e=e.filter(s=>a.indexOf(s)===-1)),!e||e.length===0)return;if(e.length===1)return e[0];if(!t)return e.indexOf("none")===-1?e[0]:"none";if(e.indexOf(t)!==-1)return t;let o=t.indexOf("-")===-1?null:t.slice(0,t.indexOf("-"));if(o&&e.indexOf(o)!==-1)return o;for(let s of n)if(e.indexOf(s)!==-1)return s;if(!i&&t){let s=e.map(d=>d.indexOf("-")===-1?null:d.slice(0,d.indexOf("-"))),u=s.indexOf(t);if(u!==-1)return e[u];for(let d of n){let p=d.indexOf("-")===-1?null:d.slice(0,d.indexOf("-")),f=p?e.indexOf(p):-1;if(f!==-1)return e[f]}}return e.indexOf("none")===-1?e.indexOf("@none")===-1?e[0]:"@none":"none"}function zR(t,e,n={}){let{strictFallback:i=!1,defaultText:a="",separator:o=`
87
87
  `,fallbackLanguages:s=[],closest:u,skipLanguages:d}=n,p=Object.keys(t||{}),f=u?e:hD(e,p,s,i,d);if(!t)return a;if(typeof t=="string")return t;let m=f?t[f]:void 0;if(m&&f){if(typeof m=="string")return m;if(m.length===1&&m[0]===""){let g=n.skipLanguages||[];return zR(t,e,{...n,skipLanguages:[...g,f]})}return m.join(o)}return""}function dv(t,e={}){return zR(t,e.language||(typeof navigator<"u"?navigator.language:"en"),e)}var wq=pD();function pD(t={}){return(e,...n)=>{let i="";for(let a=0;a<e.length;a++)if(i+=e[a],a<n.length){let o=n[a];if(o==null)continue;typeof o=="string"?i+=o:i+=dv(o,t)}return i}}function NR(t){return t.type==="SpecificResource"?[t.source,{selector:t.selector}]:[t,{selector:null}]}function Qc(t=Bo){function e(a){let o=a?typeof a=="string"?t.get(a):a:null;if(!o)return[];let s=t.get(o.items,{parent:o}),u=[];for(let d of s)u.push(...t.get(d.items,{parent:d}));return u}function n(a,o=[]){let s=Array.isArray(a)?a:e(a),u=[],d={items:[],type:"complex-choice"},p=[];for(let f of s){if(f.type!=="Annotation")throw Error("getPaintables() accept either a canvas or list of annotations");let m=Array.from(Array.isArray(f.body)?f.body:[f.body]);for(let g of m){let[v,{selector:S}]=NR(g),E=t.get(v),C=(E.type||"unknown").toLowerCase();if(C==="choice"){let R=t.get(E.items,{parent:E.id}),x=o.length?o.map(A=>R.find(T=>T.id===A)).filter(Boolean):[R[0]];x.length===0&&x.push(R[0]),d.items.push({type:"single-choice",items:R.map(A=>({id:A.id,label:A.label,selected:x.indexOf(A)!==-1})),label:v.label}),m.push(...x);continue}u.indexOf(C)===-1&&u.push(C),p.push({type:C,annotationId:f.id,annotation:f,resource:E,target:f.target,selector:S})}}return{types:u,items:p,choice:d.items.length<2?d.items[0]||null:d,allChoices:d.items.length?d:null}}function i(a){let{choice:o}=n(a);return o}return{getAllPaintingAnnotations:e,getPaintables:n,extractChoices:i}}function Mh(){let t=on(),e=zt();return(0,mD.useMemo)(()=>{if(!t||!e||!t.start)return null;let n=an(t.start);return!n||n.source.id!==e.id||!n||!n.selector||n.selector.type!=="TemporalSelector"?null:n.selector.temporal},[t,e])}var $e=_(nt(),1);function gD(t){return{isMuted:!1,playRequested:!1,isPlaying:!1,isFinished:!1,volume:100,duration:t}}function Mq(t,e){switch(e.type){case"RESET":return e.state;case"FINISHED":return{...t,isFinished:!0,isPlaying:!1,playRequested:!1};case"PLAY_PAUSE":return{...t,isFinished:!1,isPlaying:!t.isPlaying};case"PLAY_REQUESTED":return{...t,isFinished:!1,playRequested:!0};case"PAUSE":return{...t,isPlaying:!1};case"PLAY":return{...t,isFinished:!1,playRequested:!1,isPlaying:!0};case"MUTE":return{...t,isMuted:!0};case"SET_VOLUME":return{...t,volume:e.volume,isMuted:e.volume===0};case"TOGGLE_MUTE":return{...t,isMuted:!t.isMuted};case"UNMUTE":return{...t,isMuted:!1}}return t}function hv(t){let e=Math.round(t);return`${Math.floor(e/60)}:${`${e%60}`.padStart(2,"0")}`}function Hs(t){let[e,n]=(0,$e.useReducer)(Mq,gD(t.duration));(0,$e.useEffect)(()=>{n({type:"RESET",state:gD(t.duration)})},[t.duration]);let i=(0,$e.useRef)(null),a=(0,$e.useRef)(null),o=(0,$e.useRef)(null),s=(0,$e.useRef)(!1),u=(0,$e.useCallback)(()=>{a.current&&i.current&&(a.current.innerHTML=hv(i.current.currentTime),o.current&&(o.current.style.width=`${i.current.currentTime/t.duration*100}%`),s.current!==i.current.muted&&(s.current=i.current.muted,n(i.current.muted?{type:"MUTE"}:{type:"UNMUTE"})))},[t.duration]),d=(0,$e.useCallback)(()=>{i.current&&(n({type:"PLAY_REQUESTED"}),i.current.play().then(()=>{n({type:"PLAY"})}),u())},[u]),p=(0,$e.useCallback)(()=>{i.current&&(i.current.duration>0&&i.current.paused?d():f())},[u]),f=(0,$e.useCallback)(()=>{i.current&&(i.current.pause(),n({type:"PAUSE"}),u())},[u]),m=(0,$e.useCallback)(()=>{i.current&&(i.current.muted=!i.current.muted,n(i.current.muted?{type:"MUTE"}:{type:"UNMUTE"}))},[]),g=(0,$e.useCallback)(()=>{i.current&&(i.current.muted=!0,n({type:"MUTE"}))},[]),v=(0,$e.useCallback)(()=>{i.current&&(i.current.muted=!1,n({type:"UNMUTE"}))},[]),S=(0,$e.useCallback)(R=>{i.current&&(i.current.muted=!1,i.current.volume=R/100,n({type:"SET_VOLUME",volume:R}))},[]),E=(0,$e.useCallback)(R=>{i.current&&(i.current.currentTime=Math.max(0,Math.min(R*t.duration,t.duration)),u())},[]),C=(0,$e.useCallback)(R=>{if(i.current){let x=typeof R=="function"?R(i.current.currentTime):R;i.current.currentTime=Math.max(0,Math.min(x,t.duration)),u()}},[]);return(0,$e.useEffect)(()=>{let R=setInterval(()=>{u()},350);return()=>clearInterval(R)},[u,t.duration]),(0,$e.useEffect)(()=>{let R=()=>{n({type:"FINISHED"})},x=i.current;return x?.addEventListener("ended",R),()=>x?.removeEventListener("ended",R)},[]),[{element:i,currentTime:a,progress:o},e,{play:d,pause:f,playPause:p,mute:g,unmute:v,toggleMute:m,setVolume:S,setDurationPercent:E,setTime:C}]}var Us=_(nt(),1),IR=(0,Us.createContext)(()=>{}),DR=(0,Us.createContext)(()=>{});function Xn(t,e,n,i,a=[]){let o=(0,Us.useContext)(t==="portal"?DR:IR);(0,Us.useEffect)(()=>(t!=="none"&&o(e,n,i),()=>{o(e,null)}),[e,t,o,...a])}var mv=_(Y(),1);function LR({media:t,startTime:e,children:n}){let[{element:i,currentTime:a,progress:o},s,u]=Hs({duration:t.duration}),d=e?`${t.url}#t=${e}`:t.url;return(0,mv.jsxs)(wh,{state:s,actions:u,currentTime:a,progress:o,element:i,children:[(0,mv.jsx)("audio",{ref:i,src:d}),n]},t.url)}function pv({media:t,mediaControlsDeps:e,audioCopmonent:n=LR,children:i}){let a=Mh();return Xn("portal","audio",n,{media:t,startTime:a?a.startTime:null,children:i},[t,a,...e||[]]),null}var gv=_(nt(),1),Rl=(0,gv.createContext)(null);Rl.displayName="Strategy";function Fe(){let t=(0,gv.useContext)(Rl);if(!t)throw new Error("useStrategy must be used within a StrategyProvider");return t}var Al=_(Y(),1);function PR({model:t}){return(0,Al.jsxs)(Al.Fragment,{children:[(0,Al.jsx)("style",{children:`
88
88
  .model-container {