import{r as e,j as a}from"./client.ySKnF1Cn.js";import{C as t,A as s,d as n,B as r}from"./alert.L02_PIIX.js";import{u as c}from"./useTranslation.Cb9bYI5g.js";import{C as p}from"./circle-alert.D9xMeaAw.js";import{R as l}from"./refresh-cw.D_zjBwcO.js";import{L as i}from"./loader-circle.BwcL2Xcc.js";import"./context.ZwV2gUb-.js";const o=()=>window.walletUpData?.ajaxUrl||window.ajaxurl||"/wp-admin/admin-ajax.php",d=({amount:d,currency:m="USD",referenceId:u="",redirectUrl:h="",onSuccess:g,onError:x,onProcessing:w,className:f=""})=>{const{t:y}=c(),[j,_]=e.useState("idle"),[b,N]=e.useState(null),[A,F]=e.useState(null),P=e.useRef(null),v=e.useRef(0),C=e.useCallback(()=>{P.current&&(clearInterval(P.current),P.current=null),v.current=0},[]);e.useEffect(()=>C,[C]);const D=e=>{v.current=0,P.current=setInterval(async()=>{if(v.current++,v.current>60)return C(),N(y("payment.cashApp.requestTimedOut")),void _("failed");try{const a=new FormData;a.append("action","wallet_up_cashapp_api_check_status"),a.append("request_id",e);const t=await fetch(o(),{method:"POST",body:a}),s=await t.json();if(!s.success)return;const n=s.data.status;"APPROVED"===n&&s.data.grants?.length>0?(C(),_("polling"),await E(s.data.grants[0].id)):"DECLINED"!==n&&"EXPIRED"!==n||(C(),N(`Payment ${n.toLowerCase()}. Please try again.`),_("failed"),x?.(`Payment ${n.toLowerCase()}`))}catch{}},5e3)},E=async e=>{try{const a=new FormData;a.append("action","wallet_up_cashapp_api_capture"),a.append("grant_id",e),a.append("amount",d),a.append("currency",m),a.append("reference_id",u);const t=await fetch(o(),{method:"POST",body:a}),s=await t.json();if(!s.success)throw new Error(s.data?.message||"Payment capture failed");_("completed"),g?.(s.data)}catch(a){N(a.message),_("failed"),x?.(a.message)}},S=()=>{C(),_("idle"),N(null),F(null)};return"completed"===j?a.jsxs("div",{className:`text-center p-4 bg-green-50 rounded-lg border border-green-200 ${f}`,children:[a.jsx(t,{className:"h-8 w-8 text-green-600 mx-auto mb-2"}),a.jsx("p",{className:"text-green-800 font-medium",children:y("payment.cashApp.paymentSuccessful")}),a.jsx("p",{className:"text-green-600 text-sm",children:y("payment.cashApp.paymentCaptured",{amount:parseFloat(d).toFixed(2)})})]}):"failed"===j?a.jsxs("div",{className:`space-y-3 ${f}`,children:[a.jsxs(s,{variant:"destructive",children:[a.jsx(p,{className:"h-4 w-4"}),a.jsx(n,{children:b})]}),a.jsxs(r,{onClick:S,variant:"outline",size:"sm",className:"w-full",children:[a.jsx(l,{className:"h-4 w-4 mr-2"}),y("payment.cashApp.tryAgain")]})]}):"ready"===j||"polling"===j?a.jsxs("div",{className:`text-center p-4 bg-green-50 rounded-lg border border-green-200 ${f}`,children:[a.jsx(i,{className:"h-6 w-6 animate-spin text-green-600 mx-auto mb-2"}),a.jsx("p",{className:"text-green-800 font-medium text-sm",children:y("polling"===j?"payment.cashApp.capturingPayment":"payment.cashApp.waitingForApproval")}),a.jsx("p",{className:"text-green-600 text-xs mt-1",children:y("payment.cashApp.openCashApp",{amount:parseFloat(d).toFixed(2)})})]}):a.jsx(r,{onClick:async()=>{_("loading"),N(null);try{const e=new FormData;e.append("action","wallet_up_cashapp_api_create_request"),e.append("amount",d),e.append("currency",m),e.append("reference_id",u),e.append("redirect_url",h||window.location.href);const a=await fetch(o(),{method:"POST",body:e}),t=await a.json();if(!t.success)throw new Error(t.data?.message||"Failed to create payment request");F(t.data.request_id),_("ready"),w?.(),D(t.data.request_id)}catch(e){N(e.message),_("failed"),x?.(e.message)}},disabled:"loading"===j||!d,className:`bg-[#00D632] hover:bg-[#00B82B] text-white font-medium w-full ${f}`,children:"loading"===j?a.jsxs(a.Fragment,{children:[a.jsx(i,{className:"h-4 w-4 mr-2 animate-spin"}),y("payment.cashApp.connecting")]}):a.jsx(a.Fragment,{children:y("payment.cashApp.payWithCashApp",{amount:parseFloat(d||0).toFixed(2)})})})};export{d as default};
