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