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