import{r as a,j as p}from"./client-BbMi5ewG.js";import{A as O,m as L}from"./proxy-DChtKkEh.js";import{L as $}from"./loader-circle--S58donA.js";import"./vendor-DDjlQeP6.js";import"./utils-Q0fnGDhj.js";let g=null,w=null;const S=()=>{var o,e;return(e=(o=window==null?void 0:window.radius_booking_site_param)==null?void 0:o.currency)==null?void 0:e.symbol},b=()=>{var o;return((o=window==null?void 0:window.radius_booking_site_param)==null?void 0:o.rest_root)||"/wp-json/"},A=async()=>(w||(w=fetch(`${b()}radius-booking/v1/appointments/config`,{method:"GET",headers:{"Content-Type":"application/json","X-WP-Nonce":window.radius_booking_site_param.nonce}}).then(o=>o.json()).catch(o=>{throw console.error("Failed to load config:",o),w=null,o})),w),W=async()=>(g||(g=new Promise(async(o,e)=>{try{if(window.paypal){o(window.paypal);return}const c=(await A()).data.paypal.clientId,s=document.createElement("script");s.src=`https://www.paypal.com/sdk/js?client-id=${c}&currency=${S()}`,s.onload=()=>o(window.paypal),s.onerror=()=>{g=null,e(new Error("Failed to load PayPal SDK"))},document.body.appendChild(s)}catch(l){console.error("Failed to load PayPal config:",l),g=null,e(l)}})),g);function H({total:o,data:e,onSuccess:l,onError:c,next:s,setField:d}){var C,R,D,E,M;const P=a.useRef(null),[_,j]=a.useState(!0),[v,y]=a.useState(""),t=a.useRef(!0),I=a.useRef(!1),x=a.useMemo(()=>{var i;return`Booking - ${((i=e==null?void 0:e.service)==null?void 0:i.name)||"Service"}`},[(C=e==null?void 0:e.service)==null?void 0:C.name]),k=a.useMemo(()=>{var i,m,n;return{service_id:(i=e==null?void 0:e.service)==null?void 0:i.id,location_id:(m=e==null?void 0:e.location)==null?void 0:m.id,employee_id:(n=e==null?void 0:e.employee)==null?void 0:n.id}},[(R=e==null?void 0:e.service)==null?void 0:R.id,(D=e==null?void 0:e.location)==null?void 0:D.id,(E=e==null?void 0:e.employee)==null?void 0:E.id]),N=a.useMemo(()=>{var i;return((i=e==null?void 0:e.customer)==null?void 0:i.email)||""},[(M=e==null?void 0:e.customer)==null?void 0:M.email]);return a.useEffect(()=>(t.current=!0,I.current=!1,(async()=>{try{const m=await W();if(!t.current||!m||I.current)return;P.current&&(P.current.innerHTML=""),m.Buttons({style:{layout:"vertical",color:"gold",shape:"rect",label:"paypal"},createOrder:async(n,h)=>{var f;try{const r=await(await fetch(`${b()}radius-booking/v1/appointments/paypal/create-order`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":window.radius_booking_site_param.nonce},body:JSON.stringify({amount:o,currency:S(),description:x,customer_email:N,metadata:k})})).json();if(r!=null&&r.success&&((f=r==null?void 0:r.data)!=null&&f.orderId))return r.data.orderId;throw new Error((r==null?void 0:r.message)||"Failed to create PayPal order")}catch(u){const r=u.message||"Failed to create order";throw t.current&&(y(r),c(r)),u}},onApprove:async(n,h)=>{var f;try{const r=await(await fetch(`${b()}radius-booking/v1/appointments/paypal/capture-order`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":window.radius_booking_site_param.nonce},body:JSON.stringify({orderId:n.orderID})})).json();if(r!=null&&r.success&&((f=r==null?void 0:r.data)==null?void 0:f.status)==="COMPLETED"){d("paypalOrderId",n.orderID),d("paypalPayerId",r.data.payerId),d("paypalCaptureId",r.data.captureId),d("paymentStatus",r.data.status),d("amount",o);const T={orderId:n.orderID,payerId:r.data.payerId,captureId:r.data.captureId,status:r.data.status,amount:o};l({orderId:n.orderID,payerId:r.data.payerId,captureId:r.data.captureId,status:r.data.status,amount:o}),s==null||s({paypal:T})}else throw new Error((r==null?void 0:r.message)||"Payment capture failed")}catch(u){const r=u.message||"Payment failed";t.current&&(y(r),c(r))}},onError:n=>{const h=(n==null?void 0:n.message)||"PayPal payment failed";t.current&&(y(h),c(h))},onCancel:()=>{const n="Payment was cancelled";t.current&&(y(n),c(n))}}).render(P.current),I.current=!0,t.current&&j(!1)}catch{t.current&&(y("Failed to load PayPal"),c("Failed to load PayPal"),j(!1))}})(),()=>{t.current=!1}),[o,x,k,N,l,c,s,d]),p.jsx(O,{initial:!1,children:p.jsx(L.div,{initial:{height:0,opacity:0},animate:{height:"auto",opacity:1},exit:{height:0,opacity:0},transition:{type:"spring",stiffness:200,damping:28},className:"overflow-hidden",children:p.jsxs("div",{className:"mt-4 rounded-lg border bg-background p-4",children:[_&&p.jsxs("div",{className:"flex items-center justify-center py-8",children:[p.jsx($,{className:"h-6 w-6 animate-spin text-primary"}),p.jsx("span",{className:"ml-2 text-sm text-muted-foreground",children:"Loading PayPal..."})]}),v&&p.jsx("div",{className:"mb-3 rounded-lg border border-red-200 bg-red-50 text-red-700 px-3 py-2 text-sm",children:v}),p.jsx("div",{ref:P,className:_?"hidden":""})]})})})}export{H as default};
//# sourceMappingURL=PayPalPayment-20yU39Qi.js.map
