import { getData as w, getCategories as b } from "./data-api.js";
import * as _ from "immutable";
import G from "papaparse";
const d = "LOAD_DATA", T = "LOAD_DATA_DONE", F = "LOAD_DATA_ERROR", O = "LOAD_CATEGORIES", R = "LOAD_CATEGORIES_DONE", D = "LOAD_CATEGORIES_ERROR", L = "SET_PAGE_MODULE_PROPS", f = "SET_FILTER", m = "SET_INITIAL_FILTER", v = "SET_POSTS_FILTER", P = "SET_INITIAL_POSTS_FILTER", U = "SET_POSTS_PAGINATION", h = "SET_APPLY", C = "CLEAN_FILTER", q = _.Map({ mode: "info" }), N = "SET_MEASURES", x = "CLEAN_MEASURES", J = ({ app: r, group: o }) => (e, s) => {
  e({ type: x, app: r, group: o });
}, V = ({ app: r, group: o, mGroup: e }) => (s, t) => {
  const n = Object.keys(e.measures).filter((l) => e.measures[l].selected), a = { ...e };
  a[r] = { measures: {} }, n.forEach((l) => {
    a[r].measures[l] = { ...e.measures[l] };
  }), a[r].format = e.format, s({ type: N, app: r, group: o, measure: a });
}, Y = ({ app: r, group: o, param: e, value: s, autoApply: t }) => (n, a) => {
  n({ type: f, app: r, group: o, param: e, value: s, autoApply: t });
}, $ = ({ app: r, group: o }) => (e, s) => {
  e({ type: C, app: r, group: o });
  const n = s().getIn(["data", "filters", "initial", r, o]);
  if (n) {
    const a = n.entrySeq ? n.entrySeq().toArray() : Object.entries(n);
    for (const [l, i] of a)
      e({
        type: f,
        app: r,
        group: o,
        param: l,
        value: i,
        autoApply: !0
      });
  }
}, H = ({ app: r, group: o }) => (e, s) => {
  e({ type: h, app: r, group: o });
}, K = ({ app: r, group: o, param: e, value: s }) => (t, n) => {
  t({ type: m, app: r, group: o, param: e, value: s });
}, Q = (r) => (o, e) => {
  const { app: s, params: t, dvzProxyDatasetId: n, uniqueStorage: a } = r;
  o({ type: O, params: t, app: s, uniqueStorage: a, dvzProxyDatasetId: n }), b({ app: s, params: t }).then((l) => (l.appliedFilters = t, o({
    type: R,
    app: s,
    data: l,
    uniqueStorage: a,
    dvzProxyDatasetId: n
  }))).catch((l) => o({
    type: D,
    app: s,
    uniqueStorage: a,
    error: l,
    dvzProxyDatasetId: n
  }));
}, W = ({ app: r, group: o, csv: e, store: s, params: t }) => (n, a) => {
  const l = a().get("data").getIn(["filters", r, o]);
  l ? t = { ...t, ...l.toJS() } : t = t || {};
  const i = G.parse(e, { header: !0, dynamicTyping: !0 }), c = i.data.filter((I) => {
    let g = !1;
    return Object.keys(t).forEach((y) => {
      const S = t[y];
      if (I[y]) {
        const A = I[y].toString().trim().toLowerCase();
        g = S.filter((p) => p && p.toString().trim().toLowerCase() == A).length == 0;
      }
    }), !g;
  }), u = { ...i, data: c, appliedFilters: t };
  n({ type: T, app: r, group: o, store: s, data: { count: u.data.length, itemsSize: u.data.length, ...u } });
}, X = ({ app: r, group: o, source: e, store: s, params: t }) => (n, a) => {
  let l = a().get("data").getIn(["filters", r, o]);
  t && Object.keys(t).forEach((c) => {
    if (l && l.has(c)) {
      let u = t[c], g = l.get(c).filter((y) => u.indexOf(y) > -1);
      l = l.set(c, g);
    }
  }), l && (t = { ...t, ...l.toJS() }), n({ type: d, app: r, group: o, params: t, store: s }), w({ app: r, source: e, params: t }).then((i) => (i.appliedFilters = t, n({ type: T, app: r, group: o, store: s, data: i, params: t }))).catch((i) => n({ type: F, app: r, group: o, store: s, error: i }));
}, Z = ({ data: r }) => (o, e) => {
  o({ type: L, data: r });
}, k = (r = q, o) => {
  switch (o.type) {
    case d: {
      const { store: e, app: s, group: t, params: n } = o, a = Date.now();
      return r.deleteIn([...e, "error"]).setIn([...e, "loading"], !0).setIn([...e, "time"], a).setIn([...e, "presetFilter"], n);
    }
    case F: {
      const { error: e, store: s, app: t, group: n } = o;
      return r.setIn([...s, "loading"], !1).setIn([...s, "error"], e).setIn(["filters-settings", t, n, "apply"], null);
    }
    case T: {
      const { data: e, app: s, group: t, store: n } = o;
      return r.setIn([...n, "loading"], !1).deleteIn([...n, "error"]).setIn([...n, "data"], e).setIn(["filters-settings", s, t, "apply"], null);
    }
    case L: {
      const { data: e } = o;
      return r.setIn(["pageModuleProps"], e);
    }
    case O: {
      const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: n } = o, a = ["categories", s];
      return t && a.push(t), n && a.push(n), r.setIn([...a, "loading"], !0).deleteIn([...a, "error"]);
    }
    case R: {
      const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: n } = o, a = ["categories", s];
      return n && a.push(n), t && a.push(t), r.setIn([...a, "loading"], !1).setIn([...a, "items"], _.fromJS(e));
    }
    case D: {
      const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: n } = o, a = ["categories", s];
      return t && a.push(t), n && a.push(n), r.setIn([...a, "loading"], !1).setIn([...a, "error"], e);
    }
    case h: {
      const { app: e, group: s } = o;
      return r.setIn(["filters-settings", e, s, "apply"], (/* @__PURE__ */ new Date()).getTime());
    }
    case f: {
      const e = Date.now(), { app: s, group: t, param: n, value: a, autoApply: l } = o;
      return r.setIn(["filters-settings", s, t, "autoApply"], l).setIn(["filters", s, t, n], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters-settings", s, t, "apply"], null).setIn(["filters-settings", s, t, "lastUserFilterChange"], e);
    }
    case m: {
      const e = Date.now(), { app: s, group: t, param: n, value: a, autoApply: l } = o;
      return console.log(n), r.setIn(["filters-settings", s, t, "autoApply"], l).setIn(["filters", "initial", s, t, n], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters", s, t, n], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters-settings", s, t, "lastInitialFilterChange"], e).setIn(["filters-settings", s, t, "lastUserFilterChange"], e);
    }
    case C: {
      const { app: e, group: s } = o, t = r.getIn(["filters", "initial", e, s]);
      return r.setIn(["filters", e, s], t);
    }
    case P: {
      const {
        group: e,
        sortFirstBy: s,
        categoryCategory: t,
        countryCategory: n,
        categoryTaxonomy: a,
        countryTaxonomy: l,
        categoryFilter: i,
        countryFilter: c,
        yearFilter: u
      } = o, I = Number(s), g = {
        yearFilter: u,
        categoryFilter: i,
        categoryTaxonomy: a,
        categoryCategory: t,
        countryCategory: n,
        countryFilter: c,
        countryTaxonomy: l,
        page: 1,
        itemsPerPage: void 0,
        sortFirstBy: I,
        postTaxonomy: null,
        postCategory: null,
        postType: null
      };
      return r.setIn(["posts", e], g).setIn(["posts", "initialFilters", e], g);
    }
    case v: {
      const {
        group: e,
        yearFilter: s,
        categoryFilter: t,
        countryFilter: n,
        page: a,
        itemsPerPage: l,
        sortFirstBy: i,
        countryCategory: c,
        categoryCategory: u,
        taxonomy: I,
        categoryTaxonomy: g,
        countryTaxonomy: y
      } = o, S = r.getIn(["posts", e]) || {}, A = {
        yearFilter: s,
        categoryFilter: t,
        countryFilter: n,
        page: a,
        itemsPerPage: l,
        sortFirstBy: i,
        countryCategory: c,
        // preserve or set taxonomy helpers needed by filters UI and querying
        categoryCategory: u !== void 0 ? u : S.categoryCategory,
        taxonomy: I,
        categoryTaxonomy: g !== void 0 ? g : S.categoryTaxonomy,
        countryTaxonomy: y !== void 0 ? y : S.countryTaxonomy
      };
      let p = r.setIn(["posts", e], A);
      const E = r.getIn(["posts", "initialFilters", e]);
      if ((!E || E && E.yearFilter === null && E.categoryFilter === null && E.countryFilter === null && E.categoryCategory === null) && (Array.isArray(t) && t.length > 0 || Array.isArray(n) && n.length > 0 || Array.isArray(s) && s.length > 0)) {
        const M = { ...A };
        p = p.setIn(["posts", "initialFilters", e], M);
      }
      return p;
    }
    case U: {
      const { group: e, totalPages: s, totalItems: t } = o;
      return r.setIn(["postsPagination", e], { totalPages: s, totalItems: t });
    }
    case N: {
      const { app: e, group: s, measure: t } = o;
      return r.setIn(["measures", e, s], t);
    }
    case x: {
      const { app: e, group: s, measure: t } = o;
      return r.deleteIn(["measures", e, s]);
    }
    default:
      return r;
  }
}, tt = ({ group: r, dispatch: o, initialFilters: e }) => {
  o({
    type: P,
    group: r,
    ...e,
    reset: !0,
    page: 1
  });
};
export {
  H as applyFilter,
  $ as cleanFilter,
  J as cleanMeasures,
  tt as clearPostsFilter,
  k as default,
  Q as getCategories,
  X as getData,
  W as setData,
  Y as setFilter,
  K as setInitialFilters,
  V as setMeasures,
  Z as setPageModuleProps
};
