class NMPTestMode extends HTMLElement{constructor(){super(),this.config={dom:{modal:".ac-modal-trigger[data-ajax-url], .ac-modal-trigger[data-modal-target]",buttons:".js-nmp-button",options:".js-nmp-option",selects:".js-nmp-select",active_price:".js-nmp-active-price",total:".js-nmp-total-price",frequency:".js-nmp-total-frequency",terms:".js-nmp-terms",cto_length:".js-cto-finance-length",cto_bank:".js-cto-finance-bank"},cls:{active:"active"}},this.modals=document.querySelectorAll(this.config.dom.modal)||[],this?.modals?.length>0&&(this.addModalEvents(),window.refreshNmpAddModalEvents=()=>{this.modals=document.querySelectorAll(this.config.dom.modal)||[],this.addModalEvents()})}async getNMPTerms(handle,isCart){const url=isCart?"/?section_id=nmp-test-mode-plan-cart":`/products/${handle}?view=nmp`,$terms=this.querySelector(this.config.dom.terms)||null,$html=await(await fetch(url)).text()||null;$terms&&$html&&($terms.innerHTML=$html),this.addSelectEvents(),this.addButtonsEvents()}addModalEvents(){this?.modals?.forEach(modal=>{modal.addEventListener("click",()=>{const{product,price}=modal.dataset||{};this.resetState(),product&&this.getNMPTerms(product),price&&this.getNMPTerms(null,!0)})})}addSelectEvents(){const $selects=this.querySelectorAll(this.config.dom.selects)||[];$selects?.length>0&&[...$selects].forEach($select=>{$select.addEventListener("change",()=>{const{value}=$select||{},$ctoTerms=document.querySelectorAll(this.config.dom.cto_length)||null;if(value){const $activeOption=$select.querySelector(`option[value="${value}"]`)||null;if($activeOption){const term=$activeOption.dataset.term||"";this.updatePrice(value,term),$ctoTerms?.length>0&&[...$ctoTerms].forEach($ctoTerm=>{$ctoTerm.innerHTML=term+" "})}}})})}addButtonsEvents(){const $buttons=this.querySelectorAll(this.config.dom.buttons)||[];$buttons?.length>0&&[...$buttons].forEach($button=>{$button.addEventListener("click",()=>{this.setActiveTerms($button)})})}resetState(){const $buttons=this.querySelectorAll(this.config.dom.buttons)||[],$options=this.querySelectorAll(this.config.dom.options)||[],$selects=this.querySelectorAll(this.config.dom.selects)||[],$ctoBanks=document.querySelectorAll(this.config.dom.cto_bank)||null,$ctoTerms=document.querySelectorAll(this.config.dom.cto_length)||null;$buttons?.length>0&&[...$buttons].forEach($button=>{$button.classList.remove(this.config.cls.active)}),$options?.length>0&&[...$options].forEach($option=>{$option.classList.remove(this.config.cls.active)}),$selects?.length>0&&[...$selects].forEach($select=>{$select.selectedIndex=0}),$ctoBanks?.length>0&&[...$ctoBanks].forEach($ctoBank=>{const initialText=$ctoBank.dataset.initialTitle||"";$ctoBank.innerHTML=initialText}),$ctoTerms?.length>0&&[...$ctoTerms].forEach($ctoTerm=>{const initialText=$ctoTerm.dataset.initialTerm||"";$ctoTerm.innerHTML=initialText})}setActiveTerms(el){if(!el)return;const $button=el,{id}=$button||{},$ctoBanks=document.querySelectorAll(this.config.dom.cto_bank)||null,$ctoTerms=document.querySelectorAll(this.config.dom.cto_length)||null;if(id){this.resetState(),$button.classList.add(this.config.cls.active);const $terms=document.getElementById(`${id}-option`)||null;let activeTerm;if($terms){const $select=$terms.querySelector(this.config.dom.selects)||null;if($select){const $firstOption=$select.querySelector("option")||null;if($firstOption?.value){const term=$firstOption.dataset.term||"";activeTerm=term,this.updatePrice($firstOption.value,term)}}$terms.classList.add(this.config.cls.active),$ctoBanks?.length>0&&[...$ctoBanks].forEach($ctoBank=>{$ctoBank.innerHTML=$button.innerHTML}),$ctoTerms?.length>0&&[...$ctoTerms].forEach($ctoTerm=>{$ctoTerm.innerHTML=activeTerm+" "})}}}updatePrice(val,term=""){const $activePrice=this.querySelectorAll(this.config.dom.active_price)||[],$totals=this.querySelectorAll(this.config.dom.frequency)||[],frequencyText=this.dataset.frequency||null;if($activePrice?.length>0&&[...$activePrice].forEach($price=>{$price.innerHTML=val}),frequencyText&&$totals?.length>0){const frequency=frequencyText.replace("{{ per_month }}",term);$totals?.length>0&&[...$totals].forEach($total=>{$total.innerHTML=frequency})}}}customElements.define("nmp-test-mode",NMPTestMode);
//# sourceMappingURL=/cdn/shop/t/14/assets/nmp-test-mode.js.map?v=52863412589471034481742784889
