import { getData as q, getCategory as J, getCategories as j } from "./data-api.js";
import * as m from "immutable";
import H from "papaparse";
const y = "LOAD_DATA", T = "LOAD_DATA_DONE", D = "LOAD_DATA_ERROR", R = "LOAD_CATEGORIES", L = "LOAD_CATEGORIES_DONE", O = "LOAD_CATEGORIES_ERROR", F = "SET_PAGE_MODULE_PROPS", h = "RELOAD_CHILD_FILTER_ITEMS", N = "SET_FILTER", V = "SET_FILTERS", C = "SET_INITIAL_FILTER", P = "SET_APPLY", w = "CLEAN_FILTER", M = "UNSET_FILTER", Y = m.Map({ mode: "info" }), v = "SET_MEASURES", U = "CLEAN_MEASURES", K = ({ app: r, group: a }) => (s, e) => {
  s({ type: U, app: r, group: a });
}, Q = ({ app: r, group: a, mGroup: s }) => (e, t) => {
  const o = Object.keys(s.measures).filter((l) => s.measures[l].selected), n = { ...s };
  n[r] = { measures: {} }, o.forEach((l) => {
    n[r].measures[l] = { ...s.measures[l] };
  }), n[r].format = s.format, e({ type: v, app: r, group: a, measure: n });
}, W = ({ app: r, type: a, group: s, param: e, autoApply: t, params: o, parentParam: n, parentType: l, parentSelectedItems: i, uniqueStorage: g, dvzProxyDatasetId: p }) => (c, I) => {
  const E = { ...o };
  E[n] = i, J({ app: r, params: E, type: a }).then(
    (f) => {
      c({
        type: h,
        filterType: a,
        parentType: l,
        param: e,
        app: r,
        group: s,
        data: f,
        uniqueStorage: g,
        dvzProxyDatasetId: p,
        autoApply: t
      });
    }
  );
}, X = (r) => (a, s) => {
  const { app: e, group: t, param: o, value: n, autoApply: l, parent: i } = r;
  console.log("set filters"), a({ type: N, app: e, group: t, param: o, value: n, autoApply: l, parent: i });
}, Z = ({ app: r, group: a }) => (s, e) => {
  s({ type: w, app: r, group: a });
}, k = ({ app: r, group: a, param: s, parent: e }) => (t, o) => {
  t({ type: M, app: r, group: a, param: s, parent: e });
}, ee = ({ app: r, group: a }) => (s, e) => {
  s({ type: P, app: r, group: a });
}, te = ({ app: r, group: a, param: s, value: e }) => (t, o) => {
  t({ type: C, app: r, group: a, param: s, value: e });
}, se = (r) => (a, s) => {
  const { app: e, params: t, dvzProxyDatasetId: o, uniqueStorage: n } = r;
  a({ type: R, params: t, app: e, uniqueStorage: n, dvzProxyDatasetId: o }), j({ app: e, params: t }).then((l) => (l.appliedFilters = t, a({
    type: L,
    app: e,
    data: l,
    uniqueStorage: n,
    dvzProxyDatasetId: o
  }))).catch((l) => a({
    type: O,
    app: e,
    uniqueStorage: n,
    error: l,
    dvzProxyDatasetId: o
  }));
}, re = ({ app: r, group: a, csv: s, store: e, params: t }) => (o, n) => {
  const l = n().get("data").getIn(["filters", r, a]);
  l ? t = { ...t, ...l.toJS() } : t = t || {};
  const i = H.parse(s, { header: !0, dynamicTyping: !0 }), g = i.data.filter((c) => {
    let I = !1;
    return Object.keys(t).forEach((E) => {
      const f = t[E];
      if (c[E]) {
        const S = c[E].toString().trim().toLowerCase();
        I = f.filter((_) => _ && _.toString().trim().toLowerCase() == S).length == 0;
      }
    }), !I;
  }), p = { ...i, data: g, appliedFilters: t };
  o({ type: T, app: r, group: a, store: e, data: { count: p.data.length, itemsSize: p.data.length, ...p } });
}, ne = (r) => (a, s) => {
  const { app: e, group: t, source: o, store: n, params: l, parent: i } = r;
  let g = s().get("data").getIn(["filters", e, t]);
  i && l && Object.keys(l).forEach((I) => {
    if (g && g.has(I)) {
      let E = l[I], S = g.get(I).filter((_) => E.indexOf(_) > -1);
      g = g.set(I, S);
    }
  });
  let p = { ...l };
  g && (p = { ...p, ...g.toJS() }), a({ type: y, app: e, group: t, params: p, store: n }), q({ app: e, source: o, params: p }).then((c) => (c.appliedFilters = p, a({ type: T, app: e, group: t, store: n, data: c, params: p }))).catch((c) => a({ type: D, app: e, group: t, store: n, error: c }));
}, ae = ({ data: r }) => (a, s) => {
  a({ type: F, data: r });
}, oe = (r = Y, a) => {
  switch (a.type) {
    case y: {
      const { store: s, app: e, group: t, params: o } = a, n = Date.now();
      return r.deleteIn([...s, "error"]).setIn([...s, "loading"], !0).setIn([...s, "time"], n).setIn([...s, "presetFilter"], o);
    }
    case D: {
      const { error: s, store: e, app: t, group: o } = a;
      return r.setIn([...e, "loading"], !1).setIn([...e, "error"], s).setIn(["filters-settings", t, o, "apply"], null);
    }
    case T: {
      const { data: s, app: e, group: t, store: o } = a;
      return r.setIn([...o, "loading"], !1).deleteIn([...o, "error"]).setIn([...o, "data"], s).setIn(["filters-settings", e, t, "apply"], null);
    }
    case F: {
      const { data: s } = a;
      return r.setIn(["pageModuleProps"], s);
    }
    case R: {
      const { data: s, app: e, uniqueStorage: t, dvzProxyDatasetId: o } = a, n = ["categories", e];
      return t && n.push(t), o && n.push(o), r.setIn([...n, "loading"], !0).deleteIn([...n, "error"]);
    }
    case L: {
      const { data: s, app: e, uniqueStorage: t, dvzProxyDatasetId: o } = a, n = ["categories", e];
      return o && n.push(o), t && n.push(t), r.setIn([...n, "loading"], !1).setIn([...n, "items"], m.fromJS(s));
    }
    case O: {
      const { data: s, app: e, uniqueStorage: t, dvzProxyDatasetId: o } = a, n = ["categories", e];
      return t && n.push(t), o && n.push(o), r.setIn([...n, "loading"], !1).setIn([...n, "error"], s);
    }
    case P: {
      const { app: s, group: e } = a;
      return r.setIn(["filters-settings", s, e, "apply"], (/* @__PURE__ */ new Date()).getTime());
    }
    case N: {
      const s = Date.now(), { app: e, group: t, param: o, value: n, autoApply: l, parent: i } = a;
      return r.setIn(["filters-settings", e, t, "autoApply"], l).setIn(["filters", e, t, o], n.length === 0 ? [Number.MIN_SAFE_INTEGER] : n).setIn(["filters-settings", e, t, "apply"], null).setIn(["filters-settings", e, t, "lastUserFilterChange"], s).setIn(["filters-settings", e, t, "parent"], i || null);
    }
    case V: {
      const s = Date.now(), { app: e, group: t, filters: o, autoApply: n } = a;
      return r.setIn(["filters-settings", e, t, "autoApply"], n).setIn(["filters", e, t, param], value.length === 0 ? [Number.MIN_SAFE_INTEGER] : value).setIn(["filters-settings", e, t, "apply"], null).setIn(["filters-settings", e, t, "lastUserFilterChange"], s);
    }
    case h: {
      const { data: s, app: e, group: t, param: o, uniqueStorage: n, dvzProxyDatasetId: l, filterType: i, parentType: g, autoApply: p } = a, c = Date.now(), I = ["categories", e];
      l && I.push(l), n && I.push(n);
      const E = s.filter((u) => u.type == i), S = r.getIn([...I, "items"]).filter((u) => u.get("type") == i).get(0).get("items").map((u) => u.value), b = r.getIn([...I, "items"]).filter((u) => u.get("type") != i).push(m.Map(E[0])), x = r.getIn(["filters", e, t, o]), d = s[0].items.map((u) => u.value), G = x.filter((u) => d.indexOf(u) > -1), z = d.filter((u) => S.indexOf(u) == -1), A = Array.from(/* @__PURE__ */ new Set([...G, ...z]));
      return r.setIn([...I, "items"], b).setIn(["filters", e, t, o], Array.from(A)).setIn(["filters-settings", e, t, "autoApply"], p).setIn(["filters", e, t, o], A.length === 0 ? [Number.MIN_SAFE_INTEGER] : A).setIn(["filters-settings", e, t, "apply"], null).setIn(["filters-settings", e, t, "lastUserFilterChange"], c);
    }
    case C: {
      const s = Date.now(), { app: e, group: t, param: o, value: n } = a;
      return r.setIn(["filters", "initial", e, t, o], n.length === 0 ? [Number.MIN_SAFE_INTEGER] : n).setIn(["filters", e, t, o], n.length === 0 ? [Number.MIN_SAFE_INTEGER] : n).setIn(["filters-settings", e, t, "lastInitialFilterChange"], s).setIn(["filters-settings", e, t, "lastUserFilterChange"], s);
    }
    case M: {
      const s = Date.now(), { app: e, group: t, param: o, parent: n } = a;
      return r.deleteIn(["filters", e, t, o]).setIn(["filters-settings", e, t, "lastUserFilterChange"], s).setIn(["filters-settings", e, t, "parent"], n || null);
    }
    case w: {
      const { app: s, group: e } = a, t = r.getIn(["filters", "initial", s, e]);
      return r.setIn(["filters", s, e], t);
    }
    case v: {
      const { app: s, group: e, measure: t } = a;
      return r.setIn(["measures", s, e], t);
    }
    case U: {
      const { app: s, group: e, measure: t } = a;
      return r.deleteIn(["measures", s, e]);
    }
    default:
      return r;
  }
}, le = ({ group: r, dispatch: a, initialFilters: s }) => {
  a({
    type: SET_INITIAL_POSTS_FILTER,
    group: r,
    ...s,
    reset: !0,
    page: 1
  });
};
export {
  ee as applyFilter,
  Z as cleanFilter,
  K as cleanMeasures,
  le as clearPostsFilter,
  oe as default,
  se as getCategories,
  ne as getData,
  W as loadFilterItems,
  re as setData,
  X as setFilter,
  te as setInitialFilters,
  Q as setMeasures,
  ae as setPageModuleProps,
  k as unsetFilter
};
