let sppbArtcileAddonCurrentPage=1;async function loadMoreArticles(e){const{addonId:t,limit:a,ordering:n,catid:o,includeSubcat:r,postType:i,tagids:d,currentPage:s,addonSettings:c,parentItem:l}=e,p=Joomla.getOptions("sppb-root"),u=await fetch(`${p}index.php?option=com_sppagebuilder&task=articles.loadMoreArticles`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRF-Token":Joomla.getOptions("csrf.token")},body:JSON.stringify({addon_id:t,limit:Number(a),ordering:n,catid:o,include_subcat:r,post_type:i,tagids:d,page:s,currentLink:window.location.search.replace("?",""),isSite:!window.location.pathname.includes("/edit")&&!window.location.pathname.includes("/edit/"),addonSettings:c,parent_item:JSON.stringify(l)})});return await u.json()}function loadArticlesByClickingLoadMoreButton(){document.querySelectorAll("[data-sppb-articles-load-more-button]").forEach((e=>{const t=e.closest(".sppb-addon-articles"),a=t.querySelector(".sppb-addon-content .sppb-row"),n=e.closest(".sppb-addon-articles__pagination"),o=n.querySelector('[name="sppb-articles-addon-id"]')?.value,r=n.querySelector('[name="sppb-articles-limit"]')?.value,i=n.querySelector('[name="sppb-articles-ordering"]')?.value,d=JSON.parse(n.querySelector('[name="sppb-articles-catid"]')?.value||"[]"),s=n.querySelector('[name="sppb-articles-include-subcat"]')?.value,c=n.querySelector('[name="sppb-articles-post-type"]')?.value,l=JSON.parse(n.querySelector('[name="sppb-articles-tagids"]')?.value||"[]"),p=n.querySelector('[name="sppb-articles-addon-settings"]')?.value||"{}",u=n.dataset.parentItem?JSON.parse(n.dataset.parentItem):null;o&&t&&a&&n&&e.addEventListener("click",(async function(t){t.preventDefault(),sppbArtcileAddonCurrentPage++;const n=e.dataset.totalPages;e.textContent="Loading...",e.disabled=!0;try{const t=await loadMoreArticles({addonId:o,limit:r,ordering:i,catid:d,includeSubcat:s,postType:c,tagids:l,currentPage:sppbArtcileAddonCurrentPage,addonSettings:p,parentItem:u});if(e.textContent=e.dataset.text,e.disabled=!1,t?.data?.data&&a.insertAdjacentHTML("beforeend",t.data.data),sppbArtcileAddonCurrentPage>=n)return void e.remove()}catch(t){console.error("Error loading more articles:",t),e.textContent=e.dataset.text,e.disabled=!1}}))}))}window.addEventListener("DOMContentLoaded",(function(){loadArticlesByClickingLoadMoreButton()}));