import{useCurrentDevice as e,useFeaturedImageGlobal as r,useEditorMode as t,getResponsiveValueByScreen as o}from"@gem-sdk/core";import{useState as i,useRef as a,useEffect as s,useMemo as l,useCallback as n}from"react";import{useFilterImagesByVariant as p}from"./useFilterImagesByVariant.js";import{handleSetSrcForProductImageList as m}from"../../../../image/common/helpers.js";import{getShapeByLayout as u,checkDisplayGalleryByWidth as d}from"../common/productGallery.js";let useGalleryCarousel=c=>{let{builderPropUID:h,productImages:F,onHandleClick:y,activeIndexFormFeatureImage:B,onClickFeatureArrow:f,builderPropsClass:I,arrow:g,priorityFeatureImage:C,...q}=c,T=e(),b=r(),[v,O]=i(f||""),x=t(),[j,k]=i(0),G=a(),[P,S]=i(j),[V,A]=i(!1),{filteredProductImages:L}=p({productImages:F,displayByVariant:q?.displayByVariant,variantSelect:q?.variantSelect});s(()=>{O(f||"")},[f]),s(()=>{G.current&&clearTimeout(G.current),v&&(G.current=setTimeout(()=>{k(B||0),O("")}))},[B]),s(()=>{if(C){let e=L?.findIndex(e=>e.id===C.id)||0;k(e),O("")}},[C,L]),s(()=>{let e=L?.findIndex(e=>e.id===b?.id);k(e||0),O("")},[b,L]);let w=l(()=>u({shape:q.shape,shapeForBottom:q.shapeForBottom,shapeFor1Col:q.shapeFor1Col,shapeFor2Col:q.shapeFor2Col,shapeForFtOnly:q.shapeForFtOnly,shapeForInsideBottom:q.shapeForInsideBottom,shapeForInside:q.shapeForInside},q.position),[q.shape,q.shapeForBottom,q.shapeFor1Col,q.shapeFor2Col,q.shapeForFtOnly,q.position,q.shapeForInsideBottom,q.shapeForInside]),z=n(()=>{m({builderPropUID:h,shapeByLayout:w,qualityType:q.qualityType,qualityPercent:q.qualityPercent})},[w,q.qualityPercent,q.qualityType,h]);s(()=>{if("edit"===x)return;let e=document?.querySelector(`.gem-slider-item-${h} .product-list-image`);if(!e)return;z();let r=new ResizeObserver(()=>{z()});return r?.observe(e,{}),()=>{r?.disconnect()}},[z,h,x]);let R=o(q.clickOpenLightBox,T),$=l(()=>{let e=d({shapeByLayout:w});return o(e,T)},[w,T]),D=l(()=>q?.borderActive?.border!=="none",[q?.borderActive?.border]),E=o(q?.clickOpenLightBox,T),H=e=>{y&&y(e||"",R)};return s(()=>{let e;return L&&L.length>0&&V&&(e=setTimeout(()=>{S(j)},80)),()=>{e&&clearTimeout(e)}},[j,L,V]),{moveToIdx:j,isDisplay:$,shapeByLayout:w,isEnableBorderActive:D,isShowButtonLightbox:E,filteredProductImages:L,handleClickCarouselItem:H,setSliderReady:A,safeMoveToIdx:P}};export{useGalleryCarousel};
