import{a as e}from"./rolldown-runtime-COnpUsM8.js";import{At as t,Ht as n,mt as r,n as i,ut as a}from"./vendor-react-N_fu9J_Y.js";import{Ot as o,U as s}from"./Storyblok-DGCBwXgG.js";import{t as c}from"./matsuya-api-BAyFV8GL.js";import{Gt as l,Yt as u,en as d,p as f}from"./drivers-wEwLhNmR.js";import{n as p}from"./NotificationsContextProvider-gq_W_pl_.js";import{s as m}from"./useAntavoMemberInfo-2rKaqvDh.js";import{t as h}from"./Button-CfyTBrFT.js";import"./Button-CpxzSFWT.js";import{t as g}from"./usePlaceOrder-DGOrlsGb.js";var _=e=>{if(e){if(e===`CREDIT_CARD`)return`saved_cards`;if(e===`BANK_TRANSFER`)return`bank_transfer`}},v=e(n()),y=i(),b=()=>{let[e,t]=(0,v.useState)(!1),n=(0,v.useRef)(!1),i=a(),{t:d,i18n:m}=r(),h=(0,y.useStripe)(),b=(0,y.useElements)(),{addNotification:x}=p(),{preSubmitPaymentCheck:S}=s(),{placeOrder:C,completeOrderCleanup:w,loading:T}=g(),{savedCardId:E,setPaymentLoading:D,selectedPaymentMethod:O,checkoutId:k}=o(),A=e=>e?.code===`payment_intent_unexpected_state`&&e?.payment_intent?.status===`requires_capture`,j=async({paymentMethod:e,savePaymentMethod:t,savedCardPaymentMethodId:n})=>{if(!k)throw Error(`Checkout session not found.`);let{error:r}=await c.POST(`/v1/checkout/{checkoutId}/payment`,{params:{path:{checkoutId:k}},body:{paymentMethod:e,savePaymentMethod:t,savedCardPaymentMethodId:n}});if(r)throw Error(r?.message||`Failed to set payment method.`)};return{onPlaceOrder:async({saveCard:e=!1,elements:r})=>{if(n.current)return;n.current=!0;let a=_(O),o;try{await S(),D(!0),t(!0);let n=r||b,s=n?.getElement?.(`cardNumber`),c=a===`card`||a===`saved_cards`&&!!s;if(c){if(!h||!n)throw Error(`Stripe.js not loaded`);if(!s)throw Error(d(`error.missingCardNumber`,`Card number element not found. Please ensure the payment form is properly loaded.`));h.registerAppInfo({name:l,partner_id:`pp_partner_EX2ZbzXcjjKsxn`});let{error:t,paymentMethod:r}=await h.createPaymentMethod({type:`card`,card:s});if(t)throw new u(t.message,t.code);await j({paymentMethod:`CREDIT_CARD`,savePaymentMethod:e});let a=await C();o=a.orderId;let c=a.payment?.clientSecret;if(c){let{error:t}=await h.confirmCardPayment(c,{payment_method:r.id,save_payment_method:e});if(t)if(A(t))console.warn(`Stripe intent already awaiting capture`);else throw new u(t.message,t.code)}o&&(w(o),i(`/${m.language||`en`}/order-confirmation?pc=1&on=${o}`))}if(a===`saved_cards`&&!c){if(!h||!b)throw Error(d(`error.stripeNotLoaded`,`Stripe.js not loaded. Please try again.`));h.registerAppInfo({name:l,partner_id:`pp_partner_EX2ZbzXcjjKsxn`}),await j({paymentMethod:`CREDIT_CARD`,savedCardPaymentMethodId:E});let e=await C();o=e.orderId;let t=e.payment?.clientSecret;if(t){let{error:e}=await h.confirmCardPayment(t,{save_payment_method:!0,payment_method:E});if(e)if(A(e))console.warn(`Stripe intent already awaiting capture`);else throw new u(e.message,e.code)}o&&(w(o),i(`/${m.language||`en`}/order-confirmation?pc=1&on=${o}`))}if(a===`bank_transfer`){await j({paymentMethod:`BANK_TRANSFER`});let e=await C();e.orderId&&(w(e.orderId),i(`/${m.language||`en`}/order-confirmation?pc=0&on=${e.orderId}`))}}catch(e){if(console.error(`Error during payment processing:`,e),e.code===`inventoryError`){x({type:`error`,message:d(`error.inventoryError`,`There is a problem with your order. Please try again.`)});return}let t=e.code;x({type:`error`,message:(t?f(t):``)||e.message}),o&&i(`/${m.language||`en`}/checkout?orderNo=${o}`)}finally{n.current=!1,t(!1),D(!1)}},loading:e||T}},x=t(),S=({disabled:e,role:t,saveCard:n,onBeforeSubmit:r,elements:i})=>{let{onPlaceOrder:a,loading:s}=b(),{isPlaceOrderDisabled:c,isPaymentRetry:l}=o();return l?null:(0,x.jsxs)(h,{type:`button`,role:t||`primary`,disabled:e||s||c,size:`fill`,classes:{buttonModifier:`!w-full !h-auto`},onClick:async()=>{r&&!r()||await a({saveCard:n,elements:i})},children:[s&&(0,x.jsx)(m,{}),d.payment.pay()]})};export{S as t};