"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("react/jsx-runtime"),n=require("react"),Fe=require("semantic-ui-react"),i=require("../../utils/data.js"),de=require("../../redux/hooks.js"),ge=require("react-redux"),pe=require("./CategoricalFilter.js"),fe=require("./YearFilter.js"),Se=J=>{const{"data-alphabetical-sort":U,"data-asc-order":k,"data-group":u,"data-placeholder":N,"data-all-label":V,"data-none-label":A,"data-use-single-column":$,"data-enable-text-search":G,"data-filter-type":h,"data-show-no-data-option":H,"data-close-on-select":Q,"data-all-none-same-behaviour":W,"data-auto-apply":X,"data-taxonomy":c,"data-categories":y="[]","data-is-country-filter":Z,"data-is-year-filter":ee,"data-type":te,"data-sort-first-by":_,"data-default-values":re="[]",editing:ae=!1}=J,m=de.useAppDispatch(),t=ge.useSelector(e=>e.getIn(["data","posts",u]))||{},o=h==="multi-select",E=n.useRef(0),Y=e=>ae?e:decodeURIComponent(e);let s=(e=>{try{return JSON.parse(Y(e))}catch(r){return console.warn("JSON parsing failed for value:",e,"- treating as plain string. Error:",r),Y(e)}})(re);s||(s=[]),typeof s=="string"&&(s=s.split(",").map(Number)),typeof s=="number"&&(s=[s]);const oe=i.toBoolean(U),le=i.toBoolean(k),I=i.toBoolean(H),R=i.toBoolean(Q),L=i.toBoolean(W),q=i.toBoolean(X),a=i.toBoolean(Z),l=i.toBoolean(ee),M=i.toBoolean($),v=i.toBoolean(G),C=_!=="none"?i.toNumber(_):null,[F,ne]=n.useState([]),[T,se]=n.useState(!1),x=(e,r)=>r?Array.isArray(e)?e:e!=null?[e]:[]:e||void 0,B=(e,r)=>Array.isArray(e)&&Array.isArray(r)?e.length!==r.length?!1:e.every((f,S)=>f===r[S]):e===r,d=n.useMemo(()=>x(t.countryFilter,o),[t.countryFilter,o]),g=n.useMemo(()=>x(t.categoryFilter,o),[t.categoryFilter,o]),p=n.useMemo(()=>l?x(t.yearFilter,o):void 0,[t.yearFilter,o,l]),[ie,w]=n.useState(p),[D,P]=n.useState(d),[j,z]=n.useState(g),K=n.useRef({countryFilter:d,categoryFilter:g,yearFilter:p,isMultiSelectFilter:o});n.useEffect(()=>{const e=K.current,r=!B(e.countryFilter,d),f=!B(e.categoryFilter,g),S=l&&!B(e.yearFilter,p),O=e.isMultiSelectFilter!==o;(r||f||S||O)&&(r&&P(d),f&&z(g),S&&w(p),K.current={countryFilter:d,categoryFilter:g,yearFilter:p,isMultiSelectFilter:o})},[d,g,p,o,l]);const ce=e=>{w(e),m({type:"SET_POSTS_FILTER",group:u,...t,isYearFilter:l,yearFilter:l?e:null,isCountryFilter:a,categoryFilter:t.categoryFilter,countryFilter:t.countryFilter,sortFirstBy:C,countryCategory:c,categoryTaxonomy:c,countryTaxonomy:c})},ue=e=>{a?P(e):z(e),m({type:"SET_POSTS_FILTER",group:u,...t,categoryFilter:a?t.categoryFilter:e,countryFilter:a?e:t.countryFilter,isYearFilter:l,yearFilter:t.yearFilter,isCountryFilter:a,sortFirstBy:a?C:t.sortFirstBy,countryCategory:a?c:t.countryCategory,categoryCategory:a?t.categoryCategory:e,categoryTaxonomy:a?t.categoryTaxonomy:c,countryTaxonomy:a?c:t.countryTaxonomy})};return n.useEffect(()=>{const e=s.length>0,r=o?s:s[0],f=a?t.categoryFilter:e?r:o?y?y.split(",").map(Number):[]:t.categoryFilter,S=a?e?r:o?y?y.split(",").map(Number):[]:t.countryFilter:t.countryFilter,O=l&&o?F.length>0?F.map(ye=>ye.value):[]:t.yearFilter;m({type:"SET_INITIAL_POSTS_FILTER",group:u,categoryFilter:f,countryFilter:S,isYearFilter:l,isCountryFilter:a,sortFirstBy:C,yearFilter:l?O:null,categoryCategory:a?null:t.categoryCategory,categoryTaxonomy:a?null:c,countryCategory:a?t.countryCategory:null,countryTaxonomy:a?c:null,page:1})},[]),n.useEffect(()=>{if(l&&!T){const e=l&&o?F.length>0?F.map(r=>r.value):[]:t.yearFilter;m({type:"SET_INITIAL_POSTS_FILTER",group:u,...t,isYearFilter:l,yearFilter:l?e:null})}},[T]),b.jsxs(Fe.Container,{fluid:!0,className:"filter post-filter",children:[l&&b.jsx(fe.default,{group:u,placeholder:N,allLabel:V,noneLabel:A,useSingleColumn:M,enableTextSearch:v,filterType:h,showNoDataOption:I,closeOnSelect:R,allNoneSameBehaviour:L,autoApply:q,alphabeticalSort:oe,ascOrder:le,options:F,value:ie,yearOptions:F,setYearOptions:ne,yearFilterLoading:T,setYearFilterLoading:se,onChange:(e,r)=>{ce(r)},resetKey:E.current}),!l&&b.jsx(pe.default,{group:u,placeholder:N,allLabel:V,noneLabel:A,useSingleColumn:M,enableTextSearch:v,filterType:h,showNoDataOption:I,closeOnSelect:R,allNoneSameBehaviour:L,autoApply:q,taxonomy:c,type:te,value:a?D:j,onChange:(e,r)=>{ue(r)},categories:y?y.split(","):[],resetKey:E.current},`cat-${u}`)]})};exports.default=Se;
