import{useRenderMode as e,usePageType as t,useInteraction as n}from"@gem-sdk/core";import{useState as o,useRef as r,useMemo as i,useEffect as u}from"react";let useCompareImage=({props:s,aspectRatio:m})=>{let{styles:v,leftImage:d,rightImage:a,hover:l=!1,vertical:c=!1,sliderLineWidth:p=2,sliderPositionPercentage:g=.5,onSliderPositionChange:w,preload:h}=s,E=!c,{isPreviewSharePageMode:L,isEditMode:f}=e(),R="STATIC"===t(),[b,C]=o(g),[B,I]=o(0),[O,X]=o(0),[Y,A]=o(!1),[N,T]=o(!1),U=r(null),k=r(null),x=r(null),z=r(null),{onListener:D,saveToElementInteractionData:M}=n(),P=r(null),S=i(()=>m,[d,a,v?.aspectRatio,v?.customAspectRadio]);u(()=>{let e=`[data-id="${s?.componentUid}"]`;D({event:"gp:change-ratio-original",selector:e,elementRef:P},e=>{let{data:t,isRollback:n,uniqueStorageKey:o}=e||{};if(!n||void 0!==t){if(n){C(Number(t));return}M(P.current,o,b),C(Number(t)/100)}})},[D,s?.componentUid,M,b]);let $=i(()=>p/2+21,[p]),j=i(()=>(E?B-p/2:O-p/2)-21,[E,B,p,O]),q=h||f||L||R;u(()=>{let e=U.current;if(!e)return;let t=new ResizeObserver(([e])=>{let t=e?.target.getBoundingClientRect().width??0;I(t)});return t.observe(e),()=>t.disconnect()},[]),u(()=>{let e=z?.current?.complete;return e&&A(!0),()=>{A(!1)}},[d]),u(()=>{let e=x?.current?.complete;return e&&T(!0),()=>{T(!1)}},[a]);let y=N&&Y;return u(()=>{let e=E?g*B:g*O;e<$?C(E?$/B:$/O):e>j?C(E?j/B:j/O):C(g)},[O,B,E,j,$,g]),u(()=>{let e=e=>{let t=e||window.event;if(t.stopPropagation(),t.button&&0!==t.button)return;let n="touches"in t?t?.touches?.[0]?.pageX??0:t.pageX,o="touches"in t?t?.touches?.[0]?.pageY??0:t.pageY,r=n-window.pageXOffset,i=o-window.pageYOffset,u=x.current?.getBoundingClientRect(),s=E?r-(u?.left??0):i-(u?.top??0);s<$&&(s=$),s>j&&(s=j),E?C(s/B):C(s/O),w&&(E?w(s/B):w(s/O))},t=t=>{t.preventDefault(),e(t),t.button&&0!==t.button||(window.addEventListener("mousemove",e),r.addEventListener("touchmove",e))},n=()=>{window.removeEventListener("mousemove",e),r.removeEventListener("touchmove",e)},o=U.current,r=k.current;if(y&&o&&r){r.addEventListener("touchstart",t),r.addEventListener("touchend",n),l?(o.addEventListener("mousemove",e),o.addEventListener("mouseleave",n)):(o.addEventListener("mousedown",t),window.addEventListener("mouseup",n));let i=z.current?.getBoundingClientRect(),u=x.current?.getBoundingClientRect(),s=(i?.height??0)/(i?.width??1),m=(u?.height??0)/(u?.width??1),v=Math.min(s,m),d=B*v;X(d)}return()=>{o&&r&&(r.removeEventListener("touchstart",t),r.removeEventListener("touchend",n),o.removeEventListener("mousemove",e),o.removeEventListener("mouseleave",n),o.removeEventListener("mousedown",t),window.removeEventListener("mouseup",n),window.removeEventListener("mousemove",e),r.removeEventListener("touchmove",e))}},[l,j,$,E,S,B,O,y,p,v?.aspectRatio,w]),{ref:P,priority:q,sliderRef:k,containerRef:U,leftImageRef:z,rightImageRef:x,sliderPosition:b,containerWidth:B,containerHeight:O,allImagesLoaded:y,setLeftImgLoaded:A,setRightImgLoaded:T}};export{useCompareImage};
