class BrfQueryBuilder{loops={};infiniteScrollOffset=0;loadMoreButtons=[];observer=null;constructor(){}init(){this.collectLoops(),this.collectLoadMoreButtons(),this.loops&&0!==Object.keys(this.loops).length&&(this.handleLoops(),this.handleLoadMoreButtons())}collectLoops(){const t=document.querySelectorAll("[data-brf-loading-type]");t&&0!==t.length&&(t.forEach((t=>{const e=t.dataset.brfItemId,o=t.dataset.brfLoadingType;this.loops[e]||(this.loops[e]=[]),this.loops[e].push({itemId:e,loadingType:o})})),window.bricksforgeData||(window.bricksforgeData={}),window.bricksforgeData.apiQueryBuilder||(window.bricksforgeData.apiQueryBuilder={}),window.bricksforgeData.apiQueryBuilder.loops=this.loops)}collectLoadMoreButtons(){const t=document.querySelectorAll(".brxe-brf-api-query-builder-load-more[data-connection]");t&&0!==t.length&&t.forEach((t=>{this.loadMoreButtons.push({connection:t.dataset.connection,button:t})}))}handleLoops(){this.handleInfiniteScroll()}handleInfiniteScroll(){const t=this.filterLoopsByLoadingType("infinite");this.observer=new IntersectionObserver((e=>{e.forEach((e=>{e.isIntersecting&&(this.loadItems(e.target,t),this.observer.unobserve(e.target))}))}),{root:null,threshold:.5,rootMargin:`0px 0px ${this.infiniteScrollOffset}px 0px`}),this.observeLastElementsForInfiniteScroll(t)}getBricksIdFromItemId(t){const e=document.querySelector(`[data-brf-item-id="${t}"]`);return!(!e||!e.dataset.brfQueryId)&&e.dataset.brfQueryId}getItemIdFromBricksId(t){const e=document.querySelector(`[data-brf-query-id="${t}"]`);return!(!e||!e.dataset.brfItemId)&&e.dataset.brfItemId}observeLastElementsForInfiniteScroll(t){for(const e in t){const t=this.getBricksIdFromItemId(e);if(this.loadMoreButtons.some((o=>o.connection===e||o.connection===t)))continue;const o=document.querySelectorAll(`[data-brf-item-id="${e}"]`);if(!o||0===o.length)continue;const n=o[o.length-1];this.observer.observe(n)}}filterLoopsByLoadingType(t){const e={};for(const o in this.loops){const n=this.loops[o].filter((e=>e.loadingType===t));n.length>0&&(e[o]=n)}return e}async loadItems(t,e){const o=t.dataset.brfItemId,n=(t.dataset.brfLoadingType,!!window.BRFQUERYBUILDER?.apiurl&&window.BRFQUERYBUILDER.apiurl),r=!!window.BRFQUERYBUILDER?.nonce&&window.BRFQUERYBUILDER.nonce;if(!n||!r)return;const i=document.querySelectorAll(`[data-brf-item-id="${o}"]`).length,s=window.BRFQUERYBUILDER?.postId;let l=t.dataset.brfQueryId;l||(l=t.className.split(" ").find((t=>t.startsWith("brxe-"))).replace("brxe-",""));const d=await fetch(`${n}/query_builder_load_items`,{method:"POST",body:JSON.stringify({item_id:o,count:i,post_id:s,query_element_id:l}),headers:{"X-WP-Nonce":r}});if(!d.ok)return;const a=await d.json();if(!a)return;const{html:c}=a;t.lastElementChild.parentElement.insertAdjacentHTML("afterend",c),this.observeLastElementsForInfiniteScroll(e)}handleLoadMoreButtons(){this.loadMoreButtons.forEach((t=>{t.button.addEventListener("click",(async()=>{const e=t.connection;let o=document.querySelectorAll(`[data-brf-item-id="${e}"]`);if(!o||0===o.length){const t=this.getItemIdFromBricksId(e);if(!t)return;o=document.querySelectorAll(`[data-brf-item-id="${t}"]`)}if(!o||0===o.length)return;const n=o[o.length-1],r=t.button.innerHTML,i=t.button.dataset.loadingText||"Loading...",s=t.button.querySelector("i");if(s){const e=s.outerHTML;t.button.innerHTML=i+" "+e}else t.button.innerHTML=i;t.button.setAttribute("disabled","disabled");try{const r=this.filterLoopsByLoadingType("button");await this.loadItems(n,r);document.querySelectorAll(`[data-brf-item-id="${e}"]`).length===o.length&&(t.button.style.display="none")}catch(t){console.error("Error loading more items:",t)}finally{t.button.innerHTML=r,t.button.removeAttribute("disabled")}}))}))}}document.addEventListener("DOMContentLoaded",(()=>{(new BrfQueryBuilder).init()}));