import { jsx as J, Fragment as P } from "react/jsx-runtime";
import F from "react";
import { measuresMap as U, typesMap as q, getTranslatedValue as N, alphaSort as V, dateSort as _, numericSort as T } from "./Utils.js";
const z = (a) => {
  const { data: t, measures: v, swap: w, dimensions: g, locale: E, customLabels: O } = a;
  let n = {};
  const R = g.filter((j) => j != ""), C = /* @__PURE__ */ new Set();
  if (R.length == 0 && t) {
    const j = U(t), A = /* @__PURE__ */ new Set(), M = /* @__PURE__ */ new Set();
    let u = [], r;
    if (t.metadata && t.metadata.measures) {
      const y = t.metadata.measures.filter((o) => v.includes(o.value)).sort((o, p) => o.position != null && p.position != null && o.position != p.position ? o.position - p.position : 0);
      u = [], r = "measure", A.add("measure");
      const x = {};
      Object.keys(t).forEach((o) => {
        x[o] = t[o];
      }), y.forEach((o) => {
        const p = {}, b = O[o.value] || N(j[o.value], E);
        p.type = "measure", p.measureFieldName = o.value, p.measure = b, p[b] = t[o.value], p.variables = x, u.push(p), M.add(b), C.add(j[o.value]);
      }), n = {
        categories: A,
        indexBy: r,
        keys: Array.from(M),
        measuresMetadata: C,
        data: u
      };
    }
  }
  return n;
}, G = (a) => {
  const { data: t, measures: v, dimensions: w, overallLabel: g } = a;
  if (w.length == 1 && t.children) {
    if (!(t.children.filter((O) => O.value == g).length > 0)) {
      const O = {};
      O.type = w[0], O.value = g, O.label = g, Object.keys(t).forEach((n) => {
        ["children", "metadata", "type", "value"].includes(n) || (O[n] = t[n]);
      }), t.children = [O, ...t.children];
    }
  } else w.length == 2 && t.children && t.children.forEach((E) => {
    if (!(E.children.filter((n) => n.value == g).length > 0)) {
      const n = {};
      n.type = w[1], n.value = g, n.label = g, Object.keys(E).forEach((R) => {
        ["children", "metadata", "type", "value"].includes(R) || (n[R] = E[R]);
      }), E.children = [n, ...E.children];
    }
  });
  return t;
}, Q = (a) => {
  var u;
  let t = {};
  const {
    data: v,
    measures: w,
    swap: g,
    dimensions: E,
    includeOverall: O,
    locale: n,
    customLabels: R,
    colorBy: C,
    hiddenBars: j
  } = a, A = E.filter((r) => r != ""), M = (u = v == null ? void 0 : v.metadata) == null ? void 0 : u.measures.filter((r) => w.includes(r.value)).sort((r, y) => r.position != null && y.position != null && r.position != y.position ? r.position - y.position : 0);
  if (O && w.length == 1 && G(a), A.length == 0 && v)
    t = z(a);
  else if (v && v.children && A.length > 0) {
    const r = U(v), y = q(v), x = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set(), p = /* @__PURE__ */ new Set(), b = [];
    let B;
    g && A.length == 1 && w.length > 0 ? (B = "measure", M.forEach((s) => {
      const c = {};
      c.measure = R[s.value] || N(r[s.value], n), o.add(r[s.value]), v.children.forEach((m) => {
        const f = N(
          y[m.type].items.filter((i) => i.value === m.value)[0],
          n
        ) || m.value, e = {};
        Object.keys(m).forEach((i) => {
          e[i] = m[i];
        }), e[m.type] = m.value.toString(), c.variables = e, x.add(y[m.type]), c[f] = m[s.value], p.add(f);
      }), b.push({ ...c });
    })) : (v.children[0] && (B = v.children[0].type), v.children.forEach((s) => {
      const c = {}, m = {};
      m[s.type] = N(
        y[s.type] && y[s.type].items ? y[s.type].items.filter((f) => f.value === s.value)[0] : s.value,
        n
      ) || s.value, Object.keys(s).forEach((f) => {
        c[f] = s[f];
      }), x.add(y[s.type]), c[s.type] = s.value.toString(), M.map((f) => {
        const e = R[f.value] || N(r[f.value], n);
        m[e] = s[f.value], o.add(r[f.value]), p.add(e);
      }), b.push({ ...m, variables: c, parent_variables: c });
    }));
    const D = Array.from(p);
    let S = j && b ? b.filter((s) => j.indexOf(s[B]) == -1) : b;
    a.sort == "alphabetically" && (S = S.sort(
      (s, c) => V(a.sortReverse, n, s[B], c[B])
    )), a.sort == "date" && (S = S.sort(
      (s, c) => _(a.sortReverse, s[B], c[B])
    )), a.sort == "values" && (S = S.sort((s, c) => {
      const m = Math.max(...D.map((e) => s[e])), f = Math.max(...D.map((e) => c[e]));
      return T(a.sortReverse, m, f);
    })), t = {
      metadata: v.metadata,
      indexBy: B,
      dimensionsMetadata: x,
      measuresMetadata: o,
      keys: D,
      data: S
    };
  }
  return F.Children.map(
    a.children,
    (r) => F.cloneElement(r, { options: t })
  );
}, W = (a) => {
  var A, M;
  const {
    data: t,
    measures: v,
    includeOverall: w,
    dimensions: g,
    hiddenBars: E,
    colorBy: O,
    locale: n,
    customLabels: R
  } = a, C = g.filter((u) => u != "");
  let j = {};
  if (w && G(a), C.length == 0 && t)
    j = z(a);
  else if (t && t.children && t.children.length > 0 && C.length > 0) {
    U(t);
    const u = q(t), r = v[0], y = /* @__PURE__ */ new Set(), x = /* @__PURE__ */ new Set(), o = [], p = [], b = t.children[0].type;
    let B = 0, D, S;
    t.children.forEach((e) => {
      const i = { variables: {} };
      if (S = N(
        u[e.type] && u[e.type].items ? u[e.type].items.filter((l) => l.value === e.value)[0] : e.value,
        n
      ) || e.value, i[e.type] = S, i[S] = e[r], D = new Object(), D[e.type] = S, i.parent_variables = D, Object.keys(e).forEach((l) => {
        D[l] = e[l];
      }), y.add(u[e.type]), e.children || x.add(S), e.children)
        e.children.forEach((l) => {
          D = new Object(), y.add(u[l.type]);
          const h = N(
            u[l.type] && u[l.type].items ? u[l.type].items.filter((d) => d.value === l.value)[0] : l.value,
            n
          ) || l.value;
          D[e.type] = S, D[l.type] = h, Object.keys(l).forEach((d) => {
            D[d] = l[d];
          }), i.variables[h] = D, x.add(h), B += l[r], p.push(l[r]), i[h] = l[r];
        });
      else {
        const l = new Object();
        l[e.type] = S, y.add(u[e.type]), Object.keys(t).forEach((h) => {
          l[h] = e[h];
        }), i.variables = l;
      }
      o.push(i);
    });
    const s = n.toUpperCase(), c = O == "id" ? o : o.filter((e) => E.indexOf(e[b]) == -1), m = Array.from(x);
    a.sort == "alphabetically" ? c.sort(
      (e, i) => V(a.sortReverse, n, e[b], i[b])
    ) : a.sort == "date" ? c.sort(
      (e, i) => _(a.sortReverse, e[b], i[b])
    ) : a.sort == "values" ? c.sort((e, i) => {
      const l = m.map((d) => e[d]).filter((d) => d).reduce((d, K) => d + K), h = m.map((d) => i[d]).filter((d) => d).reduce((d, K) => d + K);
      return console.log(m.map((d) => e[d])), l == null ? 1 : h == null ? -1 : T(a.sortReverse, l, h);
    }) : c.sort((e, i) => {
      const l = e[a.sort], h = i[a.sort];
      return l == null ? 1 : h == null ? -1 : T(a.sortReverse, l, h);
    });
    const f = [...x];
    ((M = (A = t == null ? void 0 : t.metadata) == null ? void 0 : A.types) == null ? void 0 : M.length) > 1 && f.sort((e, i) => {
      var l, h;
      if (a.sortSecondDimension == "date")
        return _(a.sortReverseSecondDimension, e, i);
      if (a.sortSecondDimension == "alphabetically")
        return V(a.sortReverseSecondDimension, n, e, i);
      {
        const d = t.metadata.types[1].items.filter(
          (L) => L.value == e || L.labels && L.labels[s] == e
        ), K = t.metadata.types[1].items.filter(
          (L) => L.value == i || L.labels && L.labels[s] == i
        ), H = (l = d[0]) == null ? void 0 : l.position, I = (h = K[0]) == null ? void 0 : h.position;
        return H - I;
      }
    }), j = {
      metadata: t.metadata,
      dimensionsMetadata: y,
      indexBy: b,
      keys: O == "index" ? f : f.filter((e) => E.indexOf(e) == -1),
      data: c
    };
  }
  return /* @__PURE__ */ J(P, { children: F.Children.map(
    a.children,
    (u) => F.cloneElement(u, { options: j })
  ) });
}, $ = (a) => {
  const { data: t, measures: v, dimensions: w } = a, g = JSON.parse(JSON.stringify(t));
  return w.length === 1 ? /* @__PURE__ */ J(Q, { ...a, data: g }) : /* @__PURE__ */ J(W, { ...a, data: g });
};
export {
  $ as default
};
