"use strict";var React=require("react"),useTrackPreviewPause=require("../../carousel-v3/hooks/useTrackPreviewPause.js"),useTransition=require("./useTransition.js"),core=require("@gem-sdk/core");const useMarqueeScrollToItem=e=>{let{setting:t,builderProps:r,styles:u}=e,n=React.useRef(null),c=core.useCurrentDevice(),{speed:i,direction:a,isPreview:s,itemWidthType:o}=t||{},{itemMaxWidth:l}=u||{},m=React.useRef(null),T=React.useRef(null),f=React.useRef(null),R=React.useRef(null),[d,v]=React.useState(),[q,S]=React.useState(),[g,w]=React.useState(0),[h,E]=React.useState(!1),[I,k]=React.useState(!1),{isFirstPreviewClicked:M}=useTrackPreviewPause.default(s),b=React.useMemo(()=>g||(i&&M&&s&&!h?100*i:0),[i,M,s,g,h]),B=e=>{let t=n.current?.getBoundingClientRect(),r=window.innerWidth,u=n.current?.querySelector(`.gem-marquee-item-${e}`);if(!u)return;let c=u.getBoundingClientRect().left,i=u.getBoundingClientRect().width;if(c>0&&c+20<r)return;let a=c+i/2-r/2;i>r&&(a=c-(t?.left||0));let s=a/1e3;return s},P=e=>{let t=e.detail;r?.uid===t.id&&("activeItem"===t.field&&v(t.value),"hoverItem"===t.field&&S(t.value))};useTransition.useTransition((e,t)=>{if(void 0===e||!t||e==i&&t==a)return;R.current&&clearTimeout(R.current);let r=i||0;return w(100*r),R.current=setTimeout(()=>{w(0)},3e3),()=>{R.current&&clearTimeout(R.current)}},[i,a]),useTransition.useTransition(e=>{e!==b&&void 0!==e&&0===b&&y()},[b]),useTransition.useTransition(e=>{e&&JSON.stringify(e)!==JSON.stringify(l)&&(m.current&&clearTimeout(m.current),k(!0),f.current&&clearTimeout(f.current),f.current=setTimeout(()=>{y()},1500),m.current=setTimeout(()=>{k(!1)},3e3))},[l]),React.useEffect(()=>(window.addEventListener("update-marquee",P),()=>{window.removeEventListener("update-marquee",P)}),[]),React.useEffect(()=>{if(void 0!==d)return p(d||"0"),()=>{f.current&&clearTimeout(f.current),m.current&&clearTimeout(m.current)}},[d]),React.useEffect(()=>()=>{f.current&&clearTimeout(f.current),m.current&&clearTimeout(m.current),T.current&&clearTimeout(T.current)},[]),React.useEffect(()=>{h&&E(!1)},[s]);let y=e=>{let t=n.current?.querySelector(".first-block");t?.scrollIntoView({inline:"center",block:"nearest",behavior:e?"smooth":void 0})},p=e=>{if(o?.[c]==="BASE_ON_NUMBER_OF_ITEMS")return;if(T.current&&clearTimeout(T.current),k(!0),T.current=setTimeout(()=>{k(!1)},2e3),"0"==e){y(!0);return}let t=n.current?.querySelector(`.gem-marquee-item-${e}`);t?.scrollIntoView({inline:"center",block:"nearest",behavior:"smooth"})},C=React.useMemo(()=>core.isBrowser()?B(parseInt(d??"0")):void 0,[d]),N=e=>`gem-marquee-item gem-marquee-item-${e}`;return{rate:b,wrapperElement:n,timeToScroll:C,hoverItem:q,setHoverItem:S,setActiveItem:v,getTimeScroll:B,activeItem:d,direction:a,isPause:I,scrollTime:1e3*Math.abs(C??0),scrollToItemClass:N}};exports.useMarqueeScrollToItem=useMarqueeScrollToItem;
