const CheckoutPage={installmentsEnabled:!1,INSTALLMENTS_SELECT_PLACHOLDER_TEXT:wc_mercadopago_custom_page_params.installments_select_placeholder_text,TAX_TYPES:{cft:{key:"CFT_",label:"CFTEA",bold:!0},tna:{key:"TNA_",label:"TNA",bold:!1},tea:{key:"TEA_",label:"TEA",bold:!1}},setElementDisplay(e,t){const r=document.querySelector(CheckoutElements[e]);r&&(r.style.display=t)},setText(e,t){const r=document.querySelector(CheckoutElements[e]);r&&(r.innerHTML=t)},setValueOn(e,t){const r=document.querySelector(CheckoutElements[e]);r&&(r.value=t)},setBackground(e,t){const r=document.querySelector(CheckoutElements[e]);r&&r.style.setProperty("background",t,"important")},setImageCard(e){const t=document.querySelector(CheckoutElements.fcCardNumberContainer);t&&(t.style.setProperty("--card-brand-icon",`url(${e})`,"important"),t.classList.add("mp-card-icon-detected"))},findContainerField(e){let t="cardholderName"===e?`#form-checkout__${e}`:`#form-checkout__${e}-container`;return Object.keys(CheckoutElements).find((e=>CheckoutElements[e]===t))},setDisplayOfError(e,t,r,n="customContent"){const s=document.querySelector(CheckoutElements[n]);if(!s)return;const o=s.querySelector(CheckoutElements[e]);o&&("add"===t?o.classList.add(`${r}`):o.classList.remove(`${r}`))},getInputHelperElement(e,t,r="customContent"){const n=document.querySelector(CheckoutElements[r]);if(n)return n.querySelector(`input-helper[input-id=${e}-${t}]`)},setDisplayOfInputHelperInfo(e,t,r="customContent"){let n=this.getInputHelperElement(e,"helper-info",r);n&&(n.childNodes[1].style.display=t)},setDisplayOfInputHelper(e,t,r="customContent"){let n=this.getInputHelperElement(e,"helper",r);if(n){n.querySelector("div").style.display=t}},setDisplayOfInputHelperMessage(e,t,r="customContent"){let n=this.getInputHelperElement(e,"helper",r);if(n){n.querySelector("div").childNodes[1].innerHTML=t}},setCvvConfig(e){this.setCvvHint(e.length),this.changeCvvPlaceHolder(e.length)},changeCvvPlaceHolder(e){3===e?window.mpCustomCheckoutHandler.cardForm.form.update("securityCode",{placeholder:wc_mercadopago_custom_page_params.security_code_placeholder_text_3_digits}):window.mpCustomCheckoutHandler.cardForm.form.update("securityCode",{placeholder:wc_mercadopago_custom_page_params.security_code_placeholder_text_4_digits})},setCvvHint(e){3===e?document.querySelector(CheckoutElements.mpSecurityCodeInfo).setAttribute("data-tooltip",wc_mercadopago_custom_page_params.security_code_tooltip_text_3_digits):document.querySelector(CheckoutElements.mpSecurityCodeInfo).setAttribute("data-tooltip",wc_mercadopago_custom_page_params.security_code_tooltip_text_4_digits)},additionalInfoHandler(e){e.cardholder_name?this.setElementDisplay("fcCardholderName","block"):this.setElementDisplay("fcCardholderName","none"),e.issuer?this.setElementDisplay("mpIssuerContainer","block"):this.setElementDisplay("mpIssuerContainer","none"),e.cardholder_identification_type&&e.cardholder_identification_number?this.setElementDisplay("mpDocumentContainer","block"):this.setElementDisplay("mpDocumentContainer","none")},getCountry:()=>wc_mercadopago_custom_checkout_params.site_id,formatCurrency(e){let t=new Intl.NumberFormat(wc_mercadopago_custom_checkout_params.intl,{currency:wc_mercadopago_custom_checkout_params.currency_code,style:"currency",currencyDisplay:"narrowSymbol"}).format(e);return"MLM"===this.getCountry()&&(t=t.replace(/^(\D+)/,"$1 ")),t},inputHelperName:e=>({cardNumber:CheckoutElements.mpCardNumber,cardholderName:CheckoutElements.mpCardholderName,expirationDate:CheckoutElements.mpExpirationDate,securityCode:CheckoutElements.mpSecurityCode,identificationNumber:CheckoutElements.mpIdentificationNumber}[e]),removeAdditionFields(){this.setElementDisplay("mpDocumentContainer","none"),this.setElementDisplay("mpInstallmentsCard","none"),this.setElementDisplay("mpIssuerContainer","none"),this.setValueOn("cardInstallments","")},getHelperMessage(e){let t="input-helper[input-id="+this.inputHelperName(e)+"-helper]";return document.querySelector(t).querySelector("div[class=mp-helper]").childNodes[1]},verifyDocument(){let e=document.querySelector(CheckoutElements.fcIdentificationNumber),t=document.querySelector(CheckoutElements.mpDocumentContainer);return"none"===t.style.display||""===t.style.display||"-1"!==e.value&&""!==e.value&&"flex"!==document.querySelector("input-helper[input-id=mp-doc-number-helper]").querySelector("div").style.display},loadAdditionalInfo(e){const t={issuer:!1,cardholder_name:!1,cardholder_identification_type:!1,cardholder_identification_number:!1};for(let r=0;r<e.length;r++)"issuer_id"===e[r]&&(t.issuer=!0),"cardholder_name"===e[r]&&(t.cardholder_name=!0),"cardholder_identification_type"===e[r]&&(t.cardholder_identification_type=!0),"cardholder_identification_number"===e[r]&&(t.cardholder_identification_number=!0);return t},clearInstallmentsComponent(){const e=document.querySelector(CheckoutElements.mpInstallmentsContainer);e?.classList?.remove(CheckoutElements.mpInstallmentsContainer),e?.firstElementChild&&e?.removeChild(e?.firstElementChild)},showInstallmentsComponent(e){const t=document.querySelector(CheckoutElements.mpInstallmentsContainer);t.classList.add(CheckoutElements.mpInstallmentsContainer),t.appendChild(e)},shouldEnableInstallmentsComponent(e){if("debit_card"===e)return this.clearInstallmentsComponent(),void(this.installmentsEnabled=!1);this.installmentsEnabled=!0},hideErrors(){const e=document.querySelector(".mp-checkout-custom-container");if(!e)return;const t=e.querySelectorAll("input-helper");t&&t.forEach((e=>{e.querySelector("div").style.display="none"}))},clearInputs(){this.hideErrors(),this.setBackground("fcCardNumberContainer","no-repeat #fff");const e=document.querySelector(CheckoutElements.fcCardNumberContainer);e&&(e.classList.remove("mp-card-icon-detected"),this.setValueOn("fcCardNumberContainer",""),this.setDisplayOfError("fcCardNumberContainer","removed","mp-error")),this.setValueOn("fcCardholderName",""),this.setDisplayOfError("fcCardholderName","removed","mp-error"),this.setValueOn("fcCardExpirationDateContainer",""),this.setDisplayOfError("fcCardExpirationDateContainer","removed","mp-error"),this.setValueOn("fcSecurityNumberContainer",""),this.setDisplayOfError("fcSecurityNumberContainer","removed","mp-error"),this.setValueOn("fcIdentificationNumber",""),this.setElementDisplay("mpDocumentContainer","none"),this.setDisplayOfError("fcIdentificationNumberContainer","removed","mp-error"),this.setDisplayOfError("mpCardholderNameInputLabel","removed","mp-label-error"),this.setDisplayOfInputHelper("mp-card-holder-name","none"),this.setDisplayOfError("mpCardholderNameInputLabel","removed","mp-label-error-2x"),this.clearInstallmentsComponent(),this.setElementDisplay("mpInstallmentsCard","none");const t=document.querySelector("input[data-cy=input-document]");t&&(t.value="")},needsBankInterestDisclaimer(){const e=this.getCountry();return"MLC"===e.toUpperCase()||"MCO"===e.toUpperCase()||"MPE"===e.toUpperCase()},getInstallments(e){const t=[];if(!e||!Array.isArray(e.payer_costs))return t;return("MCO"===this.getCountry()?e.payer_costs.slice(0,Math.min(6,e.payer_costs.length)):e.payer_costs).forEach((e=>{const r=e.installments,n=this.formatCurrency(e.installment_amount),s=0!==e.installment_rate,o=e?.installment_rate_collector,l=Array.isArray(o)&&o.includes("THIRD_PARTY"),a=this.formatCurrency(e.total_amount);let i=`${r.toString()}x `;1==r?i+=a:s?i+=`${n} (${a})`:this.needsBankInterestDisclaimer()&&l&&!s?i+=`${n} (${a})*`:i+=`${n} ${wc_mercadopago_custom_checkout_params.input_helper_message.installments.interest_free_option_text}`;const c={value:r.toString(),title:i};if("MLA"===this.getCountry()&&e.labels){const t=this.parseTaxInfo(e.labels);t&&(c.taxInfo=t)}t.push(c)})),t},parseTaxInfo(e){if(!e||!Array.isArray(e))return null;const t={cft:null,tna:null,tea:null},r=e=>{if(!e)return null;const t=e.replace("%","").trim();return/^\d+([,.]\d+)?$/.test(t)?t:null};return e.forEach((e=>{if("string"!=typeof e)return;e.split("|").forEach((e=>{if(e.includes("CFT_")){const n=e.split("CFT_");if(n.length>1&&n[1]){const e=r(n[1]);e&&(t.cft=e)}}else if(e.includes("TEA_")){const n=e.split("TEA_");if(n.length>1&&n[1]){const e=r(n[1]);e&&(t.tea=e)}}else if(e.includes("TNA_")){const n=e.split("TNA_");if(n.length>1&&n[1]){const e=r(n[1]);e&&(t.tna=e)}}}))})),t},verifyCardholderName(){const e=document.querySelector(CheckoutElements.fcCardholderName);if(!e)return!0;const t=e.value.trim(),r=t.length>2&&!/[^a-zA-ZÀ-ÿ0-9 ]/.test(t);return this.updateCardholderNameState(e,r),r},updateCardholderNameState(e,t){const r=document.querySelector(CheckoutElements.mpCardHolderNameHelper),n=document.querySelector(CheckoutElements.mpCardholderNameInputLabel),s=document.querySelector(CheckoutElements.mpCardHolderNameHelperInfo);t?(this.toggleErrorBorder(e),n?.classList.remove("mp-label-error"),r&&(r.style.display="none"),document.activeElement===e&&(e.classList.add("mp-focus"),s.style.display="flex")):(e.classList.remove("mp-focus"),r&&(r.style.display="flex",s.style.display="none"),n&&(n.classList.add("mp-label-error"),s.style.display="none"),this.toggleErrorBorder(e,document.activeElement===e))},verifyCardholderNameOnFocus(){const e=document.querySelector(CheckoutElements.fcCardholderName),t=document.querySelector(CheckoutElements.mpCardHolderNameHelper),r=document.querySelector(CheckoutElements.mpCardholderNameInputLabel),n=document.querySelector(CheckoutElements.mpCardHolderNameHelperInfo);e&&(e.addEventListener("focus",(()=>{e.classList.contains("mp-error")||e.classList.contains("mp-error-2px")?e.classList.contains("mp-error")&&(this.toggleErrorBorder(e,!0),n.style.display="none"):(e.classList.add("mp-focus"),n.style.display="flex")})),e.addEventListener("blur",(()=>{const s=e.classList.contains("mp-error-2px");e.classList.remove("mp-focus"),this.toggleErrorBorder(e),t&&(t.style.display="none"),r?.classList.remove("mp-label-error"),n.style.display="flex",(""!==e.value.trim()||s)&&this.verifyCardholderName()})),e.addEventListener("input",(()=>{""!==e.value.trim()?this.verifyCardholderName():this.updateCardholderNameState(e,!0)})))},addDefaultOptionOnInstallmentsSelect(e){const t=document.createElement("option");t.value="",t.disabled=!0,t.selected=!0,t.innerHTML=this.INSTALLMENTS_SELECT_PLACHOLDER_TEXT,e.insertBefore(t,e.firstChild)},setChangeEventOnInstallments(e){this.clearInstallmentsComponent();const t=document.getElementById("form-checkout__installments");if(t){if(this.installmentsItemsData=e||[],this.addDefaultOptionOnInstallmentsSelect(t),!this.installmentsEnabled)return CheckoutPage.setValueOn("cardInstallments","1"),void(t.value="1");this.replaceInstallmentsOptions(t,e),this.renderBankInterestDisclaimer(e),t.addEventListener("change",(e=>{const r=e.target.value;r&&(CheckoutPage.setValueOn("cardInstallments",r),t.value=r,this.updateTaxInfoForSelect(r,"mp-installments-tax-info"),this.setInstallmentsErrorState(!1))})),t.addEventListener("focus",(()=>{t.classList.contains("mp-error")&&this.toggleErrorBorder(t,!0)})),t.addEventListener("blur",(()=>{this.installmentsWasSelected()?this.setInstallmentsErrorState(!1):this.setInstallmentsErrorState(!0)})),this.setElementDisplay("mpInstallmentsCard","block")}},replaceInstallmentsOptions(e,t){const r=this.getInstallments(t);r.length&&(Array.from(e.querySelectorAll("option")).filter((e=>!e.disabled)).forEach((e=>e.remove())),r.forEach((({value:t,title:r})=>{const n=document.createElement("option");n.value=t,n.textContent=r,e.appendChild(n)})))},hasThirdPartyInterestFreeInstallment:e=>!(!e||!Array.isArray(e.payer_costs))&&e.payer_costs.some((e=>{const t=e?.installment_rate_collector;return Array.isArray(t)&&t.includes("THIRD_PARTY")&&0===e?.installment_rate})),renderBankInterestDisclaimer(e){const t=document.getElementById("mp-installments-bank-interest-hint");if(t&&t.remove(),!this.needsBankInterestDisclaimer())return;if(!this.hasThirdPartyInterestFreeInstallment(e))return;const r=wc_mercadopago_custom_checkout_params?.input_helper_message?.installments?.bank_interest_hint_text;if(!r)return;const n=document.querySelector(".mp-checkout-custom-installments-select-container");if(!n)return;const s=document.createElement("div");s.id="mp-installments-bank-interest-hint",s.className="mp-installments-bank-interest-hint",s.textContent=`*${r}`;const o=document.getElementById("mp-installments-tax-info");o?n.insertBefore(s,o):n.appendChild(s)},installmentsWasSelected(){const e=document.getElementById("form-checkout__installments");return!!e?.value},scrollToCheckoutCustomContainer(){const e=document.querySelector(".mp-checkout-custom-container");e&&e.scrollIntoView({behavior:"smooth"})},toggleErrorBorder(e,t){e.classList.remove("mp-error","mp-error-2px"),!0===t?e.classList.add("mp-error-2px"):!1===t&&e.classList.add("mp-error")},setInstallmentsErrorState(e){const t=document.getElementById("form-checkout__installments"),r=document.querySelector(".mp-checkout-custom-installments-select-container .mp-input-label"),n=document.querySelector("#mp-installments-error");if(t&&r&&n)if(e){const e=document.activeElement===t;n.style.display="flex",r.classList.add("mp-label-error"),this.toggleErrorBorder(t,e)}else n.style.display="none",this.toggleErrorBorder(t),r.classList.remove("mp-label-error")},updateTaxInfoForSelect(e,t,r){const n=r||this.installmentsItemsData?.payer_costs,s=document.getElementById(t);if(!s)return;if("MLA"!==this.getCountry()||parseInt(e,10)<2)return void(s.style.display="none");const o=n?.find((t=>`${t.installments}`==`${e}`)),l=this.parseTaxInfo(o?.labels),a=this.formatTaxDisplay(l);s.style.display=a?"block":"none",a&&(s.innerHTML=a)},parseTaxInfo(e){if(!Array.isArray(e))return{};const t=e=>{const t=e?.replace("%","").trim();return/^\d+([,.]\d+)?$/.test(t)?t:null};return e.filter((e=>"string"==typeof e)).flatMap((e=>e.split("|"))).reduce(((e,r)=>{for(const[n,{key:s}]of Object.entries(this.TAX_TYPES))if(r.includes(s)){const o=t(r.split(s)[1]);o&&(e[n]=o)}return e}),{})},formatTaxDisplay(e){if(!e)return null;const t=Object.entries(this.TAX_TYPES).filter((([t])=>e[t])).map((([t,{label:r,bold:n}])=>{const s=`${r}: ${e[t]}%`;return n?`<b>${s}</b>`:s}));return t.length?`${t.join(" - ")}. Tasa fija.`:null}};