class MPThreeDSHandler{constructor(){this.threedsTarget="mp_custom_checkout_security_fields_client"}set3dsStatusValidationListener(){window.addEventListener("message",(e=>{"COMPLETE"===e.data.status&&(this.sendMetric("MP_THREE_DS_SUCCESS","3DS iframe Closed",this.threedsTarget),document.getElementById("mp-3ds-modal-content").innerHTML="",this.addLoadSpinner3dsSubmit(),this.redirectAfter3dsChallenge())}))}load3DSFlow(e){var t=document.createElement("div");t.setAttribute("id","mp-3ds-modal-container"),t.className="mp-3ds-modal";var d=document.createElement("div");d.id="mp-3ds-modal-content",d.innerHTML='<div><div id="mp-modal-3ds-title"><span id="mp-3ds-title"></span><span id="mp-3ds-modal-close" >&times;</span></div><div id="mp-loading-container-3ds">   <div>     <div class="mp-spinner-3ds"></div>       <div class="mp-loading-text-3ds">         <p>'+wc_mercadopago_custom_checkout_params.threeDsText.title_loading+"<br>           ("+document.getElementById("paymentMethodId").value+"****"+e+") "+wc_mercadopago_custom_checkout_params.threeDsText.title_loading2+'          </p>       </div>       <p class="mp-normal-text-3ds">'+wc_mercadopago_custom_checkout_params.threeDsText.text_loading+"</p>   </div> <div></div>",t.appendChild(d),document.body.appendChild(t),document.querySelector("#mp-3ds-modal-close").addEventListener("click",(()=>{this.setDisplayOfErrorCheckout(wc_mercadopago_custom_checkout_params.threeDsText.message_close),this.removeModal3ds()})),jQuery.post(woocommerce_params.wc_ajax_url.replace("%%endpoint%%","mp_get_3ds_from_session")).done((e=>{if(e.success){var t=e.data.data["3ds_url"],d=e.data.data["3ds_creq"];this.threeDSHandler(t,d)}else console.error("Error POST:",e),window.dispatchEvent(new CustomEvent("completed_3ds",{detail:{error:!0}})),this.removeModal3ds()})).fail(((e,t,d)=>{console.error("Failed to make POST:",t,d),"function"==typeof window.sendMetric&&window.sendMetric(String(e?.status??0),`3DS flow failed: ${t} ${d}`,"mp_api_error",{api_route:"3ds_challenge"}),window.dispatchEvent(new CustomEvent("completed_3ds",{detail:{error:!0}})),this.removeModal3ds()}))}threeDSHandler(e,t){try{if(null==e||null==t)return this.removeModal3ds(),this.sendMetric("MP_THREE_DS_ERROR","3DS URL or CRED not set",this.threedsTarget),void console.log("Invalid parameters for 3ds");var d=document.createElement("div");d.className="mp-card-info",d.innerHTML='<div class="mp-alert-color-success"></div><div class="mp-card-body-3ds"><div class="mp-icon-badge-info"></div><div><span class="mp-text-subtitle">'+wc_mercadopago_custom_checkout_params.threeDsText.title_frame+"</span></div></div>";var o=document.getElementById("mp-3ds-modal-content"),r=document.createElement("iframe");r.name="mp-3ds-frame",r.id="mp-3ds-frame",r.onload=()=>this.removeLoadSpinner3ds(),document.getElementById("mp-3ds-title").innerText=wc_mercadopago_custom_checkout_params.threeDsText.tooltip_frame,o.appendChild(d),o.appendChild(r);var a=r.contentWindow.document,s=a.createElement("form");s.name="mp-3ds-frame",s.className="mp-modal",s.setAttribute("target","mp-3ds-frame"),s.setAttribute("method","post"),s.setAttribute("action",e);var i=a.createElement("input");i.setAttribute("type","hidden"),i.setAttribute("name","creq"),i.setAttribute("value",t),s.appendChild(i),r.appendChild(s),s.submit()}catch(e){this.sendMetric("MP_THREE_DS_ERROR","3DS Loading error: "+e,this.threedsTarget),alert("Error doing Challenge, try again later.")}}redirectAfter3dsChallenge(){jQuery.post(woocommerce_params.wc_ajax_url.replace("%%endpoint%%","mp_redirect_after_3ds_challenge")).done((e=>{e.data.redirect?(window.dispatchEvent(new CustomEvent("completed_3ds",{detail:{error:!1}})),this.sendMetric("MP_THREE_DS_SUCCESS","3DS challenge complete",this.threedsTarget),this.removeModal3ds(),window.location.href=e.data.redirect):(window.dispatchEvent(new CustomEvent("completed_3ds",{detail:{error:e.data.data.error}})),this.setDisplayOfErrorCheckout(e.data.data.error),this.removeModal3ds())}))}removeLoadSpinner3ds(){var e=document.getElementById("mp-loading-container-3ds");e&&e.remove()}addLoadSpinner3dsSubmit(){document.getElementById("mp-3ds-modal-content").innerHTML='<div id="mp-loading-container-3ds">   <div>     <div class="mp-spinner-3ds"></div>       <div class="mp-loading-text-3ds">         <p>'+wc_mercadopago_custom_checkout_params.threeDsText.title_loading_response+"          </p>       </div>   </div> <div>"}removeModal3ds(){CheckoutPage.clearInputs(),document.getElementById("mp-3ds-modal-container").remove()}setDisplayOfErrorCheckout(e){this.sendMetric("MP_THREE_DS_ERROR",e,this.threedsTarget),"blocks_checkout_form"!==window.mpFormId&&this.addErrorAlert(e)}addErrorAlert(e){this.removeElementsByClass("woocommerce-NoticeGroup-checkout"),jQuery("form[name=checkout]").prepend(`\n            <div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">\n                <ul class="woocommerce-error" role="alert">\n                    <li>${e}<li>\n                </ul>\n            </div>\n        `),window.scrollTo(0,0)}removeElementsByClass(e){const t=document.getElementsByClassName(e);for(;t.length>0;)t[0].parentNode.removeChild(t[0])}sendMetric(e,t,d){void 0!==window.mPmetrics&&window.mPmetrics.push({action:e,label:t,target:d})}};