import { jsx as E, jsxs as G } from "react/jsx-runtime";
import { useState as X, useRef as ee, useEffect as T } from "react";
import { Container as te, Grid as I } from "semantic-ui-react";
import { BigNumberItem as re } from "./BigNumberItem.js";
import { decode as le } from "../utils/parseUtils.js";
import ne from "lodash";
const pe = (_) => {
  const {
    group: s,
    app: r,
    parent: i,
    nColumns: k,
    height: y,
    blockName: m,
    measures: p,
    dimension1: n,
    dimension2: B,
    data: o,
    numberFontSize: L,
    backgroundColor: M,
    numberColor: j,
    labelColor: P,
    unselectedBackgroundColor: D,
    unselectedNumberColor: O,
    unselectedLabelColor: w,
    labelFontSize: z,
    intl: K,
    onUnSetFilter: d,
    onSetFilter: a,
    appliedFilters: g,
    ///self applied filters
    hasParentFilters: h,
    //util flag
    effectiveFilter: oe,
    //all filter toghether
    parentAppliedFilters: f,
    //parent applied filter
    sort: U,
    order: Y,
    showZeroValues: Z
  } = _, u = p[r] ? Object.keys(p[r]).find((e) => p[r][e].selected) : null, N = m, [F, b] = X([]), $ = p[r] && p[r].format ? p[r].format : {
    style: "percent",
    minimumFractionDigits: 1,
    maximumFractionDigits: 1
  }, q = (e, t) => {
    const l = e[u], c = t[u], R = Y === "asc" ? 1 : -1;
    if (U === "alpha") {
      const C = S(e.value) || "", A = S(t.value) || "";
      return R * C.localeCompare(A);
    } else {
      const C = parseFloat(l) || 0, A = parseFloat(c) || 0;
      return R * (C - A);
    }
  }, S = (e) => {
    var l;
    if (!o || !o.metadata || !o.metadata.types) return null;
    const t = o.metadata.types.filter((c) => c.dimension == n);
    return t && t.length > 0 ? (l = t[0].items.filter((c) => c.code == e)[0]) == null ? void 0 : l.value : null;
  }, H = ee(
    ne.debounce((e, t) => {
      e.length == 0 ? (a({ app: r, group: s, parent: i, param: t, value: [] }), d({ app: r, group: m, parent: i, param: t })) : (a({ app: r, group: s, param: t, parent: i, value: [...e] }), a({ app: r, group: m, param: t, parent: i, value: [...e] }));
    }, 400)
    // 400ms delay
  ).current, J = (e, t) => {
    if (h && (!f || f.length === 0)) return;
    let l = [...F];
    l.includes(e) ? l = l.filter((c) => c !== e) : l.push(e), b(l), H(l, t);
  }, v = o.children.filter((e) => Z === "true" ? e[u] !== null && e[u] !== void 0 : e[u] && e[u] > 0).sort(q);
  T(() => {
    a({ app: r, group: s, param: n, value: [Number.MIN_SAFE_INTEGER] }), d({ app: r, group: m, parent: i, param: n });
  }, []), o.children.map((e) => e.value);
  const x = v.map((e) => e.value);
  T(() => {
    const e = g ? g.filter((t) => !x.includes(t)) : [];
    if (h && f.length == 0)
      console.log(m, "RESETING FILTER; PARENT IS EMPTY"), a({ app: r, group: s, param: n, value: [Number.MIN_SAFE_INTEGER] }), d({ app: r, group: N, param: n }), b([]);
    else if (e.length > 0) {
      const t = g.filter((l) => x.indexOf(l) > -1);
      t.length == 0 ? (b([]), a({ app: r, group: s, param: n, value: [Number.MIN_SAFE_INTEGER] }), d({ app: r, group: N, param: n })) : (a({ app: r, group: s, param: n, value: t }), a({ app: r, group: N, parent: i, param: n, value: t }));
    } else
      b(g);
  }, [o]);
  const Q = F ? F.length : 0, V = v ? v.length : 0, W = h && (!f || f.length === 0);
  return n == null ? /* @__PURE__ */ E("h2", { children: "Select a dimension to start configuring the component" }) : /* @__PURE__ */ E(te, { fluid: !0, style: { padding: "0px", margin: "0px", height: `${y}px` }, children: /* @__PURE__ */ G(I, { fluid: !0, celled: !0, stackable: !0, columns: k, style: { border: "1px solid #EEE" }, children: [
    /* @__PURE__ */ E(I.Row, { children: /* @__PURE__ */ G(I.Column, { width: 16, textAlign: "right", children: [
      "Selected    ",
      Q,
      "/",
      V
    ] }) }),
    o.children && v.map((e, t) => /* @__PURE__ */ E(
      re,
      {
        idx: t,
        child: e,
        isDisabled: W,
        selectedKey: u,
        appliedFilters: F,
        dimension1: n,
        dimension2: B,
        app: r,
        group: s,
        parent: i,
        blockName: m,
        hasParentFilters: h,
        onSetFilter: (l) => {
        },
        handleClick: () => J(e.value, e.type),
        onUnSetFilter: d,
        intl: K,
        numberFontSize: L,
        labelFontSize: z,
        backgroundColor: M,
        numberColor: j,
        labelColor: P,
        unselectedBackgroundColor: D,
        unselectedNumberColor: O,
        unselectedLabelColor: w,
        formatObject: $,
        decode: le,
        getLabel: S
      },
      t
    ))
  ] }) });
};
export {
  pe as default
};
