class MPErrorMessageNormalizer{static DEFAULT_ERROR_MESSAGE="empty error message";static normalize(e){return e&&e.replace(/[\t\n]/g,"").trim()||this.DEFAULT_ERROR_MESSAGE}}class MPCustomEventDispatcher{static dispatch(e,r){document.dispatchEvent(new CustomEvent(e,{detail:r}))}}class MPDispatchedErrorTracker{static#e=new Set;static track(e,r){const t=`${r}::${e}`;return!this.#e.has(t)&&(this.#e.add(t),!0)}static reset(){this.#e.clear()}}class MPCheckoutErrorHandler{handle(){throw new Error("Method must be implemented")}}class MPClassicCheckoutErrorHandler extends MPCheckoutErrorHandler{#r="checkout_error";#t="form[name=checkout]";#o=".woocommerce-error";ERROR_EVENT_NAME="mp_checkout_error";handle(){document.querySelector(this.#t)&&jQuery(document.body).on(this.#r,((e,r)=>{const t=document.querySelector(this.#o),o=t?.textContent||r,s=MPErrorMessageNormalizer.normalize(o),a=!!t?.querySelector('li[data-mp-source="plugin"]')?"post_submit_mercado_pago":"post_submit_woocommerce";MPCustomEventDispatcher.dispatch(this.ERROR_EVENT_NAME,{message:s,errorOrigin:a})}))}}class MPOrderPayCheckoutErrorHandler extends MPCheckoutErrorHandler{#s="form#order_review";#a=[".woocommerce-notices-wrapper .woocommerce-error","#order_review > .woocommerce-error"];ERROR_EVENT_NAME="mp_checkout_error";handle(){const e=document.querySelector(this.#s);e&&(e.addEventListener("submit",(()=>MPDispatchedErrorTracker.reset())),this.handlePageLoadErrors(),this.observeForDynamicErrors(e))}findAllErrorElements(){const e=[];for(const r of this.#a)e.push(...document.querySelectorAll(r));return e}dispatchError(e){const r=MPErrorMessageNormalizer.normalize(e.textContent),t=!!e.querySelector('li[data-mp-source="plugin"]')?"post_submit_mercado_pago":"post_submit_woocommerce";MPDispatchedErrorTracker.track(r,t)&&MPCustomEventDispatcher.dispatch(this.ERROR_EVENT_NAME,{message:r,errorOrigin:t})}dispatchAllErrors(e){e.forEach((e=>this.dispatchError(e)))}handlePageLoadErrors(){const e=this.findAllErrorElements();e.length&&this.waitForMelidata().then((()=>this.dispatchAllErrors(e)))}waitForMelidata(){return new Promise((e=>{window.melidata?e():window.melidataReady&&"function"==typeof window.melidataReady.then?window.melidataReady.then(e).catch(e):window.melidataReady?e():"complete"!==document.readyState?window.addEventListener("load",(()=>{window.melidataReady&&"function"==typeof window.melidataReady.then?window.melidataReady.then(e).catch(e):e()}),{once:!0}):e()}))}observeForDynamicErrors(e){[document.querySelector(".woocommerce-notices-wrapper"),e].filter(Boolean).forEach((e=>{new MutationObserver((e=>{if(!e.some((e=>e.addedNodes.length>0)))return;const r=this.findAllErrorElements();this.dispatchAllErrors(r)})).observe(e,{childList:!0})}))}}class MPBlocksCheckoutErrorHandler extends MPCheckoutErrorHandler{ERROR_EVENT_NAME="mp_checkout_error";handle(e){const r=e?.processingResponse?.paymentDetails?.message,t=MPErrorMessageNormalizer.normalize(r);MPCustomEventDispatcher.dispatch(this.ERROR_EVENT_NAME,{message:t,errorOrigin:"post_submit_mercado_pago"})}}class MPCheckoutErrorDispatcher{constructor(){this.handlers=new Map([["classic",new MPClassicCheckoutErrorHandler],["orderPay",new MPOrderPayCheckoutErrorHandler],["blocks",new MPBlocksCheckoutErrorHandler]])}init(){document.addEventListener("DOMContentLoaded",(()=>{this.handlers.get("classic").handle(),this.handlers.get("orderPay").handle()}))}handleBlocksError(e){this.handlers.get("blocks").handle(e)}static dispatchEventWhenBlocksCheckoutErrorOccurred(e){(new MPCheckoutErrorDispatcher).handleBlocksError(e)}}(new MPCheckoutErrorDispatcher).init();