(()=>{"use strict";const e=window.React,t=window.wp.htmlEntities,a=window.wc.wcBlocksRegistry,n=window.wp.i18n,o=window.wp.element,r=window.wc.wcSettings,s=window.wp.data,i="square_cash_app_pay",{PAYMENT_STORE_KEY:c}=window.wc.wcBlocksData;let d=null;const p=()=>{if(null!==d)return d;const e=(0,r.getSetting)("square_cash_app_pay_data",null);if(!e)throw new Error("Square Cash App Pay initialization data is not available");return d={title:e.title||"",description:e.description||"",applicationId:e.application_id||"",locationId:e.location_id||"",isSandbox:e.is_sandbox||!1,loggingEnabled:e.logging_enabled||!1,generalError:e.general_error||"",showSavedCards:e.show_saved_cards||!1,showSaveOption:e.show_save_option||!1,supports:e.supports||{},isPayForOrderPage:e.is_pay_for_order_page||!1,orderId:e.order_id||"",ajaxUrl:e.ajax_url||"",paymentRequestNonce:e.payment_request_nonce||"",continuationSessionNonce:e.continuation_session_nonce||"",gatewayIdDasherized:e.gateway_id_dasherized||"",buttonStyles:e.button_styles||{},isContinuation:e.is_continuation||!1,refereneceId:e.reference_id||""},d},l=e=>p().ajaxUrl.replace("%%endpoint%%",`square_cash_app_pay_${e}`),u=(e=!1)=>new Promise(((t,a)=>{const n={security:p().continuationSessionNonce,clear:e};jQuery.post(l("set_continuation_session"),n,(e=>e.success?t(e.data):a(e.data)))})),w=(e,t="notice")=>{p().loggingEnabled&&("error"===t?console.error(e):console.log(e))},y="wc-square-cash-app-pay",m=a=>{const[n,r]=(0,o.useState)(null),[s,c]=(0,o.useState)(!1),[d,m]=(0,o.useState)(""),{applicationId:_,locationId:h,buttonStyles:S,referenceId:g,generalError:E,gatewayIdDasherized:P,description:q}=p(),{onSubmit:C,emitResponse:b,eventRegistration:f,billing:{cartTotal:A,currency:v},components:{LoadingMask:R},activePaymentMethod:I}=a,{onPaymentSetup:M}=f;return(0,o.useEffect)((()=>M((()=>{if(!d)return{type:b.responseTypes.ERROR,message:E};const e={[`wc-${P}-payment-nonce`]:d||""};return{type:b.responseTypes.SUCCESS,meta:{paymentMethodData:e}}}))),[b.responseTypes.SUCCESS,b.responseTypes.ERROR,M,d,P,E]),(0,o.useEffect)((()=>{if(c(!1),r(null),!window.Square)return;w("[Square Cash App Pay] Initializing Square Cash App Pay Button");const e=window.Square.payments(_,h);return e?(async function t(){c(!1);try{const a=await(async e=>{const t=await new Promise(((e,t)=>{const a={security:p().paymentRequestNonce,is_pay_for_order_page:p().isPayForOrderPage||!1,order_id:p().orderId||0};jQuery.post(l("get_payment_request"),a,(a=>a.success?e(a.data):t(a.data)))})),a=JSON.parse(t);return e.paymentRequest(a)})(e);window.wcSquareCashAppPay&&(await window.wcSquareCashAppPay.destroy(),window.wcSquareCashAppPay=null);const n=await e.cashAppPay(a,{redirectURL:window.location.href,referenceId:g});await n.attach(`#${y}`,S),n.addEventListener("ontokenization",(e=>{const{tokenResult:a,error:n}=e.detail;if(n)m(""),r(n.message);else if("OK"===a.status){const e=a.token;e||(m(""),r(E)),m(e),C()}else m(null),t()})),n.addEventListener("customerInteraction",(e=>{if(e.detail&&e.detail.isMobile)return u()})),window.wcSquareCashAppPay=n,w("[Square Cash App Pay] Square Cash App Pay Button Loaded")}catch(e){r(E),console.error(e)}c(!0)}(),()=>(async()=>{window.wcSquareCashAppPay&&(await window.wcSquareCashAppPay.destroy(),window.wcSquareCashAppPay=null)})()):void 0}),[A.value,v.code,_,h,E,g]),(0,o.useEffect)((()=>{const e=document.querySelector("button.wc-block-components-checkout-place-order-button");if(e)return I!==i||d||e.setAttribute("disabled","disabled"),()=>{e.removeAttribute("disabled")}}),[I,d]),(0,e.createElement)(e.Fragment,null,(0,e.createElement)("p",null,(0,t.decodeEntities)(q||"")),n&&(0,e.createElement)("div",{className:"woocommerce-error"},n),!n&&(0,e.createElement)(R,{isLoading:!s,showSpinner:!0},(0,e.createElement)("div",{id:y})))},{title:_,applicationId:h,locationId:S}=p(),g=({RenderedComponent:t,isEdit:a,...n})=>a?null:(0,e.createElement)(t,{...n}),E={name:i,label:(0,e.createElement)((a=>{const{PaymentMethodLabel:n}=a.components;return(0,e.createElement)(n,{text:(0,t.decodeEntities)(_)})}),null),paymentMethodId:i,ariaLabel:(0,n.__)("Cash App Pay payment method","woocommerce-square"),content:(0,e.createElement)(g,{RenderedComponent:m}),edit:(0,e.createElement)(g,{RenderedComponent:m,isEdit:!0}),canMakePayment:({billingData:e,cartTotals:t})=>{const a=h&&S,n="US"===e.country,o="USD"===t.currency_code,r=a&&n&&o;return r&&(()=>{const e=document&&document.getElementById("radio-control-wc-payment-method-options-square_cash_app_pay");p().isContinuation&&!window.wcSquareCashAppPaySelected&&e&&(w("[Square Cash App Pay] Selecting Cash App Pay payment method"),(0,s.dispatch)(c).__internalSetActivePaymentMethod(i),window.wcSquareCashAppPaySelected=!0,u(!0))})(),r},supports:{features:p().supports||[],showSavedCards:p().showSavedCards||!1,showSaveOption:p().showSaveOption||!1}};(0,a.registerPaymentMethod)(E)})();