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