import{r as c}from"./index.BIRwh3HZ.js";const p=.5,L=5,E={scale:1,translateX:0,translateY:0};function k(T=!0){const[D,M]=c.useState(E),[n,A]=c.useState(null),C=c.useCallback(h=>{A(h)},[]),a=c.useRef(E),o=c.useRef(0),l=c.useCallback(()=>{o.current||(o.current=requestAnimationFrame(()=>{M(a.current),o.current=0}))},[]);c.useEffect(()=>()=>{o.current&&cancelAnimationFrame(o.current)},[]);const u=c.useRef({isDragging:!1,startX:0,startY:0,lastTranslateX:0,lastTranslateY:0,initialPinchDistance:0,initialPinchScale:1}),x=c.useCallback(()=>{a.current=E,o.current&&(cancelAnimationFrame(o.current),o.current=0),M(E)},[]),P=c.useCallback((h,g,X)=>{if(!n)return;const f=Math.min(Math.max(p,h),L),d=a.current,r=n.getBoundingClientRect(),m=(g??r.left+r.width/2)-r.left-r.width/2,v=(X??r.top+r.height/2)-r.top-r.height/2,t=f/d.scale;a.current={scale:f,translateX:m-(m-d.translateX)*t,translateY:v-(v-d.translateY)*t},l()},[l,n]);return c.useEffect(()=>{if(!T||!n)return;const h=(t,e)=>{const s=t.clientX-e.clientX,i=t.clientY-e.clientY;return Math.sqrt(s*s+i*i)},g=t=>{t.preventDefault();const e=t.deltaY>0?.9:1.1,s=a.current,i=Math.min(Math.max(p,s.scale*e),L),Y=n.getBoundingClientRect(),w=t.clientX-Y.left-Y.width/2,S=t.clientY-Y.top-Y.height/2,R=i/s.scale;a.current={scale:i,translateX:w-(w-s.translateX)*R,translateY:S-(S-s.translateY)*R},l()},X=t=>{if(t.button!==0)return;const e=u.current,s=a.current;e.isDragging=!0,e.startX=t.clientX,e.startY=t.clientY,e.lastTranslateX=s.translateX,e.lastTranslateY=s.translateY},f=t=>{const e=u.current;e.isDragging&&(a.current={...a.current,translateX:e.lastTranslateX+(t.clientX-e.startX),translateY:e.lastTranslateY+(t.clientY-e.startY)},l())},d=()=>{u.current.isDragging=!1},r=t=>{const e=u.current,s=a.current;t.touches.length===2?(t.preventDefault(),e.initialPinchDistance=h(t.touches[0],t.touches[1]),e.initialPinchScale=s.scale):t.touches.length===1&&(e.isDragging=!0,e.startX=t.touches[0].clientX,e.startY=t.touches[0].clientY,e.lastTranslateX=s.translateX,e.lastTranslateY=s.translateY)},m=t=>{const e=u.current;if(t.touches.length===2){t.preventDefault();const s=h(t.touches[0],t.touches[1]),i=Math.min(Math.max(p,e.initialPinchScale*(s/e.initialPinchDistance)),L);a.current={...a.current,scale:i},l()}else t.touches.length===1&&e.isDragging&&(a.current={...a.current,translateX:e.lastTranslateX+(t.touches[0].clientX-e.startX),translateY:e.lastTranslateY+(t.touches[0].clientY-e.startY)},l())},v=()=>{u.current.isDragging=!1};return n.addEventListener("wheel",g,{passive:!1}),n.addEventListener("mousedown",X),document.addEventListener("mousemove",f),document.addEventListener("mouseup",d),n.addEventListener("touchstart",r,{passive:!1}),n.addEventListener("touchmove",m,{passive:!1}),n.addEventListener("touchend",v),()=>{n.removeEventListener("wheel",g),n.removeEventListener("mousedown",X),document.removeEventListener("mousemove",f),document.removeEventListener("mouseup",d),n.removeEventListener("touchstart",r),n.removeEventListener("touchmove",m),n.removeEventListener("touchend",v)}},[l,T,n]),{containerRef:C,state:D,reset:x,zoomTo:P,zoomLevel:`${Math.round(D.scale*100)}%`}}export{k as u};
