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