import{r as e,j as a}from"./client.ySKnF1Cn.js";import{C as t,A as n,d as s,B as o}from"./alert.L02_PIIX.js";import{u as r}from"./useTranslation.Cb9bYI5g.js";import{C as i}from"./circle-alert.D9xMeaAw.js";import{R as c}from"./refresh-cw.D_zjBwcO.js";import{L as l}from"./loader-circle.BwcL2Xcc.js";import"./context.ZwV2gUb-.js";const m=()=>window.walletUpData?.ajaxUrl||window.ajaxurl||"/wp-admin/admin-ajax.php",d=({amount:d,orderId:p="",email:u="",firstName:w="",lastName:h="",onSuccess:x,onError:j,onProcessing:b,className:f=""})=>{const{t:g}=r(),[y,v]=e.useState("idle"),[N,C]=e.useState(null),[_,F]=e.useState(null),[E,S]=e.useState(""),k=e.useRef(!0);e.useEffect(()=>()=>{k.current=!1},[]);const z=e.useCallback(async()=>{v("loading"),C(null);try{const e=new FormData;e.append("action","wallet_up_venmo_client_token");const a=await fetch(m(),{method:"POST",body:e}),t=await a.json();if(!t.success)throw new Error(t.data?.message||"Failed to get client token");const n=await new Promise((e,a)=>{if(window.braintree?.client&&window.braintree?.venmo)return void e(window.braintree);const t=["https://js.braintreegateway.com/web/3.101.0/js/client.min.js","https://js.braintreegateway.com/web/3.101.0/js/venmo.min.js","https://js.braintreegateway.com/web/3.101.0/js/data-collector.min.js"];let n=0;t.forEach(s=>{if(document.querySelector(`script[src="${s}"]`))return n++,void(n===t.length&&e(window.braintree));const o=document.createElement("script");o.src=s,o.async=!0,o.onload=()=>{n++,n===t.length&&e(window.braintree)},o.onerror=()=>a(new Error("Failed to load Braintree SDK")),document.head.appendChild(o)})}),s=await n.client.create({authorization:t.data.client_token});try{const e=await n.dataCollector.create({client:s});k.current&&S(e.deviceData||"")}catch{}const o=await n.venmo.create({client:s,allowDesktop:!0,allowDesktopWebLogin:!0,paymentMethodUsage:"single_use"});if(!o.isBrowserSupported())throw new Error("Venmo is not supported in this browser. Please use a mobile device or the Venmo app.");k.current&&(F(o),v("ready"))}catch(e){k.current&&(C(e.message),v("failed"),j?.(e.message))}},[j]);e.useEffect(()=>{z()},[z]);const P=async()=>{if(_){v("authorizing"),C(null),b?.();try{const e=await _.tokenize();if(!e?.nonce)throw new Error("Venmo authorization failed - no payment nonce received");v("processing");const a=new FormData;a.append("action","wallet_up_venmo_process_payment"),a.append("payment_nonce",e.nonce),a.append("amount",d),a.append("order_id",p),a.append("device_data",E),a.append("email",u),a.append("first_name",w),a.append("last_name",h);const t=await fetch(m(),{method:"POST",body:a}),n=await t.json();if(!n.success)throw new Error(n.data?.message||"Payment processing failed");k.current&&(v("completed"),x?.(n.data))}catch(e){if(k.current){if("VENMO_CANCELED"===e.code||e.message?.includes("canceled"))return C(g("payment.venmo.paymentCancelled")),void v("ready");C(e.message),v("failed"),j?.(e.message)}}}},D=()=>{v("idle"),C(null),F(null),z()};return"completed"===y?a.jsxs("div",{className:`text-center p-4 bg-blue-50 rounded-lg border border-blue-200 ${f}`,children:[a.jsx(t,{className:"h-8 w-8 text-blue-600 mx-auto mb-2"}),a.jsx("p",{className:"text-blue-800 font-medium",children:g("payment.venmo.paymentSuccessful")}),a.jsx("p",{className:"text-blue-600 text-sm",children:g("payment.venmo.paymentProcessed",{amount:parseFloat(d).toFixed(2)})})]}):"failed"===y?a.jsxs("div",{className:`space-y-3 ${f}`,children:[a.jsxs(n,{variant:"destructive",children:[a.jsx(i,{className:"h-4 w-4"}),a.jsx(s,{children:N})]}),a.jsxs(o,{onClick:D,variant:"outline",size:"sm",className:"w-full",children:[a.jsx(c,{className:"h-4 w-4 mr-2"}),g("payment.venmo.tryAgain")]})]}):"loading"===y?a.jsxs("div",{className:`text-center p-3 ${f}`,children:[a.jsx(l,{className:"h-5 w-5 animate-spin text-blue-600 mx-auto mb-1"}),a.jsx("p",{className:"text-sm text-gray-500",children:g("payment.venmo.loadingVenmo")})]}):"authorizing"===y||"processing"===y?a.jsxs("div",{className:`text-center p-4 bg-blue-50 rounded-lg border border-blue-200 ${f}`,children:[a.jsx(l,{className:"h-6 w-6 animate-spin text-blue-600 mx-auto mb-2"}),a.jsx("p",{className:"text-blue-800 font-medium text-sm",children:g("authorizing"===y?"payment.venmo.authorizing":"payment.venmo.processingPayment")}),"authorizing"===y&&a.jsx("p",{className:"text-blue-600 text-xs mt-1",children:g("payment.venmo.completeInApp")})]}):N&&"ready"===y?a.jsxs("div",{className:`space-y-3 ${f}`,children:[a.jsxs(n,{children:[a.jsx(i,{className:"h-4 w-4"}),a.jsx(s,{children:N})]}),a.jsx(o,{onClick:P,className:"bg-[#008CFF] hover:bg-[#0070CC] text-white font-medium w-full",children:g("payment.venmo.payWithVenmo",{amount:parseFloat(d||0).toFixed(2)})})]}):a.jsxs(o,{onClick:P,disabled:"ready"!==y||!d,className:`bg-[#008CFF] hover:bg-[#0070CC] text-white font-medium w-full ${f}`,children:["Pay $",parseFloat(d||0).toFixed(2)," with Venmo"]})};export{d as default};
