(()=>{"use strict";jQuery(document).ready(e=>{window.WC_Square_Cash_App_Pay_Handler=class{constructor(e){this.args=e,this.payment_request=e.payment_request||{},this.isPayForOrderPage=e.is_pay_for_order_page,this.orderId=e.order_id,this.orderKey=e.order_key||"",this.id_dasherized=e.gateway_id_dasherized,this.buttonStyles=e.button_styles,this.referenceId=this.reference_id,this.cashAppButton="#wc-square-cash-app",this.settingUp=!1,this.build_cash_app(),this.attach_page_events()}build_cash_app(){if(!this.settingUp&&0!==e(document).find(this.cashAppButton).length)return this.settingUp=!0,this.block_ui(),this.get_payment_request().then(t=>{const a=JSON.stringify(this.payment_request);return this.payment_request=JSON.parse(t),this.total_amount=this.payment_request.total.amount,this.has_payment_nonce()&&e("#wc-square-cash-app #cash_app_pay_v1_element").length&&JSON.stringify(this.payment_request)===a?(this.settingUp=!1,void this.unblock_ui()):(e(this.cashAppButton).hide(),e(`input[name=wc-${this.id_dasherized}-payment-nonce]`).val(""),this.load_cash_app_form())},t=>{this.log("[Square Cash App Pay] Could not build payment request. "+t,"error"),e(this.cashAppButton).hide(),this.unblock_ui(),this.settingUp=!1})}attach_page_events(){e(document.body).on("updated_checkout",()=>this.build_cash_app()),e(document.body).on("payment_method_selected",()=>this.toggle_order_button())}async load_cash_app_form(){this.log("[Square Cash App Pay] Building Cash App Pay");const{applicationId:e,locationId:t}=this.get_form_params();this.payments=window.Square.payments(e,t),await this.initializeCashAppPay(),this.unblock_ui(),this.log("[Square Cash App Pay] Square Cash App Pay Button Loaded"),this.settingUp=!1}async initializeCashAppPay(){if(!this.payments)return;const t=this.payments.paymentRequest(this.create_payment_request());this.cashAppPay&&await this.cashAppPay.destroy(),this.cashAppPay=await this.payments.cashAppPay(t,{redirectURL:window.location.href,referenceId:this.referenceId}),await this.cashAppPay.attach("#wc-square-cash-app",this.buttonStyles),this.cashAppPay.addEventListener("ontokenization",e=>this.handleCashAppPaymentResponse(e)),this.toggle_order_button(),this.cashAppPay&&e(this.cashAppButton).show()}handleCashAppPaymentResponse(t){this.blockedForm=this.blockForm();const{tokenResult:a,error:s}=t.detail;if(this.log_data(t.detail,"response"),s)this.render_errors([s.message]),this.blockedForm&&this.blockedForm.unblock();else if("OK"===a.status){const t=a.token;if(!t)return this.blockedForm&&this.blockedForm.unblock(),this.render_errors(this.args.general_error);e(`input[name=wc-${this.id_dasherized}-payment-nonce]`).val(t),e("input#payment_method_square_cash_app_pay").is(":checked")||(e("input#payment_method_square_cash_app_pay").trigger("click"),e("input#payment_method_square_cash_app_pay").attr("checked",!0)),this.toggle_order_button(),e("#order_review").length?e("#order_review").trigger("submit"):e("form.checkout").trigger("submit")}else this.blockedForm&&this.blockedForm.unblock(),this.build_cash_app()}blockForm(){const t=e("form.checkout, form#order_review");return t.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),t}get_form_params(){return{applicationId:this.args.application_id,locationId:this.args.location_id}}create_payment_request(){return this.payment_request}get_payment_request(){return new Promise((t,a)=>{const s={security:this.args.payment_request_nonce,is_pay_for_order_page:this.isPayForOrderPage,order_id:this.orderId,check_for_giftcard:!this.isPayForOrderPage,...this.isPayForOrderPage&&this.orderKey&&{order_key:this.orderKey}};e.post(this.get_ajax_url("get_payment_request"),s,e=>e.success?t(e.data):a(e.data))})}get_ajax_url(e){return this.args.ajax_url.replace("%%endpoint%%","square_cash_app_pay_"+e)}render_errors_html(t){e(".woocommerce-error, .woocommerce-message").remove();const a=e('form[name="checkout"]');a.before(t),this.blockedForm&&this.blockedForm.unblock(),e("html, body").animate({scrollTop:a.offset().top-100},1e3)}render_errors(e){const t='<ul class="woocommerce-error"><li>'+e.join("</li><li>")+"</li></ul>";this.render_errors_html(t)}block_ui(){e(".woocommerce-checkout-payment, #payment").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})}unblock_ui(){e(".woocommerce-checkout-payment, #payment").unblock()}log_data(t,a){if(!this.args.logging_enabled)return;const s={security:this.args.ajax_log_nonce,type:a,data:t};e.ajax({url:this.get_ajax_url("log_js_data"),data:s})}log(e,t="notice"){if(this.args.checkout_logging)return"error"===t?console.error(e):console.log(e)}has_payment_nonce(){return e(`input[name=wc-${this.id_dasherized}-payment-nonce]`).val()}get_selected_gateway_id(){return e("form.checkout, form#order_review").find("input[name=payment_method]:checked").val()}toggle_order_button(){this.get_selected_gateway_id()!==this.args.gateway_id||this.has_payment_nonce()?e("#place_order").show():e("#place_order").hide()}}})})();