(()=>{"use strict";const e=window.wp.htmlEntities,n=window.wp.element,t=window.ReactJSXRuntime,{registerPaymentMethod:r}=window.wc.wcBlocksRegistry,{getSetting:s}=window.wc.wcSettings,o=s("china_payments_stripe_unionpay_data",{}),i=(0,e.decodeEntities)(o.title),a=()=>o.icon?(0,t.jsx)("img",{src:o.icon,style:{height:"24px",width:"auto",maxWidth:"120px",marginLeft:"auto"}}):"",c=()=>(0,t.jsxs)("span",{style:{width:"100%",display:"flex",alignItems:"center"},children:[i,(0,t.jsx)(a,{})]}),p=({eventRegistration:e,emitResponse:r})=>{const s=(0,n.useRef)(null),i=(0,n.useRef)(null),a=(0,n.useRef)(!1),[c,p]=(0,n.useState)(""),[l,d]=(0,n.useState)(!1);return(0,n.useEffect)(()=>{if(a.current)return;const e=o.stripe_publishable_key;if(!e)return p("Stripe configuration error. Please contact the store administrator."),void console.error("[UnionPay] Missing stripe_publishable_key in settings:",o);const n=()=>{if("function"==typeof window.Stripe)try{i.current=window.Stripe(e);const n=i.current.elements();if(!n)return void console.error("[UnionPay] stripe.elements() returned falsy");s.current=n.create("card",{style:{base:{fontSize:"16px",color:"#32325d",fontFamily:'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif',"::placeholder":{color:"#aab7c4"}},invalid:{color:"#fa755a",iconColor:"#fa755a"}},hidePostalCode:!0});const t=document.getElementById("china-payments-unionpay-card-element");t?(s.current.mount(t),a.current=!0,d(!0),s.current.on("change",e=>{p(e.error?e.error.message:"")}),s.current.on("ready",()=>{console.log("[UnionPay] Card Element ready")})):console.error("[UnionPay] Mount element not found")}catch(e){console.error("[UnionPay] Error initializing Stripe:",e),p("Failed to initialize payment form. Please refresh the page.")}else console.error("[UnionPay] window.Stripe is not a function:",typeof window.Stripe)};if("function"!=typeof window.Stripe){let e=0;const t=setInterval(()=>{e++,"function"==typeof window.Stripe?(clearInterval(t),n()):e>50&&(clearInterval(t),console.error("[UnionPay] Stripe.js failed to load after 10 seconds"),p("Payment system failed to load. Please refresh the page."))},200);return()=>clearInterval(t)}n()},[]),(0,n.useEffect)(()=>e.onPaymentSetup(()=>a.current?{type:r.responseTypes.SUCCESS,meta:{}}:{type:r.responseTypes.ERROR,message:"Card element not ready. Please wait and try again."}),[e,r]),(0,n.useEffect)(()=>e.onCheckoutSuccess(async({redirectUrl:e})=>{if(!e||!e.includes("china-payments-confirm-card"))return{type:r.responseTypes.SUCCESS};const n=new URL(e),t=n.searchParams.get("china-payments-intent-secret"),a=n.searchParams.get("china-payments-return");if(!t)return console.error("[UnionPay] Missing client secret in redirect URL"),{type:r.responseTypes.SUCCESS};if(!i.current||!s.current)return console.error("[UnionPay] Stripe not initialized for payment confirmation"),{type:r.responseTypes.ERROR,message:"Payment form not ready. Please refresh and try again."};try{const n=await i.current.confirmCardPayment(t,{payment_method:{card:s.current}});if(n.error)return{type:r.responseTypes.ERROR,message:n.error.message};if(n.paymentIntent&&"succeeded"===n.paymentIntent.status){const n=o.payment_completed_url||"",t=a?decodeURIComponent(a):"",s=t.match(/order-received\/(\d+)/),i=s?s[1]:"";return n&&i?{type:r.responseTypes.SUCCESS,redirectUrl:n+i}:{type:r.responseTypes.SUCCESS,redirectUrl:t||e.split("?")[0]}}return{type:r.responseTypes.ERROR,message:"Payment was not completed. Please try again."}}catch(e){return console.error("[UnionPay] Payment confirmation error:",e),{type:r.responseTypes.ERROR,message:e.message||"An unexpected error occurred."}}}),[e,r]),(0,t.jsxs)("div",{style:{padding:"10px 0"},children:[(0,t.jsx)("div",{id:"china-payments-unionpay-card-element",style:{padding:"12px",border:"1px solid #e0e0e0",borderRadius:"6px",background:"#fff",minHeight:"24px"}}),c&&(0,t.jsx)("div",{style:{color:"#fa755a",fontSize:"13px",marginTop:"8px"},children:c})]})};r({name:"china_payments_stripe_unionpay",label:(0,t.jsx)(c,{}),content:(0,t.jsx)(p,{}),edit:(0,t.jsx)("div",{children:(0,e.decodeEntities)(o.description||"")}),canMakePayment:()=>!0,ariaLabel:i,supports:{features:o.supports}})})();