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