"use strict";var core=require("@gem-sdk/core"),React=require("react"),common=require("../common/common.js"),productFeaturedImage=require("../common/productFeaturedImage.js"),useFilterImagesByVariant=require("./useFilterImagesByVariant.js"),helpers=require("../../../../image/common/helpers.js");const useProductFeaturedImageCarousel=e=>{let t;let{productImages:r,imgRef:a,onHandleClick:u,onClickArrow:o,onHandleLoaded:i,onHandleUpdateActiveIndex:c,builderPropUID:n,builderPropsClass:s,children:l,parentUid:d,...m}=e,h=core.useFeaturedImageGlobal(),g=core.useCurrentDevice(),p=React.createRef(),{onListener:f,ref:y}=core.useInteraction(),I=core.useEditorMode(),{filteredProductImages:R}=useFilterImagesByVariant.useFilterImagesByVariant({productImages:r,displayByVariant:m?.displayByVariant,variantSelect:m?.variantSelect}),S=React.useMemo(()=>{let e=R?.findIndex(e=>e.id===h?.id);return e&&e>=0?e:0},[R,h]),q=React.useMemo(()=>productFeaturedImage.checkRenderFeaturedCarousel(m),[m]),v=React.useMemo(()=>{if(R?.length)return h||R?.[0]},[R,h]),F=React.useMemo(()=>"popup"===core.getResponsiveValueByScreen(m.ftClickOpenLightBox,g),[m.ftClickOpenLightBox,g]),k=React.useMemo(()=>m?.otherImage?R&&m.otherImage>=R.length-1||R&&-1===m.otherImage?R.length-1:m.otherImage:0,[m?.otherImage,R]),b=React.useMemo(()=>m?.hoverEffect==="other"?R?.[k]:R?.find(e=>e.id===h?.id)||R?.[0],[h?.id,k,R,m?.hoverEffect]),B=function(){return Math.random().toString(36).substr(2,5)},C=e=>{t&&clearTimeout(t),t=setTimeout(()=>c&&c(e),500)},M=e=>{let t="1/1",r=0;return e?e.width&&e.height&&(t=`${e.width}/${e.height}`):R?.forEach(e=>{if(e.width&&e.height){let a=e.height/e.width;a>r&&(r=a,t=`${e.width}/${e.height}`)}}),t},E=e=>{let t=common.getAspectRatio(m.ftShape),r=M(e);return{desktop:t.desktop??r,tablet:t.tablet??r,mobile:t.mobile??r}},P=React.useCallback(()=>{helpers.handleSetSrcForFeatureProductImage({builderPropUID:n,ftShape:m.ftShape,qualityType:m.qualityType,qualityPercent:m.qualityPercent})},[m.ftShape,m.qualityPercent,m.qualityType,n]),V=()=>{let e=`[data-id="${n}"]`;f({event:"gp:change-image-step",selector:e},t=>{let r=(y.current||document).querySelector(e),{data:a,isRollback:u,uniqueStorageKey:o}=t||{};p?.current?.moveToSlideInteraction({element:r,data:a,isRollback:u,key:o})}),f({event:"gp:change-next-slide",selector:e},t=>{let r=(y.current||document).querySelector(e),{data:a,isRollback:u,uniqueStorageKey:o}=t||{};p?.current?.nextSlideInteraction({element:r,data:a,isRollback:u,key:o})}),f({event:"gp:change-previous-slide",selector:e},t=>{let r=(y.current||document).querySelector(e),{data:a,isRollback:u,uniqueStorageKey:o}=t||{};p?.current?.previousSlideInteraction({element:r,data:a,isRollback:u,key:o})})};return React.useEffect(()=>{if("edit"===I)return;let e=document?.querySelector(`.gem-slider-item-${n} .featured-image-only`);if(!e)return;P();let t=new ResizeObserver(()=>{P()});return t?.observe(e,{}),()=>{t?.disconnect()}},[P,n,I]),React.useEffect(()=>{V()},[]),React.useEffect(()=>{!q&&i&&i(!0,0)},[q,i]),{ref:y,imageActive:v,imageActiveIndex:S,imageShowWhenHover:b,enableOpenLightBox:F,filteredProductImages:R,isRenderFeatureCarousel:q,genRandomKey:B,getAspectRatioFinal:E,emitUpdateActiveIndex:C}};exports.useProductFeaturedImageCarousel=useProductFeaturedImageCarousel;
