import{useCallback as e}from"react";import{useCartUI as o,useStoreFront as r,useCartId as t}from"@gem-sdk/core";let useCartOperations=({setting:s,showMessage:c,dispatchInteractionEvent:n,dispatchCustomEvent:a})=>{let{openCart:i}=o(),{storefrontToken:l}=r(),{cartId:u,updateCartId:k}=t(),m=e((e,o)=>{e?.enableMessage&&o(e.successMessage||"","success"),i()},[i]),f=(e,o)=>{if(!e.checkoutUrl)return;let r=o?`${e.checkoutUrl}?access_token=${o}`:e.checkoutUrl;window.location.href=r},g=e=>{e?.customURL?.link&&("_blank"===e.customURL.target?window.open(e.customURL.link,"_blank"):window.location.href=e.customURL.link)},p=e(e=>{let o={"open-cart-drawer":()=>i(),"go-to-url":()=>g(s),"buy-now":()=>f(e,l),"continue-shopping":()=>m(s,c)};s?.actionEffect!=="buy-now"&&k(e.id);let r=o[s?.actionEffect];r&&r(),n(),a()},[s,l,i,c,k,n,m,a]),w=e(e=>{s?.enableMessage&&(e?.[0]?.code==="INVALID"?c(e[0].message||"","error"):c(s.errorMessage||"","error"))},[s,c]);return{cartId:u,handleSuccess:p,handleError:w}};export{useCartOperations};
