(()=>{"use strict";const e=window.React,t=window.wp.htmlEntities,a=window.wc.wcBlocksRegistry,n=window.wp.i18n,o=window.wp.element,s=window.wc.wcSettings,r=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,s.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,s]=(0,o.useState)(null),[r,c]=(0,o.useState)(!1),[d,m]=(0,o.useState)(""),S=(0,o.useRef)(d),{applicationId:_,locationId:h,buttonStyles:g,referenceId:E,generalError:P,gatewayIdDasherized:C,description:q}=p(),{onSubmit:b,emitResponse:f,eventRegistration:A,billing:{cartTotal:v,currency:R},components:{LoadingMask:I},activePaymentMethod:M}=a,{onPaymentSetup:O}=A;return(0,o.useEffect)(()=>{S.current=d,d&&b()},[d,b]),(0,o.useEffect)(()=>O(()=>{if(!S.current)return{type:f.responseTypes.ERROR,message:P};const e={[`wc-${C}-payment-nonce`]:S.current||""};return{type:f.responseTypes.SUCCESS,meta:{paymentMethodData:e}}}),[f.responseTypes.SUCCESS,f.responseTypes.ERROR,O,d,C,P]),(0,o.useEffect)(()=>{if(c(!1),s(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:E});await n.attach(`#${y}`,g),n.addEventListener("ontokenization",async e=>{const{tokenResult:a,error:n}=e.detail;if(n)m(""),s(n.message);else if("OK"===a.status){const e=a.token;e||(m(""),s(P)),m(e)}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){s(P),console.error(e)}c(!0)}(),()=>(async()=>{window.wcSquareCashAppPay&&(await window.wcSquareCashAppPay.destroy(),window.wcSquareCashAppPay=null)})()):void 0},[v.value,R.code,_,h,P,E]),(0,o.useEffect)(()=>{const e=document.querySelector("button.wc-block-components-checkout-place-order-button");if(e)return M!==i||d||e.setAttribute("disabled","disabled"),()=>{e.removeAttribute("disabled")}},[M,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)(I,{isLoading:!r,showSpinner:!0},(0,e.createElement)("div",{id:y})))},{title:S,applicationId:_,locationId:h}=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)(S)})},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,n="US"===e.country,o="USD"===t.currency_code,s=a&&n&&o;return s&&(()=>{const e=(0,r.select)(c).getAvailablePaymentMethods();Object.keys(e||{}).includes(i)&&p().isContinuation&&!window.wcSquareCashAppPaySelected&&(w("[Square Cash App Pay] Selecting Cash App Pay payment method"),(0,r.dispatch)(c).__internalSetActivePaymentMethod(i),window.wcSquareCashAppPaySelected=!0,u(!0))})(),s},supports:{features:p().supports||[],showSavedCards:p().showSavedCards||!1,showSaveOption:p().showSaveOption||!1}};(0,a.registerPaymentMethod)(E)})();