"use strict";class CartofferList extends HTMLElement{constructor(){super(),this.selectors=this.selectors||{},this.selectors.offerSections=this.querySelectorAll("[data-offerSection]");const collections={};this.selectors.offerSections.forEach(section=>{const collectionData=JSON.parse(section.querySelector('script[type="application/json"]').textContent);collections[collectionData.type]={...collectionData,isAllLoaded:!1,currentPage:0,totalPages:0,section}}),this.state={activeBtn:null,isLoading:!1,collections,activeCollection:null,maxProducts:10},this.isCartPage=document.querySelector("body.template-cart"),this.buildSelectors(),this.init()}static get getElementName(){return"cart-offer-list"}buildSelectors(){this.selectors.modelClose=this.querySelector(".side-offers-close"),this.selectors.modelBack=this.querySelector(".back-to-prev-side-offer"),this.selectors.offerForms=this.querySelectorAll("form.cart-offer-product-form"),this.selectors.backSection=this.querySelector(".cart-offer-back"),this.selectors.loadMoreBtn=this.querySelector("[data-load-more-btn]")}init(){this.selectors.modelClose.addEventListener("click",this.hide.bind(this)),this.selectors.modelBack.addEventListener("click",this.hide.bind(this)),this.selectors.backSection.addEventListener("click",this.hide.bind(this));const throttleFn=_theme.Helpers.throttle((entries,observer)=>{const isIntersecting=entries[0].isIntersecting;if(this.state.isLoading||!isIntersecting||this.state.activeCollection.isAllLoaded)return;if(Object.keys(this.state.collections).every(key=>this.state.collections[key].isAllLoaded)){observer.unobserve(entries[0].target);return}const startIndex=this.state.activeCollection.currentPage*this.state.maxProducts;this.loadProductsFromSearchanize(this.state.activeCollection.handle,startIndex,this.state.activeCollection.type)});new IntersectionObserver(throttleFn,{root:this.querySelector("aside"),rootMargin:"0px 0px 0px 0px",threshold:.05}).observe(this.selectors.loadMoreBtn),Object.keys(this.state.collections).forEach(key=>{this.loadProductsFromSearchanize(this.state.collections[key].handle,0,this.state.collections[key].type)})}initProductEvents(){this.querySelectorAll('.offer-variant-size-container select[name="id"]').forEach(selectNode=>{selectNode.addEventListener("change",evt=>{try{const form=selectNode.closest("form"),quickview=form.querySelector("cart-offer-quickview"),parent=selectNode.closest(".cart-offer.grid-item"),variantJSONScript=form.querySelector('script[type="application/json"]'),variant=JSON.parse(variantJSONScript.textContent)[selectNode.value],salePriceElem=parent.querySelector("[data-sale-price]"),salePriceElemSrOnly=parent.querySelector("[data-sale-price-sr-only]"),comparePriceElem=parent.querySelector("[data-compare-price]"),comparePriceElemSrOnly=parent.querySelector("[data-compare-price-sr-only]"),saleElems=[salePriceElem,salePriceElemSrOnly],compareElems=[comparePriceElem,comparePriceElemSrOnly];quickview&&(quickview.setAttribute("data-id",variant.id),quickview.variantId=variant.id),Number(variant.price)?(saleElems.forEach(elem=>elem.classList.remove("hidden")),salePriceElem.textContent=_theme.Helpers.formatPrice(variant.price),salePriceElemSrOnly.textContent=`Sale Price ${_theme.Helpers.formatPrice(variant.price)}`):saleElems.forEach(elem=>elem.classList.add("hidden")),Number(variant.compareAtPrice)?(compareElems.forEach(elem=>elem.classList.remove("hidden")),comparePriceElem.textContent=_theme.Helpers.formatPrice(variant.compareAtPrice),comparePriceElemSrOnly.textContent=`Original Price ${_theme.Helpers.formatPrice(variant.compareAtPrice)}`):compareElems.forEach(elem=>elem.classList.add("hidden"))}catch(error){console.log(error)}})})}show(type){document.body.classList.add("overflow-hidden"),this.selectors.offerSections.forEach(section=>{section.dataset.tag.trim()===type?section.setAttribute("aria-hidden",!1):section.setAttribute("aria-hidden",!0)}),this.setAttribute("data-type",type),this.setAttribute("aria-hidden",!1),this.classList.add("active"),this.classList.remove("hidden"),this.state.activeCollection=this.state.collections[type],this.state.activeCollection.all_products_count<10&&(this.state.activeCollection.isAllLoaded=!0)}hide(){document.body.classList.remove("overflow-hidden"),$("*").not($(".cart-offer-container").find("*")).not(".cart-offer-container").not($(".cart-offer-container").parents()).removeAttr("aria-hidden"),this.selectors.goBackOnBtn=this.querySelector('.offerSection[aria-hidden="false"]')?.getAttribute("data-tag"),this.selectors.prevBtn=document.querySelector(`button[data-type="${this.selectors.goBackOnBtn}"]`),this.selectors?.prevBtn?.focus(),this.setAttribute("aria-hidden",!0),this.classList.add("hidden"),this.classList.remove("active"),this.removeAttribute("data-type"),this.selectors.offerSections.forEach(section=>{section.setAttribute("aria-hidden",!0)}),this.state.activeCollection=null}async loadProductsFromSearchanize(handle,startIndex,type){try{this.toggleInfiniteLoader(!0);const result=await(await fetch(`https://www.searchanise.com/getresults?api_key=2K7F7z9Y9V&output=jsonp&items=true&facets=false&facetsShowUnavailableOptions=false&categories=true&sortBy=sales_amount&sortOrder=desc&collection=${handle}&maxResults=${this.state.maxProducts}&startIndex=${startIndex}`)).json();this.renderProducts(result,type),this.state.collections[type].currentPage=this.state.collections[type].currentPage+1,this.initProductEvents(),this.toggleInfiniteLoader(!1)}catch(error){console.error("Error",error)}}renderProducts(result,type){const collection=this.state.collections[type];if(!result||result.totalItems===0){this.state.collections[type].isAllLoaded=!0;return}let mainHtml="";result.items.forEach((product,index)=>{if(product.metafield_b9ba57cd478d42c60429674e481c50c1&&!product.shopify_variants?.some(variant=>variant.search_variant_metafields_data?.variant_metafield_25c7b979d6abf42fb11a9be16739134f==="true"&&variant.available!=="0"))return;let product_html=_theme.product_html.cartOfferGrid;const offerVariant=this.getApplicableVariant(product);if(!offerVariant)return!1;let prodType="";product.metafield_9e14d49086f4ac1134f463aa2738d80e!=null?prodType=product.metafield_9e14d49086f4ac1134f463aa2738d80e:product.product_type!==void 0&&(prodType=product.product_type);const productHandle=product.link?product.link.split("/").pop():"";product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Id]]",product.product_id),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Handle]]",productHandle),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Title]]",product.title),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Type]]",prodType),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_URL]]",offerVariant.link||product.link),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Images]]",this.buildImagesHtml(product)),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Vendor]]",this.buildVendorHtml(product)),product_html=_theme.Helpers.updateHtml(product_html,"[[Product_Price]]",this.buildPriceHtml(offerVariant)),product_html=_theme.Helpers.updateHtml(product_html,"[[Variant_Id]]",offerVariant.variant_id),product_html=_theme.Helpers.updateHtml(product_html,"[[Variant_Size]]",this.buildVariantSelectorHtml(product,collection.render_variants)),product_html=_theme.Helpers.updateHtml(product_html,"[[GWP_Properties]]",this.buildGwpProperties(product)),mainHtml+=product_html}),this.selectors.offerSections.forEach(section=>{if(section.dataset.tag===type){const productsContainer=section.querySelector(".offerCollection"),oldHtml=productsContainer.innerHTML;productsContainer.innerHTML=oldHtml+mainHtml}})}buildGwpProperties(product){let html="";if(!product)return;if(product.shopify_variants?.some(variant=>variant.search_variant_metafields_data?.hasOwnProperty("variant_metafield_25c7b979d6abf42fb11a9be16739134f"))){const offerVariant=product.shopify_variants?.find(v=>v.search_variant_metafields_data?.variant_metafield_25c7b979d6abf42fb11a9be16739134f==="true");offerVariant&&offerVariant.variant_id&&(html+=``)}return html}buildImagesHtml(product){if(!product)return"";product.shopify_images||(product.shopify_images=["data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="]);let html="";return!product.shopify_images&&product.shopify_images.length===0||(product.shopify_images.length<2&&(product.shopify_images.push(product.shopify_images[0]),product.shopify_images_alt&&product.shopify_images_alt.length===1&&product.shopify_images_alt.push(product.shopify_images_alt[0])),product.shopify_images.slice(0,2).forEach((imageUrl,index)=>{const imageAlt=product.shopify_images_alt&&product.shopify_images_alt[index]||product.title;html+=`