import{a as e}from"./rolldown-runtime-COnpUsM8.js";import{Ht as t,mt as n}from"./vendor-react-N_fu9J_Y.js";import{Ot as r}from"./Storyblok-DGCBwXgG.js";import{t as i}from"./matsuya-api-BAyFV8GL.js";import{en as a}from"./drivers-wEwLhNmR.js";import{n as o}from"./NotificationsContextProvider-gq_W_pl_.js";var s=e(t()),c=()=>{let{t:e}=n(),{addNotification:t}=o(),{checkoutId:c,checkout:l,updateCheckout:u}=r(),[d,f]=(0,s.useState)(!1),[p,m]=(0,s.useState)(!1);return{t:e,onApplyCoupon:async n=>{if(!c){t({type:`error`,message:a.error.checkoutNotFound()});return}let r=l?.cart?.appliedPromotions?.filter(e=>e.redemptionType===`COUPON`);if(r&&r.length>=5){t({type:`error`,message:e(`error.onlyUpTo5CouponCodesCanBeApplied`,`Only up to 5 coupon codes can be applied.`)});return}f(!0),m(!1);try{let{data:r,error:a}=await i.POST(`/v1/checkout/{checkoutId}/coupon`,{params:{path:{checkoutId:c}},body:{couponCode:n}});if(a)throw a;r&&u(r),t({type:`success`,message:e(`cart.couponSuccessfullyApplied`,`Coupon code was successfully applied`)})}catch{m(!0),t({type:`error`,message:a.error.couponNotValid()})}finally{f(!1)}},onRemoveCoupon:async n=>{if(!c){t({type:`error`,message:a.error.checkoutNotFound()});return}f(!0);try{let{data:r,error:a}=await i.DELETE(`/v1/checkout/{checkoutId}/coupon/{couponId}`,{params:{path:{checkoutId:c,couponId:n}}});if(a)throw a;r&&u(r),t({type:`success`,message:e(`cart.couponSuccessfullyRemoved`,`Coupon code was successfully removed`)})}catch{t({type:`error`,message:e(`error.unableToRemoveTheCouponCode`,`Unable to remove the coupon code.`)})}finally{f(!1)}},loading:d,isError:p}};export{c as t};