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