"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("react/jsx-runtime"),T=require("react"),f=require("semantic-ui-react"),Q=require("../connected-templates/PostIntro.js"),X=require("react-intl"),G=require("./utils.js"),Z=require("./NoData.js"),Y=require("react-redux"),$=require("../reducers/data-api.js"),tt=require("react-router"),M=require("../../utils/data.js"),et=D=>{const{posts:u,numberOfColumns:E,sortFirstBy:c,countryCategory:l,postWidth:x,postHeight:A}=D,g=[];if(l&&c&&c!=="none"){const d=u.filter(p=>p[l].includes(Number(c))),b=u.filter(p=>!p[l].includes(Number(c)));g.push(...d),g.push(...b)}else g.push(...u);return i.jsx(f.Grid,{className:"filtered-posts",columns:E,children:i.jsx(f.GridRow,{children:g.map(d=>i.jsx(f.Grid.Column,{children:i.jsx("div",{className:"filtered-posts-column",style:{width:x,height:A,overflow:"hidden"},children:i.jsx(Q.default,{style:{width:"100%",height:"100%",overflow:"hidden",margin:0,padding:0},as:f.Container,fluid:!0,post:d},d.id)})},d.id))})})},rt=D=>{const{"data-group":u,"data-number-of-columns":E,"data-type":c,"data-taxonomy":l,"data-categories":x,"data-height":A,"data-post-width":g,"data-post-height":d,"data-number-of-items-per-page":b,"data-enable-sorting":p,"data-sort-first-by":j,"data-sorting-taxonomy":w,editing:V}=D,B=Y.useDispatch(),{locale:z}=tt.useParams(),[q,N]=T.useState(!1),I=Y.useSelector(t=>t).getIn(["data","posts",u]),[P,F]=T.useState([]),H=M.toBoolean(p)&&j!=="none"?M.toNumber(j):null,m=I??{yearFilter:null,categoryFilter:null,countryFilter:null,page:1,countryTaxonomy:null,categoryTaxonomy:null},S=t=>{if(t==null)return{values:[],isExplicitNone:!1};const e=Array.isArray(t)?t:[t],a=e.some(r=>Number(r)===Number.MIN_SAFE_INTEGER);return{values:e.map(r=>Number(r)).filter(r=>Number.isFinite(r)&&r!==Number.MIN_SAFE_INTEGER),isExplicitNone:a}},W=t=>{if(!t||t.length===0)return null;const e=Array.from(new Set(t)).sort((v,C)=>v-C);if(e.length===0)return null;const a=e[0],o=e[e.length-1],r=G.getStartDateAndEndDateFromYear(a),n=G.getStartDateAndEndDateFromYear(o);return!(r!=null&&r.startDate)||!(n!=null&&n.endDate)?null:{startDate:r.startDate,endDate:n.endDate}},J=()=>{const t=S(m.yearFilter),e=t.isExplicitNone?null:W(t.values),a=m.countryFilter??null,o=m.categoryFilter??null,r=m.categoryTaxonomy||(l&&l!=="none"?l:null),n=m.countryTaxonomy||null;return{before:(e==null?void 0:e.endDate)||null,after:(e==null?void 0:e.startDate)||null,categoryFilter:o,categoryTaxonomy:r,countryFilter:a,countryTaxonomy:n}},O=t=>V?t:decodeURIComponent(t),L=t=>{try{return JSON.parse(O(t))}catch(e){return console.warn("JSON parsing failed for value:",t,"- treating as plain string. Error:",e),O(t)}},U=()=>{const t=L(x);return t?typeof t=="string"?t.split(",").map(Number):typeof t=="number"?[t]:t:[]},k=(t,e,a)=>{if(a)return null;if(t.length===0)return e.length>0?e:null;if(e.length===0)return t;const o=new Set(t),r=e.filter(n=>o.has(n));return r.length>0?r:null},K=async()=>{N(!0);const t=J(),e=S(U()).values,a=S(t.categoryFilter),o=k(e,a.values,a.isExplicitNone),r=S(t.countryFilter);if(a.isExplicitNone){F([]),N(!1);return}if(r.isExplicitNone){F([]),N(!1);return}const n=new Map;t.categoryTaxonomy&&o&&o.length>0&&n.set(t.categoryTaxonomy,o),t.countryTaxonomy&&r.values.length>0&&n.set(t.countryTaxonomy,r.values);const v=o?o.join(","):void 0,C={after:t.after,before:t.before,perPage:Number(b||10),page:m.page||1,locale:z||"en",postType:c,taxonomy:t.categoryTaxonomy||void 0,category:v||void 0,taxonomyFilters:n,ordering:"date",orderingDirection:"desc"};await $.getCustomPosts(C).then(s=>{if(s){let h=null,y=null;if(s&&typeof s=="object"&&"data"in s?(h=s.data,y=s.meta):(Array.isArray(s),h=s),F(Array.isArray(h)?h:[]),y){const R=y["x-wp-totalpages"]?y["x-wp-totalpages"]:1,_=y["x-wp-total"]?y["x-wp-total"]:0;R&&_&&B({type:"SET_POSTS_PAGINATION",group:u,totalPages:Number(R),totalItems:Number(_)})}}else F([])}).finally(()=>{N(!1)})};return T.useEffect(()=>{(async()=>await K())()},[I,c,l,b,x,w]),i.jsx(f.Container,{fluid:!0,children:q?i.jsx(f.Loader,{active:!0,inline:"centered"}):!q&&P&&P.length>0?i.jsx(et,{posts:P,postWidth:Number(g),postHeight:Number(d),numberOfColumns:Number(E),sortFirstBy:H,countryCategory:w}):i.jsx(Z.default,{noDataMsg:"No posts found",group:u})})},ot=X.injectIntl(T.memo(rt));exports.default=ot;
