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