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=>{a.push({filter:this.getAttr("filter-option"),id:t,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=>{t&&s.push({filter:r.attr("data-filter-option"),id:t,origin:i})}))}})),s}filter(e,i){const s=new TCB_Front.PostList(t(i));if(this.shouldFilter(s)){s.listArgs&&(s.listArgs.query&&(s.listArgs.query.paged=1),s.listArgs.filters=[]);const t=[...new Set(this.selectedFilters[this.getAttr("filter-option")])],i=this.getOtherSelectedFilters(s);("search"===this.getAttr("filter-option")||i.length||t.length&&(!t.includes("all")||t.length>1))&&(s.listArgs.filters=[...e,...i]),s.enableLoading(),TCB_Front.Utils.restAjax({route:tve_frontend_options.routes.posts+"/html",data:{content:s.content,args:s.listArgs}}).done((t=>{this.restorePostListInstance(),0===t.count?s.$element.addClass("empty-list"):(s.$element.removeClass("empty-list"),s.$element.find("article").remove(),s.insertItems(t),s.initItems()),TCB_Front.event_triggers(s.$element),s.disableLoading(),s.listArgs.attr.total_post_count=t.total_post_count,s.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(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];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(){const t=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.$filterElement).find("form");t.submit((e=>{e.preventDefault();const i=t.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)=>{i&&e.push(`[data-css="${i.getAttribute("data-css")}"]`)})),e.length&&(this.postListInstance.$element=t(e.join(",")))}}window.TCB_Front.PostListFilter=e,TCB_Front.setModuleLoadedStatus("post-list-filter",!0)}})(ThriveGlobal.$j);