import { jsx as l, jsxs as L, Fragment as le } from "react/jsx-runtime";
import { useState as Y, useEffect as ie, useRef as oe } from "react";
import { Container as _, Dropdown as F, Segment as z, Label as j, Radio as K, Input as se, Icon as ce, Divider as ue, Checkbox as de } from "semantic-ui-react";
import ne from "../data/CategoriesConsumer.js";
import fe from "../data/CategoriesProvider.js";
import { connect as Q } from "react-redux";
import { setFilter as he, setInitialFilters as me } from "../reducers/data.js";
import { injectIntl as pe } from "react-intl";
const re = "multi-select", B = "single-select", ge = "NO_DATA", xe = "DEFAULT_VALUE_INPUT", Ce = "LOWEST_VALUE", Te = "HIGHEST_VALUE", N = (t) => t instanceof Boolean ? t : t == "true", Se = (t, o) => t ? t.sort((a, c) => a.position - c.position).map((a) => {
  const c = o && a.labels && a.labels[o.toUpperCase()] ? a.labels[o.toUpperCase()] : a.value;
  return {
    key: a.id,
    value: a.id,
    text: c,
    icon: a.value.toLocaleLowerCase(),
    position: a.position ? a.position : a.value
  };
}) : [], ve = (t) => decodeURIComponent(t), ae = (t) => {
  if (!t || t == "")
    return null;
  try {
    return JSON.parse(ve(t));
  } catch (o) {
    throw new Error("error parsing value:" + o);
  }
}, X = (t, o) => {
  const { app: a, group: c, param: i } = o;
  return {
    current: t.getIn(["data", "filters", a, c, i])
  };
}, Z = {
  onInit: me,
  onChange: he
}, ee = (t) => {
  const { isRange: o, options: a, alphabeticalSort: c, ascOrder: i } = t;
  N(c) ? a.sort(function(f, u) {
    const n = f.text ? f.text.toLowerCase() : "", p = u.text ? u.text.toLowerCase() : "";
    return N(i) ? n < p ? -1 : n > p ? 1 : 0 : n < p ? 1 : n > p ? -1 : 0;
  }) : a.sort(function(f, u) {
    return N(i) ? f.position - u.position : u.position - f.position;
  });
  const d = { ...t };
  return o ? /* @__PURE__ */ l(Ne, { ...d }) : /* @__PURE__ */ l(Ee, { ...d });
}, Ee = Q(
  X,
  Z
)((t) => {
  const {
    allLabel: o,
    noneLabel: a,
    placeholder: c,
    options: i,
    app: d,
    group: f,
    param: u,
    current: n,
    onChange: p,
    onInit: s,
    useSingleColumn: A,
    enableTextSearch: x,
    filterType: C,
    defaultValues: v,
    showNoDataOption: I,
    defaultValueCriteria: m,
    allNoneSameBehaviour: E,
    closeOnSelect: b,
    hiddenFilters: w,
    autoApply: U,
    childFilter: y,
    childFilterParam: V,
    params: W,
    uniqueStorage: G,
    dvzProxyDatasetId: M,
    defaultTopNEnabled: $,
    defaultTopNCount: k
  } = t, [te, q] = Y(""), R = (e) => {
    let r = [];
    C != B && !b && n && n.indexOf(e) > -1 ? r = n.filter((g) => g !== e) : C != B && n && !b ? r = [...n, e] : r = [e], p({ app: d, group: f, param: u, value: r, autoApply: U, childFilter: y, childFilterParam: V, params: W, uniqueStorage: G, dvzProxyDatasetId: M }), b && T.current && T.current.close();
  }, J = (e) => {
    const r = i.filter((g) => x && S && S.trim().length > 0 && g.text ? g.text.toLowerCase().includes(S.toLowerCase()) : !0);
    p({
      app: d,
      group: f,
      param: u,
      value: r.map((g) => g.value),
      autoApply: U,
      childFilter: y
    }), b && T.current && T.current.close();
  }, H = () => {
    const e = i.filter((r) => x && S && S.trim().length > 0 && r.text ? r.text.toLowerCase().includes(S.toLowerCase()) : !0);
    p({
      app: d,
      group: f,
      param: u,
      value: E ? e.map((r) => r.value) : [],
      autoApply: U
    }), b && T.current && T.current.close();
  }, D = (e) => {
    O(e);
  };
  ie(() => {
    if (!n) {
      const e = i.map((r) => r.value);
      if (C == re || C == "")
        if ($) {
          const r = k && k > 0 ? k : 0, g = i.slice(0, r).map((h) => h.value);
          s({ app: d, group: f, param: u, value: g });
        } else {
          const r = v ? v.split(",").map((h) => h.trim()).filter((h) => h.length > 0) : [], g = r.length > 0 ? i.filter((h) => r.indexOf(String(h.value)) > -1).map((h) => h.value) : e;
          s({ app: d, group: f, param: u, value: g });
        }
      else if (d == "csv") {
        let r = [];
        m === xe ? r = v ? v.split(",") : [] : m == Ce ? r = e.length > 0 ? [e[0]] : [] : m == Te && (r = e.length > 0 ? [e[e.length - 1]] : []), s({ app: d, group: f, param: u, value: r });
      } else if ($) {
        const r = i.slice(0, 1).map((g) => g.value);
        s({ app: d, group: f, param: u, value: r });
      } else {
        const r = v ? v.split(",").map((h) => h.trim()).filter((h) => h.length > 0) : [], g = r.length > 0 ? i.filter((h) => r.indexOf(String(h.value)) > -1).map((h) => h.value).slice(0, 1) : e.length > 0 ? [e[0]] : [];
        s({ app: d, group: f, param: u, value: g });
      }
    }
  }, []);
  const P = () => {
    if (C == B) {
      const e = n && n[0] ? i.filter((r) => r.value == n[0])[0] : null;
      return `${c} ${e ? e.text : ""}`;
    } else
      return `${c} (${n ? n.filter((e) => e == Number.MIN_SAFE_INTEGER ? !1 : w && w.length > 0 ? w.indexOf(e) == -1 : !0).length : 0}/${i.filter((e) => w && w.length > 0 ? w.indexOf(e.id) == -1 : !0).length}) `;
  }, T = oe(null), [S, O] = Y("");
  return (
    // @ts-ignore
    /* @__PURE__ */ l(
      F,
      {
        ref: T,
        fluid: !0,
        text: P(),
        scrolling: !1,
        button: !0,
        icon: "angle down ignore",
        multiple: !0,
        search: !0,
        floating: !1,
        className: `${n && n.length > 0 ? "applied " : ""}`,
        children: /* @__PURE__ */ L(F.Menu, { children: [
          C != B && /* @__PURE__ */ L(le, { children: [
            /* @__PURE__ */ l(z, { children: /* @__PURE__ */ L(F.Item, { children: [
              /* @__PURE__ */ l(j, { basic: !0, onClick: J, children: o }),
              " ",
              "|",
              " ",
              /* @__PURE__ */ l(j, { basic: !0, onClick: H, children: a })
            ] }) }),
            x && /* @__PURE__ */ L(le, { children: [
              /* @__PURE__ */ l(_, { children: /* @__PURE__ */ l(F.Item, { children: /* @__PURE__ */ l("div", { className: "ui action input", children: /* @__PURE__ */ L(se, { placeholder: "Search...", children: [
                /* @__PURE__ */ l(
                  "input",
                  {
                    className: "filter-search",
                    value: S,
                    onChange: (e) => {
                      D(e.target.value);
                    }
                  }
                ),
                /* @__PURE__ */ l(
                  ce,
                  {
                    name: "remove",
                    link: !0,
                    className: "clear-icon ignore",
                    onClick: (e) => {
                      D("");
                    }
                  }
                )
              ] }) }) }) }),
              /* @__PURE__ */ l(ue, {})
            ] })
          ] }),
          /* @__PURE__ */ l("br", {}),
          /* @__PURE__ */ l(_, { className: A ? "dropdown-single-column" : "", children: i.filter((e) => x && S && S.trim().length > 0 && e.text ? e.text.toLowerCase().includes(S.toLowerCase()) : !0).map(({ value: e, text: r }, g) => /* @__PURE__ */ L(
            F.Item,
            {
              className: A ? "dropdown-item-single-column" : "",
              children: [
                C == B && /* @__PURE__ */ l(
                  K,
                  {
                    checked: !!(n && n.indexOf(e) > -1),
                    onChange: (h) => R(e),
                    label: r
                  }
                ),
                C == re && /* @__PURE__ */ l(
                  de,
                  {
                    checked: !!(n && n.indexOf(e) > -1 && !(i.length == n.length && E)),
                    onChange: (h) => R(e),
                    label: r
                  }
                )
              ]
            },
            g
          )) })
        ] })
      }
    )
  );
}), Ne = Q(
  X,
  Z
)(
  ({
    placeholder: t,
    startLabel: o,
    endLabel: a,
    options: c,
    onChange: i,
    app: d,
    group: f,
    param: u,
    current: n,
    autoApply: p
  }) => {
    const [s, A] = Y(c[0].position), [x, C] = Y(c[c.length - 1].position);
    ie(() => {
      const I = c.filter(
        (m) => (m.position > s || m.position === s) && (m.position < x || m.position === x)
      ).map((m) => m.value);
      i({ app: d, group: f, param: u, value: I, autoApply: p });
    }, [s, x]);
    const v = oe(null);
    return /* @__PURE__ */ l(
      F,
      {
        ref: v,
        fluid: !0,
        text: `${t} (${n ? n.filter((I) => I != Number.MIN_SAFE_INTEGER).length : 0}/${c.length})`,
        scrolling: !1,
        button: !0,
        multiple: !0,
        search: !0,
        floating: !1,
        icon: "angle down ignore",
        className: `${n && n.length > 0 ? "applied " : ""} range`,
        children: /* @__PURE__ */ L(F.Menu, { children: [
          /* @__PURE__ */ l(z, { children: /* @__PURE__ */ L(F.Item, { children: [
            " ",
            /* @__PURE__ */ l(j, { basic: !0, children: o })
          ] }) }),
          /* @__PURE__ */ l(_, { children: c.map(({ value: I, text: m, position: E }) => /* @__PURE__ */ l(F.Item, { children: /* @__PURE__ */ l(
            K,
            {
              disabled: E > x,
              checked: s === E,
              onChange: (b) => A(E),
              label: m
            }
          ) })) }),
          /* @__PURE__ */ l(z, { children: /* @__PURE__ */ L(F.Item, { children: [
            " ",
            /* @__PURE__ */ l(j, { basic: !0, children: a })
          ] }) }),
          /* @__PURE__ */ l(_, { children: c.map(({ value: I, text: m, position: E }) => /* @__PURE__ */ l(F.Item, { children: /* @__PURE__ */ l(
            K,
            {
              disabled: E < s,
              checked: x === E,
              onChange: (b) => C(E),
              label: m
            }
          ) })) })
        ] })
      }
    );
  }
), Fe = (t) => {
  const { data: o, type: a, showNoDataOption: c } = t, i = o.filter((s) => s.type === a)[0], d = i ? i.items.filter((s) => !c && s.code == ge ? !1 : t.hiddenFilters && t.hiddenFilters.length > 0 ? t.hiddenFilters.indexOf(s.id) == -1 : !0) : [], f = d ? Se(d, t.locale) : [], u = o.find((s) => s.type === "FilterDefinition"), n = u ? u.items.find((s) => s.fieldType == a) : null, p = n ? n.value : "Filter";
  return /* @__PURE__ */ l(_, { fluid: !0, className: "filter filter-component", children: /* @__PURE__ */ l(
    ee,
    {
      ...t,
      options: f,
      placeholder: t.placeholder ? t.placeholder : p
    }
  ) });
}, Ie = Q(
  X,
  Z
)((t) => {
  let o = 0;
  const a = [
    {
      key: "Yes",
      value: !0,
      text: "Yes",
      position: o++
    },
    {
      key: "No",
      value: !1,
      text: "No",
      position: o++
    }
  ];
  return /* @__PURE__ */ l(_, { fluid: !0, className: "filter filter-component", children: /* @__PURE__ */ l(ee, { options: a, ...t }) });
}), be = (t) => {
  const { csvValue: o } = t;
  let a = 0;
  const c = o.split(",").map((i) => ({
    key: i,
    value: i,
    text: i,
    icon: i.toLocaleLowerCase(),
    position: a++
  }));
  return /* @__PURE__ */ l(_, { fluid: !0, className: "filter filter-component", children: /* @__PURE__ */ l(ee, { options: c, ...t, children: " " }) });
}, Le = (t) => /* @__PURE__ */ l(we, { ...t }), we = ({
  unique: t,
  "data-group": o,
  "data-app": a,
  "data-child-filter": c,
  "data-child-filter-param": i,
  "data-parent-filter": d,
  "data-parent-filter-param": f,
  "data-dvz-proxy-dataset-id": u,
  "data-param": n,
  "data-icon": p,
  "data-type": s,
  "data-place-holder": A,
  "data-is-range": x = "false",
  "data-all-label": C,
  "data-none-label": v,
  "data-start-label": I,
  "data-end-label": m,
  "data-csv-value": E,
  "data-filters": b = "[]",
  "data-use-single-column": w = "false",
  "data-enable-text-search": U = "false",
  "data-filter-type": y,
  "data-default-values": V,
  "data-show-no-data-option": W = "true",
  "data-default-value-criteria": G = "DEFAULT_VALUE_INPUT",
  "data-hidden-filters": M = "[]",
  "data-all-none-same-behaviour": $ = "false",
  "data-close-on-select": k = "false",
  "data-alphabetical-sort": te = "true",
  "data-asc-order": q = "true",
  "data-auto-apply": R = "true",
  "data-default-top-n-enabled": J = "false",
  "data-default-top-n-count": H = "0",
  intl: D
}) => {
  const P = {}, T = b ? ae(b) : {};
  T && T.forEach && T.forEach((e) => {
    e.value != null && e.value.filter((r) => r != null && r.toString().trim() != "").length > 0 && (P[e.param] = e.value);
  }), u && (P.dvzProxyDatasetId = u);
  const S = M ? ae(M) : [];
  let O;
  return y == null || y == "" ? O = x === "true" ? "range" : "multi-select" : O = y, a === "csv" ? /* @__PURE__ */ l(
    be,
    {
      allLabel: C,
      noneLabel: v,
      isRange: x === "true",
      csvValue: E,
      app: a,
      group: o,
      icon: p,
      placeholder: A,
      startLabel: I,
      endLabel: m,
      param: n,
      ascOrder: q,
      useSingleColumn: w === "true",
      enableTextSearch: U === "true",
      filterType: O,
      defaultValues: V,
      defaultValueCriteria: G,
      allNoneSameBehaviour: $ === "true",
      closeOnSelect: k === "true",
      locale: D.locale
    }
  ) : a ? /* @__PURE__ */ l(
    fe,
    {
      dvzProxyDatasetId: u,
      type: s,
      autoApply: N(R),
      parentType: d,
      params: P,
      param: n,
      parentParam: f,
      app: a,
      group: o,
      uniqueStorage: T.length > 0 ? t : null,
      hiddenFilters: S || [],
      children: /* @__PURE__ */ l(ne, { children: /* @__PURE__ */ L(_, { fluid: !0, children: [
        s === "Boolean" && /* @__PURE__ */ l(
          Ie,
          {
            startLabel: I,
            endLabel: m,
            allLabel: C,
            noneLabel: v,
            isRange: N(x),
            app: a,
            group: o,
            icon: p,
            placeholder: A,
            param: n,
            filterType: O,
            defaultValues: V,
            locale: D.locale
          }
        ),
        s !== "Boolean" && /* @__PURE__ */ l(ne, { type: s, children: /* @__PURE__ */ l(
          Fe,
          {
            params: P,
            childFilter: c,
            parentFilter: d,
            childFilterParam: i,
            startLabel: I,
            endLabel: m,
            allLabel: C,
            noneLabel: v,
            isRange: N(x),
            app: a,
            group: o,
            icon: p,
            placeholder: A,
            param: n,
            alphabeticalSort: te,
            ascOrder: q,
            useSingleColumn: N(w),
            enableTextSearch: N(U),
            showNoDataOption: N(W),
            filterType: O,
            defaultValues: V,
            defaultValueCriteria: G,
            defaultTopNEnabled: N(J),
            defaultTopNCount: H ? parseInt(H) : 0,
            hiddenFilters: S || [],
            allNoneSameBehaviour: $ == "true",
            autoApply: N(R),
            closeOnSelect: N(k),
            locale: D.locale,
            uniqueStorage: T.length > 0 ? t : null,
            dvzProxyDatasetId: u
          }
        ) })
      ] }) })
    }
  ) : null;
}, Ve = pe(Le);
export {
  Ve as default
};
