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