"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),v=core.useProductStore(e=>e.updateIsSubmit),C=useDispatchAfterSubmitEvent.useDispatchInteractionEvent(t?.uid),p=useDispatchAfterSubmitEvent.useDispatchCustomEvent({product:i,currentVariant:l,quantity:n}),{showMessage:g,notiComponent:f}=useCartMessage.useCartMessage(e,r),{handleSuccess:k,handleError:S,cartId:m}=useCartOperations.useCartOperations({setting:r,showMessage:g,dispatchInteractionEvent:C,dispatchCustomEvent:p}),{isMutating:D,trigger:I}=core.useCreateCart({onSuccess:k,onError:S}),{isMutating:E,trigger:h}=core.useAddToCart({onSuccess:k,onError:S});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(v(!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:core.convertDecimalToNumber(l?.price),variant:l.title});let s={quantity:n,attributes:t,variantId:l.baseID};r?.actionEffect!=="buy-now"&&m?h({cartId:m,lines:[s]}):I({items:[s]})},[u,a,m,c,n,o,d,i?.title,i?.vendor,i?.baseID,l?.sku,l?.price,l?.title,l?.baseID,r?.actionEffect,I,h,v]),q=core.useCurrentVariantInStock(),y=D||E,M=l?r?.outOfStockLabel:"Unavailable";return{addToCartRef:s,isLoading:y,isInStock:q,notiComponent:f,outOfStockLabel:M,addToCart:A}};exports.useBtnAddToCart=useBtnAddToCart;
