(()=>{"use strict";const t=".flexa-post-grid__grid",e=".flexa-pagination",r=".flexa-pagination-loadmore",n="is-loading";function o(o,a,i,s,d=!1){if(!o.gid||!o.postId||!o.rest)return void(window.location.href=a.toString());o.pending&&o.pending.abort();const l=new window.AbortController;o.pending=l;const c=o.wrapper.querySelector(t);c&&(c.classList.add(n),c.setAttribute("aria-busy","true"));const u=o.rest+(-1===o.rest.indexOf("?")?"?":"&")+function(t,e,r){const n=new window.URLSearchParams;n.set("post_id",r),n.set("block_id",e);const o=parseInt(t.searchParams.get(function(t){return t?`pg_${t}`:"pg"}(e))||"1",10);n.set("page",String(o>0?o:1));const a=t.searchParams.get(function(t){return`s_${t}`}(e));a&&n.set("s",a);const i=`tx_${e}_`,s={};return t.searchParams.forEach((t,e)=>{if(!e.startsWith(i)||!t)return;const r=e.slice(i.length);s[r]=(s[r]||[]).concat(t.split(",").filter(Boolean))}),Object.keys(s).forEach(t=>{n.set(`tax[${t}]`,s[t].join(","))}),n.toString()}(a,o.gid,o.postId);window.fetch(u,{credentials:"same-origin",signal:l.signal}).then(t=>{if(!t.ok)throw new Error("HTTP "+t.status);return t.json()}).then(l=>{if(function(n,o,a){const{wrapper:i}=n,s=i.querySelector(".flexa-post-grid__inner")||i,d=(new window.DOMParser).parseFromString(o.grid,"text/html"),l=d.querySelector(t),c=i.querySelector(t);if(a&&c&&l)Array.from(l.children).forEach(t=>c.appendChild(t));else if(c)c.replaceWith(l||d.body.firstElementChild);else{const t=i.querySelector(".flexa-post-grid__empty");t?t.replaceWith(l||d.body.firstElementChild):s.insertAdjacentHTML("beforeend",o.grid)}const u=i.querySelector(`${e}, ${r}`);u&&u.remove(),o.pagination&&s.insertAdjacentHTML("beforeend",o.pagination);const f=i.querySelector(".flexa-post-grid__status");if(f&&o.status){const t=f.getAttribute("data-template")||"%s";f.textContent=t.replace("%s",String(o.total))}var g;g={target:n.gid,total:o.total,page:o.page,totalPages:o.totalPages},document.dispatchEvent(new window.CustomEvent("flexa-post-grid:updated",{detail:g}))}(o,l,i),o.pending=null,s&&!i)try{window.history.pushState({flexaPostGrid:o.gid},"",l.url||a.toString())}catch(t){}const c=o.wrapper.querySelector(t);c&&(c.classList.remove(n),c.removeAttribute("aria-busy")),d&&function(t){const{wrapper:r}=t;r.getBoundingClientRect().top<0&&r.scrollIntoView({block:"start",behavior:"smooth"});const n=r.querySelector(e);n&&(n.setAttribute("tabindex","-1"),n.focus({preventScroll:!0}))}(o)}).catch(t=>{t&&"AbortError"===t.name||(o.pending=null,c&&(c.classList.remove(n),c.removeAttribute("aria-busy")),window.location.href=a.toString())})}function a(){const t=[];document.querySelectorAll("[data-flexa-post-grid]").forEach(n=>{const a=function(t){const n=t.getAttribute("data-flexa-grid-id")||"";if(!n)return null;const a={wrapper:t,gid:n,postId:t.getAttribute("data-flexa-post-id")||"",rest:t.getAttribute("data-flexa-rest")||"",loadMore:t.hasAttribute("data-flexa-loadmore"),pending:null};return t.addEventListener("click",t=>{if(t.defaultPrevented||0!==t.button||t.metaKey||t.ctrlKey||t.shiftKey||t.altKey)return;const n=t.target;if(!n)return;const i=n.closest(`${e} a.page-numbers`);if(i&&i.href)return t.preventDefault(),void o(a,new window.URL(i.href),!1,!0,!0);const s=n.closest(".flexa-pagination-loadmore__btn");if(s&&!s.disabled){const e=s.closest(r),n=e?e.getAttribute("data-next"):"";if(!n)return;t.preventDefault(),s.disabled=!0,o(a,new window.URL(n,window.location.href),!0,!1)}}),a}(n);a&&t.push(a)}),t.length&&(document.addEventListener("flexa-post-grid:filter",e=>{const r=e.detail;if(!r||!r.url)return;const n=r.target?t.filter(t=>t.gid===r.target)[0]:t[0];n&&o(n,new window.URL(r.url,window.location.href),!1,!0)}),window.addEventListener("popstate",t=>{t.state&&t.state.flexaPostGrid&&window.location.reload()}))}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",a):a()})();