void 0===TCB_Front.js_modules["post-list-filter"]&&TCB_Front.setModuleLoadedStatus("post-list-filter",!1),(t=>{if(!TCB_Front.isModuleLoaded("post-list-filter")){t(window).on("tcb_after_dom_ready",()=>{"undefined"!=typeof tcb_post_lists&&tcb_post_lists.length&&t(".tcb-post-list-filter").each((i,s)=>{new e(t(s))})});class e{constructor(e){this.$filterElement=e;const i=t(".tcb-post-list:not(.tcb-featured-list)[data-dynamic_filter]").filter((t,e)=>e.getAttribute("data-dynamic_filter").replaceAll("'",'"').includes(this.getAttr("url-query-key")));i.length&&(this.postListInstance=new TCB_Front.PostList(i)),this.selectedFilters=[],tve_frontend_options.is_editor_page||this.frontendInit()}static get optionIdentifier(){return".tcb-filter-option"}static getActiveClass(t){return"button"===t||"list"===t?"tve-state-active":"tve-state-expanded"}shouldFilter(t){let e=!1;return t&&t.$element.length&&JSON.parse(t.$element.attr("data-dynamic_filter").replaceAll("'",'"'))[this.getAttr("url-query-key")]&&(e=!0),e}frontendInit(){this.markDefaultAsSelected(),this.markQueryStringsAsSelected(),this.bindEvents()}bindEvents(){const e=`.tcb-post-list-filter[data-css=${this.getAttr("css")}] .tcb-filter-option`;t("body").off("mousedown",e).on("mousedown",e,this.clickOption.bind(this)),t(window).on("tcb_search_overlay_show",(t,e)=>{this.$filterElement.find(e).length&&this.addSearchEvents(e.data("$$clone"))}),this.addSearchEvents()}clickOption(i){const s="true"===this.getAttr("multiple-selection"),r=this.getAttr("filter-type"),l=t(i.currentTarget);s||(this.$filterElement.find(e.optionIdentifier).removeClass(e.getActiveClass(r)),this.selectedFilters[this.getAttr("filter-option")]=[]),"all"!==l.attr("data-id")?this.$filterElement.find('[data-id="all"]').removeClass(e.getActiveClass(r)):(this.$filterElement.find(`.${e.getActiveClass(r)}:not([data-id="all"])`).removeClass(e.getActiveClass(r)),this.selectedFilters=[]),l.hasClass(e.getActiveClass(r))?(l.removeClass(e.getActiveClass(r)),void 0===this.selectedFilters[this.getAttr("filter-option")]&&(this.selectedFilters[this.getAttr("filter-option")]=[]),this.selectedFilters[this.getAttr("filter-option")]=this.selectedFilters[this.getAttr("filter-option")].filter(function(t){return t!==l.attr("data-id")})):(l.addClass(e.getActiveClass(r)),this.selectedFilters[this.getAttr("filter-option")]?this.selectedFilters[this.getAttr("filter-option")].push(l.attr("data-id")):this.selectedFilters[this.getAttr("filter-option")]=[l.attr("data-id")]);const n=[...new Set([...this.selectedFilters[this.getAttr("filter-option")],...this.getQueryStringFilters(!0)])],a=[];n.forEach(t=>{if(t&&"all"!==t){const i=this.$filterElement.find(`${e.optionIdentifier}[data-id=${t}]`).attr("data-name");if(!i)return;a.push({filter:this.getAttr("filter-option"),name:i,origin:this.getAttr("url-query-key")})}}),this.setQueryStringUrl(this.getOptionNames(n).join(",")),void 0!==this.postListInstance&&this.postListInstance.$element.each((t,e)=>{this.filter(a,e)}),t("body").trigger("tcb_post_list_filter_change")}getOtherSelectedFilters(i){const s=[],r=JSON.parse(i.$element.attr("data-dynamic_filter").replaceAll("'",'"'));return Object.keys(r).forEach(i=>{if(i&&i!==this.getAttr("url-query-key")){const r=t(`.tcb-post-list-filter[data-url-query-key=${i}]`),l=[];if(r.find(`.${e.getActiveClass(r.attr("data-filter-type"))}:not([data-id=all])`).each((t,e)=>{e&&l.push(e.getAttribute("data-id"))}),"search"===r.attr("data-filter-type")){const t=r.find('input[type="search"]').val();t&&s.push({filter:"search",name:t,origin:i})}l.forEach(t=>{if(t&&"all"!==t){const l=r.find(`${e.optionIdentifier}[data-id=${t}]`).attr("data-name");if(!l)return;s.push({filter:r.attr("data-filter-option"),name:l,origin:i})}})}}),s}filter(e,i){const s=i.getAttribute("data-css");if(!s)return;const r=t(`[data-css="${s}"]`);if(0===r.length)return;const l=new TCB_Front.PostList(r);if(this.shouldFilter(l)){l.listArgs&&(l.listArgs.query&&(l.listArgs.query.paged=1,l.listArgs.query.offset&&delete l.listArgs.query.offset),l.listArgs.attr&&(l.listArgs.attr.paged=1),l.listArgs.filters=[]);const t=[...new Set(this.selectedFilters[this.getAttr("filter-option")])],i=this.getOtherSelectedFilters(l);("search"===this.getAttr("filter-option")||i.length||t.length&&(!t.includes("all")||t.length>1))&&(l.listArgs.filters=[...e,...i]),l.enableLoading(),TCB_Front.Utils.restAjax({route:tve_frontend_options.routes.posts+"/html",data:{content:l.content,args:l.listArgs}}).done(t=>{this.restorePostListInstance(),0===t.count?l.$element.addClass("empty-list"):(l.$element.removeClass("empty-list"),l.$element.find("article").remove(),l.insertItems(t),l.initItems()),TCB_Front.event_triggers(l.$element),l.disableLoading(),l.listArgs.attr.total_post_count=t.total_post_count,l.renderPagination()}).fail(()=>{console.warn("There was an error and the post list could not be filtered.")})}}getAttr(t){return this.$filterElement.attr(`data-${t}`)}getSelectedElements(){const t=[];return this.$filterElement.find(e.getActiveClass(this.getAttr("filter-option"))).each((e,i)=>{t.push(i.getAttribute("data-id"))}),t}setQueryStringUrl(t){const e=this.getAttr("url-query-key"),i=TCB_Front.queryString.setUrlParam(window.location.href,e,t).replace(/%2C/g,",");window.history.pushState("","",i)}getQueryStringFilters(t=!1){const i=(TCB_Front.queryString.get(this.getAttr("url-query-key"))||"").split(","),s=[];return i.forEach(i=>{let r=`[data-name="${i}"]`;t&&(r=`.${e.getActiveClass(this.getAttr("filter-type"))}`+r);const l=this.$filterElement.find(r).attr("data-id");i&&l&&s.push(l)}),s}markDefaultAsSelected(){if(0===this.getQueryStringFilters().length){const t=this.getAttr("default-value"),i=this.getAttr("filter-option"),s=this.$filterElement.find(`${e.optionIdentifier}[data-id=${t}]`),r=[{id:t,filter:i,origin:this.getAttr("url-query-key")}];s.addClass(e.getActiveClass(this.getAttr("filter-type"))),["all","none"].includes(t)||(this.selectedFilters[i]=[t],this.postListInstance.$element.each((t,e)=>{this.filter(r,e)}))}}markQueryStringsAsSelected(){const t=this.getAttr("url-query-key"),i=TCB_Front.queryString.get(t);i&&i.split(",").forEach(t=>{this.$filterElement.find(`[data-name="${t}"]`).addClass(e.getActiveClass(this.getAttr("filter-type")))})}getOptionNames(t){const i=[];return t.forEach(t=>{t&&"all"!==t&&i.push(this.$filterElement.find(`${e.optionIdentifier}[data-id=${t}]`).attr("data-name"))}),i}addSearchEvents(t=this.$filterElement){const e=t.find("form");e.submit(t=>{t.preventDefault();const i=e.find('input[type="search"]').val(),s=[{filter:"search",name:i}];this.setQueryStringUrl(i),this.postListInstance.$element.each((t,e)=>{this.filter(s,e)})})}restorePostListInstance(){const e=[];this.postListInstance.$element.each((t,i)=>{if(i){const t=i.getAttribute("data-css");t&&e.push(`[data-css="${t}"]`)}}),e.length&&(this.postListInstance.$element=t(e.join(",")))}}window.TCB_Front.PostListFilter=e,TCB_Front.setModuleLoadedStatus("post-list-filter",!0)}})(ThriveGlobal.$j);