import{a as e}from"./rolldown-runtime-COnpUsM8.js";import{Ht as t,mt as n}from"./vendor-react-N_fu9J_Y.js";import{At as r,Ot as i,W as a,o,q as s}from"./Storyblok-DGCBwXgG.js";import{t as c}from"./matsuya-api-BAyFV8GL.js";import{F as l,Nt as u,en as d}from"./drivers-wEwLhNmR.js";import{n as f,p}from"./checkout-storage-C4Gtbqnl.js";import{t as m}from"./vendor-stripe-BFE_J2tF.js";import{t as h}from"./useCheckoutAddressForm-B8Qb5435.js";var g=new Map,_=e=>{let t=`pk_test_51ObHA0Buqh0tr154mZn0rAfRIpi0cxsSNtHu2xGs2WCeSgM5XFOx403GRUDI5WfFiTRszmREBbyC1bNXOLOcceB500YC4tTjdu_${e}`;return g.has(t)||g.set(t,m(`pk_test_51ObHA0Buqh0tr154mZn0rAfRIpi0cxsSNtHu2xGs2WCeSgM5XFOx403GRUDI5WfFiTRszmREBbyC1bNXOLOcceB500YC4tTjdu`,{locale:e})),g.get(t)},v=e(t()),y=e=>({firstName:e.firstname||``,lastName:e.lastname||``,email:e.email||``,city:e.city||``,countryCode:e.country_code||``,postalCode:e.postcode||``,region:typeof e.region==`string`?e.region:``,regionId:Number(e.region)||0,line1:Array.isArray(e.street)?e.street[0]??``:``,line2:Array.isArray(e.street)?e.street[1]:void 0,phone:e.telephone||``,saveInAddressBook:e.save_in_address_book??!1,label:e.address_label??void 0}),b=()=>{let{regions:e}=h(),{checkoutId:t,updateCheckout:n}=i(),[r,a]=(0,v.useState)(!1),o=t=>e.length>0?e.find(e=>e.id===t.region)?.value||String(t.region||``):typeof t.region==`string`?t.region:``,s=e=>{if(e.customer_address_id)return{id:e.customer_address_id};let t=o(e);return y({...e,region:t})};return{onSubmitAddress:async(e,r)=>{if(!t)throw Error(d.error.checkoutNotFound());let i=s(e);a(!0);try{let e;if(r===`billing`){let{data:n,error:r}=await c.POST(`/v1/checkout/{checkoutId}/payment`,{params:{path:{checkoutId:t}},body:{billingAddress:i}});if(r)throw r;e=n}else{let{data:n,error:r}=await c.POST(`/v1/checkout/{checkoutId}/fulfilment`,{params:{path:{checkoutId:t}},body:{fulfilmentMethod:`DELIVERY`,shippingAddress:i}});if(r)throw r;e=n}e&&n(e)}catch(e){throw Error(e?.message||d.error.failedToSetAddress())}finally{a(!1)}},loading:r}},x=()=>{let{t:e}=n(),{checkoutId:t,updateCheckout:r}=i(),[a,o]=(0,v.useState)(!1);return{addEmailOnCart:async(n,i)=>{if(!t)throw Error(e(`error.checkoutNotFound`,`Checkout session not found.`));if(!n)throw Error(e(`error.emailRequired`,`Email is required.`));o(!0);try{let{data:e,error:a}=await c.POST(`/v1/checkout/{checkoutId}/customer`,{params:{path:{checkoutId:t}},body:{email:n,...i}});if(a)throw a;e&&r(e)}catch(t){throw Error(t?.message||e(`error.addEmailToCartFailed`,`Failed to add email to cart.`))}finally{o(!1)}},loading:a}},S=()=>{let{identify:e,cartInsight:t}=o(),[,n]=p(`cartId`,``),[,m]=s(u),[h,g]=(0,v.useState)(!1),{cart:_,isSignedIn:y,isStoreAppointment:S,isInstoreAppointment:C}=r(),{agreeToShareInfoWithBrands:w,agreed:T,createAccount:E,selectedPaymentMethod:D,checkoutId:O,checkout:k,formShippingAddress:A,formBillingAddress:j,giftSenderName:M}=i(),{onSubmitAddress:N}=b(),{addEmailOnCart:P}=x(),{submitAppointment:F,appointmentEmail:I}=a({enableFormSync:!1});return{placeOrder:async()=>{if(!T)throw Error(d.checkout.termsAndConditions());if(!O)throw Error(d.error.checkoutNotFound());g(!0);try{A&&(A.email&&await P(A.email),await N(A,`shipping`),A.use_as_billing_address&&await N(A,`billing`)),j&&!A?.use_as_billing_address&&await N(j,`billing`);let{data:e,error:t}=await c.POST(`/v1/checkout/{checkoutId}/complete`,{params:{path:{checkoutId:O}},body:{shareInfoWithBrands:w,giftSenderName:M?.trim()||void 0}});if(t||!e)throw Error(t?.message||d.error.placeOrderFailed());let{orderId:n}=e;if(S&&!C&&n)try{await F(n)}catch(e){console.error(`Appointment submission failed:`,{err:e,checkoutId:O,orderId:n})}return e}catch(e){throw console.error(`Place order error:`,{error:e,checkoutId:O}),e instanceof Error?e:Error(d.error.placeOrderFailed())}finally{g(!1)}},completeOrderCleanup:(0,v.useCallback)(r=>{f(),n(``);let i=S&&!C,a=k?.customer,o=a?.email||``;m(!y&&o&&r?{firstName:a?.firstName,lastName:a?.lastName,email:o,orderId:String(r),agreeToShareInfoWithBrands:w,createAccount:E,isStoreAppointment:i,storeAppointmentEmail:I}:{email:o,orderId:String(r),isStoreAppointment:i,storeAppointmentEmail:I}),y&&o&&e(o),t(`ORDER_COMPLETE`,{cartId:_?.id,clearCart:!0}),l({cartDetails:_,paymentType:D||`unknown`})},[n,m,k?.customer,y,S,C,w,E,I,e,t,_,D]),loading:h}};export{b as n,_ as r,S as t};