import{useRenderMode as e,useProduct as t,useCurrency as r,useQuantity as i,useProductProperties as a,useCurrentVariant as s,useHasPreSelected as o,useStickyStore as u,useProductStore as n,useCreateCart as l,useAddToCart as d,convertDecimalToNumber as m,useCurrentVariantInStock as c}from"@gem-sdk/core";import{useRef as p,useEffect as f,useCallback as b}from"react";import{addToCartTracking as v}from"../../../helpers/tracking.js";import{useDispatchInteractionEvent as g,useDispatchCustomEvent as k}from"./useDispatchAfterSubmitEvent.js";import{useCartMessage as D}from"./useCartMessage.js";import{useCartOperations as I}from"./useCartOperations.js";let E=({styles:E,setting:y,builderProps:S})=>{let j=p(null),{isEditMode:C,isPreviewSharePageMode:M}=e(),h=t(),{currency:w}=r(),{quantity:A}=i(),L=a(),O=s(),{hasPreSelected:q}=o(),x=u(e=>e.setElement),B=u(e=>e.removeElement),T=n(e=>e.updateIsSubmit),U=g(S?.uid),V=k({product:h,currentVariant:O,quantity:A}),{showMessage:z,notiComponent:F}=D(E,y),{handleSuccess:G,handleError:H,cartId:J}=I({setting:y,showMessage:z,dispatchInteractionEvent:U,dispatchCustomEvent:V}),{isMutating:K,trigger:N}=l({onSuccess:G,onError:H}),{isMutating:P,trigger:Q}=d({onSuccess:G,onError:H});f(()=>{let e=j.current;if(!(!e||e.closest(".sticky-wrapper"))&&S?.uid&&S.builderData?.advanced)return x({id:S.uid,element:e,deviceVisibility:S.builderData.advanced.d}),()=>{S?.uid&&B(S.uid)}},[]);let R=b(()=>{if(!O?.baseID||!A||!h?.baseID||void 0!==q&&!q||C||M)return;let e=L?.some(e=>e.required&&!e.value);if(T(!0),e)return;let t=L?.filter(e=>!!e.value)?.map(e=>({key:e.key,value:e.value}));v({quantity:A,currency:w,id:h?.baseID,name:h?.title,brand:h?.vendor,sku:O.sku,price:m(O?.price),variant:O.title});let r={quantity:A,attributes:t,variantId:O.baseID};y?.actionEffect!=="buy-now"&&J?Q({cartId:J,lines:[r]}):N({items:[r]})},[C,M,J,w,A,L,q,h?.title,h?.vendor,h?.baseID,O?.sku,O?.price,O?.title,O?.baseID,y?.actionEffect,N,Q,T]),W=c(),X=O?y?.outOfStockLabel:"Unavailable";return{addToCartRef:j,isLoading:K||P,isInStock:W,notiComponent:F,outOfStockLabel:X,addToCart:R}};export{E as useBtnAddToCart};
