let isProcessingOrder=!1,lastExecutionTime=0;const debounceInterval=1e3;let invoiceCreatedForOrder=null,originalButtonText=null;const setButtonProcessing=function(e){const t=document.querySelector(".wc-block-components-checkout-place-order-button__text");t&&(e?(originalButtonText=t.textContent,t.textContent=BTCPayWP.textProcessingButton||"Processing…"):originalButtonText&&(t.textContent=originalButtonText,originalButtonText=null))};wp.data.subscribe((()=>{const e=Date.now();if(!(e-lastExecutionTime<1e3)&&wp.data.select(wc.wcBlocksData.CHECKOUT_STORE_KEY).isBeforeProcessing()&&!isProcessingOrder){isProcessingOrder=!0,lastExecutionTime=e;const t=wp.data.select(wc.wcBlocksData.PAYMENT_STORE_KEY).getState().activePaymentMethod;if(t.startsWith("btcpaygf_")){setButtonProcessing(!0);const e=wp.data.select(wc.wcBlocksData.VALIDATION_STORE_KEY);if(e&&e.hasValidationErrors())return wp.data.dispatch(wc.wcBlocksData.VALIDATION_STORE_KEY).showAllValidationErrors(),void(isProcessingOrder=!1);let o=blocksProcessOrder(t);return o?(resetCheckout(),blocksShowBTCPayModal(o),!1):(blocksSubmitError(BTCPayWP.textProcessingError),isProcessingOrder=!1,!1)}return!0}}));const blocksProcessOrder=function(e){let t=null;const o=wp.data.select(wc.wcBlocksData.CHECKOUT_STORE_KEY).getOrderId();if(o&&o===invoiceCreatedForOrder)return null;let r={action:"btcpaygf_modal_blocks_checkout",orderId:o,paymentGateway:e,apiNonce:BTCPayWP.apiNonce};return jQuery.ajaxSetup({async:!1}),jQuery.post(wc_add_to_cart_params.ajax_url,r,(function(e){e.data.invoiceId?(invoiceCreatedForOrder=o,t=e.data):e.data?blocksSubmitError(e.data):blocksSubmitError(BTCPayWP.textProcessingError)})).fail((function(){blocksSubmitError(BTCPayWP.textProcessingError),console.error("Error on ajax request 2")})),jQuery.ajaxSetup({async:!0}),t},blocksShowBTCPayModal=function(e){void 0!==e.invoiceId&&(window.btcpay.setApiUrlPrefix(BTCPayWP.apiUrl),window.btcpay.onModalWillEnter((function(){setButtonProcessing(!1)})),window.btcpay.showInvoice(e.invoiceId));let t=!1;window.btcpay.onModalReceiveMessage((function(r){if(o(r.data)){if(r.data.status)switch(r.data.status.toLowerCase()){case"complete":case"paid":case"processing":case"settled":t=!0,setTimeout((function(){window.location=e.orderCompleteLink}),3e3);break;case"expired":window.btcpay.hideFrame(),isProcessingOrder=!1,invoiceCreatedForOrder=null,blocksSubmitError(BTCPayWP.textInvoiceExpired);break;case"invalid":window.btcpay.hideFrame(),isProcessingOrder=!1,invoiceCreatedForOrder=null,blocksSubmitError(BTCPayWP.textInvoiceInvalid)}}else"close"===r.data&&(!0===t&&(window.location=e.orderCompleteLink),isProcessingOrder=!1,invoiceCreatedForOrder=null,blocksSubmitError(BTCPayWP.textModalClosed))}));const o=e=>"[object Object]"===Object.prototype.toString.call(e)},blocksSubmitError=function(e){setButtonProcessing(!1),window.wp.data.dispatch("core/notices").createErrorNotice(e,{context:"wc/checkout"}),resetCheckout()},resetCheckout=function(){wp.data.dispatch(wc.wcBlocksData.CHECKOUT_STORE_KEY).__internalSetIdle()};