import h from "react";
const M = (f) => {
  const { mapType: u } = f;
  return u == "POINTS_MAP" ? v(f) : D(f);
}, v = (f) => {
  const { children: u, data: e, multipleMeasures: o, aggregationFormula: i } = f, s = {
    locationsData: [],
    nationalData: {},
    measures: [],
    measureLabelMap: {}
  }, l = e.meta.fields.findIndex((m, a) => a > 2 && !m.startsWith("_")), n = [];
  if (e && e.data && e.meta.fields && e.meta.fields.length >= 2) {
    e.data.forEach((a) => {
      const t = {
        label: a[e.meta.fields[0]],
        lat: a[e.meta.fields[1]],
        lng: a[e.meta.fields[2]],
        value: a[e.meta.fields[l]] || 1,
        measure: e.meta.fields[l]
      }, d = {};
      if (o && !s.measures.includes(e.meta.fields[l]) && s.measures.push(e.meta.fields[l]), e.meta.fields.length > l) {
        t.children = [];
        for (let r = l + 1; r <= e.meta.fields.length; r++) {
          const p = e.meta.fields[r], c = a[e.meta.fields[r]];
          if (p) {
            if (p.trim().startsWith("_"))
              d[p] = c;
            else if (c != null)
              if (o) {
                const b = { label: a[e.meta.fields[0]], value: c, measure: e.meta.fields[r] };
                b.variables = d, n.push(b), s.measures.includes(e.meta.fields[r]) || s.measures.push(e.meta.fields[r]);
              } else
                t.children.push({ label: e.meta.fields[r], value: c });
          }
        }
      }
      t.variables = d, n.push(t);
    });
    const m = [];
    n.forEach((a) => {
      let t = m.find((d) => d.label == a.label);
      t ? i == "COUNT" ? ++t.value : i == "SUM" && (t.value += a.value) : (t = { label: a.label, value: i == "COUNT" ? 1 : a.value ? a.value : 0 }, m.push(t));
    }), s.pointsData = n, s.locationsData = m, s.nationalData.value = 0;
  }
  return h.Children.map(u, (m) => h.cloneElement(m, { transformedData: s, appliedFilters: e.appliedFilters }));
}, D = (f) => {
  const { children: u, data: e, multipleMeasures: o } = f, i = {
    locationsData: [],
    nationalData: {},
    measures: [],
    measureLabelMap: {}
  }, s = e.meta.fields.findIndex((l, n) => n != 0 && !l.startsWith("_"));
  return e && e.data && e.meta.fields && e.meta.fields.length >= 2 && (e.data.forEach((l) => {
    const n = {
      label: l[e.meta.fields[0]],
      value: l[e.meta.fields[s]],
      measure: e.meta.fields[s]
    }, m = {};
    if (o && !i.measures.includes(e.meta.fields[s]) && i.measures.push(e.meta.fields[s]), e.meta.fields.length > s) {
      n.children = [];
      for (let a = s + 1; a <= e.meta.fields.length; a++) {
        const t = e.meta.fields[a], d = l[e.meta.fields[a]];
        if (t)
          if (t.trim().startsWith("_"))
            m[t] = d;
          else if (o) {
            const r = { label: l[e.meta.fields[0]], value: d, measure: e.meta.fields[a] };
            r.variables = m, i.locationsData.push(r), i.measures.includes(e.meta.fields[a]) || i.measures.push(e.meta.fields[a]);
          } else
            n.children.push({ label: e.meta.fields[a], value: d });
      }
    }
    n.variables = m, i.locationsData.push(n);
  }), i.nationalData.value = 0), h.Children.map(u, (l) => h.cloneElement(l, { transformedData: i, appliedFilters: e.appliedFilters }));
};
export {
  M as default
};
