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