if(!customElements.get("collection-load-more")){class CollectionLoadMore extends HTMLElement{constructor(){super(),this.btnLoadMore=this.querySelector(".js-btn-load-more"),this.productGrid=document.querySelector("#product-grid"),this.setProperties(),this.toggleProductsVisibility(),window.addEventListener("shopify:section:load",()=>{this.setProperties(),this.toggleProductsVisibility()}),this.btnLoadMore.addEventListener("click",e=>{e.preventDefault(),this.btnLoadMore.setAttribute("disabled",!0),this.loadMoreProducts()}),this.cardText&&window.addEventListener("resize",()=>{if(window.innerWidth<mobileWidth){this.resetHiddenProducts();return}this.toggleProductsVisibility()})}setProperties(){this.productGrid=document.querySelector("#product-grid"),this.cardText=this.productGrid.querySelector(".js-card-text"),this.btnLoadMore=this.querySelector(".js-btn-load-more"),this.hiddenProducts=[],this.cardTextIndexOffset=2,this.isLastPage=this.classList.contains("hidden"),this.cardProduct=document.querySelector("card-product"),this.setGridColsNumber()}setGridColsNumber(){this.productGridCols=Math.round(this.productGrid.getBoundingClientRect().width/this.cardProduct?.getBoundingClientRect().width)}toggleProductsVisibility(){if(!this.cardText||this.isLastPage)return;const items=Array.from(this.productGrid.children),visibleItems=items.filter(item=>!item.classList.contains("hidden")).reverse(),productRect=visibleItems.find(item=>!item.classList.contains("js-card-text")).getBoundingClientRect(),lastItem=visibleItems[0],productGridRect=this.productGrid.getBoundingClientRect(),lastItemRect=lastItem.getBoundingClientRect(),productGridCols=Math.round(productGridRect.width/productRect.width),productGridOffset=productGridRect.x+productGridRect.width,lastItemOffset=lastItemRect.x+lastItemRect.width;if(items[items.length-1].classList.contains("js-card-text")){const prevProductRect=visibleItems[1].getBoundingClientRect(),prevProductOffset=prevProductRect.x+prevProductRect.width;lastItemRect.y!==prevProductRect.y&&productGridOffset!==prevProductOffset&&(this.hiddenProducts[0].classList.remove("hidden"),this.hiddenProducts.shift())}if(productGridOffset===lastItemOffset){this.hiddenProducts.length>=productGridCols&&this.resetHiddenProducts(productGridCols);return}const gridGaps=Math.round((productGridRect.width-(lastItemOffset-productGridRect.x))/productRect.width);if(this.hiddenProducts.length>=gridGaps){const hiddenProductsLimit=this.hiddenProducts.length-gridGaps;this.resetHiddenProducts(hiddenProductsLimit);return}let productsLooped=0;for(const product of visibleItems)if(!product.classList.contains("js-card-text")&&(productsLooped++,this.hiddenProducts.unshift(product),product.classList.add("hidden"),productsLooped===productGridCols-gridGaps))break}resetHiddenProducts(limit){if(!this.cardText)return;const hiddenProducts=[...this.hiddenProducts];let i=0;for(const product of hiddenProducts)if(i++,product.classList.remove("hidden"),this.hiddenProducts.shift(),limit&&limit===i)break}async loadMoreProducts(){try{const nextPageText=await(await fetch(`${this.btnLoadMore.dataset.href}`)).text(),productGridContainer=new DOMParser().parseFromString(nextPageText,"text/html").querySelector("#ProductGridContainer"),productGrid=productGridContainer.querySelector("#product-grid"),loadMoreContainer=productGridContainer.querySelector("collection-load-more"),btnLoadMore=loadMoreContainer.querySelector(".js-btn-load-more"),isLoadMoreHidden=loadMoreContainer.classList.contains("hidden"),products=productGrid.children;if(this.cardText)if(this.cardTextIndexOffset>0){const productsArray=Array.from(products),cardTextIndex=productsArray.findIndex(product=>product.classList.contains("js-card-text")),elementAfterCardText=products[cardTextIndex-this.cardTextIndexOffset];if(this.cardTextIndexOffset=0,elementAfterCardText){const cardText=productsArray[cardTextIndex];cardText.remove(),elementAfterCardText.insertAdjacentElement("beforebegin",cardText)}}else this.cardTextIndexOffset=2;if(this.resetHiddenProducts(),history.replaceState({},"",this.btnLoadMore.dataset.href),this.productGrid.insertAdjacentHTML("beforeend",productGrid.innerHTML),isLoadMoreHidden){this.isLastPage=!0,this.classList.add("hidden");return}this.toggleProductsVisibility(),this.btnLoadMore.dataset.href=btnLoadMore.dataset.href}catch(error){console.error(error),alert(error.message)}finally{this.btnLoadMore.removeAttribute("disabled")}}}customElements.define("collection-load-more",CollectionLoadMore)}
//# sourceMappingURL=/cdn/shop/t/12/assets/collection-load-more.js.map?v=139711867507152773731682758931
