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