/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { W as P, w as U } from "./series-types.js";
import { isEqual as Y, capitalize as F, isNumber as A, zip as H } from "lodash-es";
import { o as T, bR as W, cM as X, dp as c, ab as O, bG as h, ds as C, ay as w, bT as J, aj as B, bD as ee, bE as K } from "./index.js";
import { b as R } from "./interfaces.js";
import { a as z } from "./common.js";
import "@arcgis/toolkit/type";
import { R as te } from "./rest-js-object-literals.js";
import { j as oe, n as N, p as Z, o as I } from "./box-plot-data.js";
import { a as q } from "./series-length.js";
import { g as ie } from "./histogram-data.js";
import { g as ae, a as se, b as ne } from "./pie-chart-data.js";
import { R as re, S as le, T as ce } from "./serial-chart-data.js";
import { b as ue, g as Q } from "./data.js";
const Ve = [
  P.BarSeries,
  P.BoxPlotSeries,
  P.LineSeries,
  P.PieSeries,
  P.HistogramSeries,
  P.ScatterSeries
], ke = 1, Re = { PNG: "png" };
function de(n, e) {
  return !Y(n, e);
}
function Ke(n, e) {
  return de(n, e) || n !== e;
}
function fe(n) {
  return (new Intl.NumberFormat(n, {
    minimumFractionDigits: 1,
    maximumFractionDigits: 1
  }).formatToParts(1.1).find((r) => r.type === "decimal")?.value ?? ".") === "." ? "," : ";";
}
function me(n) {
  return new Intl.NumberFormat(n, {
    minimumFractionDigits: 1,
    maximumFractionDigits: 1
  }).formatToParts(1.1).find((t) => t.type === "decimal")?.value ?? ".";
}
const E = (n) => {
  const { value: e, quoteAll: t = !1, locale: r } = n;
  let i = e == null ? "" : String(e);
  if (e === null)
    i = "null";
  else if (e === void 0)
    i = "";
  else if (typeof e == "number" && Number.isFinite(e) && r !== void 0) {
    const a = me(r), f = i.split(a)[1]?.length ?? 3;
    i = new Intl.NumberFormat(r, {
      maximumFractionDigits: f
    }).format(e).replace(/\u202F/gu, " ");
  }
  return (t || /[",\r\n]/u.test(i)) && (i = `"${i.replace(/"/gu, '""')}"`), i;
};
function Ne(n, e = {}) {
  const { columns: t, delimiter: r, includeHeader: i = !0, quoteAll: o = !1, locale: a } = e, f = n.length === 0;
  let l = "";
  if (!f) {
    const m = t ?? Object.keys(n[0]), x = [], p = r ?? fe(a ?? "en");
    if (a !== void 0 && p !== "," && (l += `sep=${p}\r
`), i) {
      const y = m.map((s) => E({ value: s, quoteAll: o, locale: a }));
      x.push(y.join(p));
    }
    n.forEach((y) => {
      const s = m.map((d) => E({ value: y[d], quoteAll: o, locale: a }));
      x.push(s.join(p));
    }), l += x.join(`\r
`);
  }
  return l;
}
function Ie(n) {
  const { rawData: e, config: t, layer: r, fieldList: i } = n;
  let o = e;
  if (t !== void 0 && Array.isArray(e) && e.length > 0) {
    const a = U(t?.series);
    a === P.HistogramSeries ? o = ye(t, e) : a === P.BarSeries || a === P.LineSeries || a === P.ComboLineAndBarSeries ? o = ge({
      config: t,
      rows: e,
      layer: r,
      fieldList: i
    }) : a === P.PieSeries ? o = xe({
      config: t,
      rows: e,
      layer: r,
      fieldList: i
    }) : a === P.BoxPlotSeries && (o = Be({
      config: t,
      rows: e,
      fieldList: i
    }));
  }
  return o;
}
function ye(n, e) {
  const t = ie(n), r = `${F(R.Min)} of ${t}`, i = `${F(R.Max)} of ${t}`, o = `${F(R.Count)} of ${t}`;
  return e.map((a) => {
    const { minValue: f, maxValue: l, count: m } = a, x = A(f), p = A(l), y = A(m), s = {};
    return x && (s[r] = f.toFixed(2)), p && (s[i] = l.toFixed(2)), x && p && (s.Label = `${f.toFixed(2)} - ${l.toFixed(2)}`), y && (s[o] = m), s;
  });
}
function pe(n, e, t) {
  const r = Object.keys(n);
  let i = [];
  return t ? i = t.map((o) => r.find((a) => a.toLowerCase().includes(o.toLowerCase())) || "") : i = r.filter((a) => a.toLowerCase().includes(e.toLowerCase())), i.filter((o) => o.trim() !== "");
}
function ge(n) {
  const { config: e, rows: t, layer: r, fieldList: i } = n, o = ue(e), a = re(e), f = F(a), l = le(e), m = T(e.series), x = q(e), p = ce(e), { idField: y, isTraditionalTypeOID: s } = W(X(r)), d = s && l.length === 1 && l[0] === y;
  let u;
  switch (m) {
    case c.BarAndLineNoAggregation:
      u = t.map((g) => {
        const b = $({
          field: o,
          value: g[o],
          fieldList: i
        }), S = { [o]: b };
        return Object.assign(S, ...l.map((D) => ({ [D]: g[D] }))), S;
      });
      break;
    case c.BarAndLineFromFields:
      u = H(l, t).map(([g, b]) => {
        const S = O(e.series)[0], D = {};
        return b !== void 0 && g !== void 0 && (D.Field = b[o], D.Value = b[S]), D;
      });
      break;
    case c.BarAndLineMonoField:
    case c.BarAndLineMultiFields:
      u = t.map((g) => {
        const b = $({
          field: o,
          value: g[o],
          fieldList: i
        }), S = { [o]: b };
        return l.forEach((D, v) => {
          const V = Q({
            config: e,
            xAxisField: o,
            numericFields: l,
            aggregationType: a,
            layer: r,
            numericFieldIndex: v,
            fieldList: i
          }), k = O(e.series)[v];
          let j = k, _ = k;
          k.toLowerCase() === V.name.toLowerCase() && (j = V.description, _ = V.name), S[j] = g[_];
        }), S;
      });
      break;
    case c.BarAndLineSplitBy:
      u = t.flatMap((g) => {
        const b = O(e.series), S = $({
          field: o,
          value: g[o],
          fieldList: i
        });
        return x.map(
          (D, v) => ({
            [o]: S,
            [p]: D,
            [d ? f : `${f} of ${l[0]}`]: g[b[v]]
          })
        );
      });
      break;
    case c.BarAndLineSplitByNoAggregation:
      u = t.map((g) => {
        const b = $({
          field: o,
          value: g[o],
          fieldList: i
        }), S = pe(g, l[0], x);
        return {
          [o]: b,
          [p]: g[p],
          [l[0]]: g[S[0]]
        };
      });
      break;
    default:
      u = t;
  }
  return u;
}
function Fe(n) {
  if (n.length === 0)
    return [];
  const e = n.reduce((t, r) => {
    for (const [i, o] of Object.entries(r))
      typeof o == "number" && Number.isFinite(o) && (t[i] = (t[i] ?? 0) + o);
    return t;
  }, {});
  return n.map((t) => {
    const r = { ...t };
    for (const [i, o] of Object.entries(t))
      if (typeof o == "number" && e[i] > 0) {
        const a = o / e[i];
        r[`${i}${h}`] = a.toPrecision(2);
      }
    return r;
  });
}
function xe(n) {
  const { config: e, rows: t, layer: r, fieldList: i } = n, o = ae(e), a = T(e.series);
  let f = Fe(t);
  const l = se(e), m = a !== c.PieFromFields ? z.Category : z.Fields, x = ne(e, m), p = Q({
    config: e,
    xAxisField: o,
    numericFields: l,
    aggregationType: x,
    mode: m,
    layer: r,
    fieldList: i
  }), y = O(e.series)[0];
  return a === c.PieFromCategory || a === c.PieNoAggregation ? f = f.map((s) => {
    const d = $({
      field: o,
      value: s[o],
      fieldList: i
    }), u = { [o]: d };
    let g = y, b = y;
    return a === c.PieFromCategory && y.toLowerCase() === p.name.toLowerCase() && (g = p.description, b = p.name), u[g] = s[b], u[F(h.trim())] = s[`${b}${h}`], u;
  }) : a === c.PieFromFields && (f = H(l, f).map(([s, d]) => {
    const u = {};
    return d !== void 0 && s !== void 0 && (u.Field = d[o], u.Value = d[y], u[F(h.trim())] = d[`${y}${h}`]), u;
  })), f;
}
function L(n) {
  const { rawData: e, keyBase: t = "", outRow: r } = n;
  for (const [i, o] of Object.entries(B)) {
    const a = e[t + o];
    a !== void 0 && (r[i] = A(a) ? a.toFixed(2) : a);
    const f = e[t + o + ee];
    f !== void 0 && (r[`${i} Outlier`] = A(f) ? f.toFixed(2) : f);
    const l = e[t + o + K];
    l !== void 0 && (r[`${i} Z-Score`] = A(l) ? l.toFixed(2) : l);
  }
}
function M(n) {
  const { rawData: e, outRow: t, config: r, boxPlotSubType: i, fieldList: o } = n, a = N(r), f = Z(r), l = I(r);
  let m = B.Outlier;
  const x = w;
  switch (i) {
    case c.BoxPlotMonoFieldAndCategory: {
      const s = $({
        field: a,
        value: e[a],
        fieldList: o
      });
      t[a] = s, t[F(B.Outlier)] = e[m];
      break;
    }
    case c.BoxPlotMultiFieldsAndCategory: {
      t[F(a)] = e[a], m = `${f.find(
        (d) => Object.keys(e).some((u) => u.toLowerCase().includes(d.toLowerCase()))
      ) ?? ""}_${m}`, t[F(B.Outlier)] = e[m];
      break;
    }
    case c.BoxPlotMonoFieldAndSplitBy:
    case c.BoxPlotMultiFieldsAndSplitBy: {
      const s = e[l];
      m = `${s}_${m}`, t.Field = e[x], t[F(l)] = s, t[F(B.Outlier)] = e[m];
      break;
    }
    case c.BoxPlotMonoFieldAndCategoryAndSplitBy: {
      const s = e[l];
      m = `${s}_${m}`, t[F(a)] = e[a], t[F(l)] = s, t[F(B.Outlier)] = e[m];
      break;
    }
    case c.BoxPlotMonoField:
    case c.BoxPlotMultiFields:
    default: {
      t.Field = e[x], t[F(B.Outlier)] = e[m];
      break;
    }
  }
  const p = `${m}${K}`, y = e[p];
  y !== void 0 && (t[`${F(B.Outlier)} Z-Score`] = A(y) ? y.toFixed(2) : y);
}
function be(n) {
  const { rawData: e, outRow: t, config: r, splitByValue: i } = n, o = T(r.series), a = e[`${i}_${B.Average}`], f = I(r), l = N(r);
  let m = "";
  switch (o) {
    case c.BoxPlotMonoFieldAndSplitBy: {
      m = `${i}_${f}`, t.Field = e[w], t[F(f)] = e[m];
      break;
    }
    case c.BoxPlotMultiFieldsAndSplitBy: {
      m = `${i}_${B.Average}`, t.Field = e[w], t[F(f)] = e[m];
      break;
    }
    case c.BoxPlotMonoFieldAndCategoryAndSplitBy: {
      m = `${i}_${B.Average}`, t[l] = e[l], t[F(f)] = i;
      break;
    }
  }
  t[F(B.Average)] = A(a) ? a.toFixed(2) : a;
  const x = `${a}${K}`, p = e[x];
  p !== void 0 && (t[`${F(B.Average)} Z-Score`] = A(p) ? p.toFixed(2) : p);
}
function Be(n) {
  const { config: e, rows: t, fieldList: r } = n, i = T(e.series);
  let o = i;
  const a = oe(e), f = Array.isArray(t) && t.length > 0 && t.every((s) => s[C.boxPlot.boxPlotMeanLineMarkerId] !== void 0);
  a && !f && (i === c.BoxPlotMonoFieldAndSplitBy ? o = c.BoxPlotMonoField : i === c.BoxPlotMultiFieldsAndSplitBy ? o = c.BoxPlotMultiFields : i === c.BoxPlotMonoFieldAndCategoryAndSplitBy && (o = c.BoxPlotMonoFieldAndCategory));
  const l = N(e), m = Z(e), x = q(e), p = I(e);
  let y;
  switch (o) {
    case c.BoxPlotMonoField:
    case c.BoxPlotMonoFieldAndCategory: {
      y = t.map((s) => {
        const d = {};
        if (s[C.boxPlot.boxPlotOutlierId])
          M({ rawData: s, outRow: d, config: e, boxPlotSubType: o });
        else {
          if (o === c.BoxPlotMonoFieldAndCategory) {
            const u = $({
              field: l,
              value: s[l],
              fieldList: r
            });
            d[l] = u;
          } else o === c.BoxPlotMonoField && (d.Field = s[w]);
          L({ rawData: s, outRow: d });
        }
        return d;
      });
      break;
    }
    case c.BoxPlotMultiFields: {
      y = t.flatMap((s) => {
        const d = {};
        return s[C.boxPlot.boxPlotOutlierId] ? M({
          rawData: s,
          outRow: d,
          config: e,
          boxPlotSubType: o
        }) : (d.Field = s[w], L({ rawData: s, outRow: d })), [d];
      });
      break;
    }
    case c.BoxPlotMultiFieldsAndCategory: {
      y = t.flatMap(
        (s) => m.map((d) => {
          const u = {};
          let g = "";
          if (u.Field = d, s[C.boxPlot.boxPlotOutlierId])
            M({
              rawData: s,
              outRow: u,
              config: e,
              boxPlotSubType: o
            });
          else {
            if (o === c.BoxPlotMultiFieldsAndCategory) {
              const b = $({
                field: l,
                value: s[l],
                fieldList: r
              });
              u[l] = b, g = `${d}_`;
            }
            L({ rawData: s, outRow: u, keyBase: g });
          }
          return u;
        })
      );
      break;
    }
    case c.BoxPlotMonoFieldAndSplitBy:
    case c.BoxPlotMultiFieldsAndSplitBy:
    case c.BoxPlotMonoFieldAndCategoryAndSplitBy: {
      y = t.flatMap((s) => {
        if (s[C.boxPlot.boxPlotOutlierId]) {
          const d = {};
          return M({
            rawData: s,
            outRow: d,
            config: e,
            boxPlotSubType: o
          }), [d];
        } else return s[C.boxPlot.boxPlotMeanLineMarkerId] ? x?.map((d) => {
          const u = {};
          return be({
            rawData: s,
            outRow: u,
            splitByValue: d,
            config: e
          }), u;
        }) : x.map((d) => {
          const u = {};
          if (o === c.BoxPlotMonoFieldAndCategoryAndSplitBy) {
            const g = $({
              field: l,
              value: s[l],
              fieldList: r
            });
            u[l] = g;
          }
          return s[C.boxPlot.boxPlotOutlierId] ? M({
            rawData: s,
            outRow: u,
            config: e,
            boxPlotSubType: o
          }) : (u[p] = d, L({ rawData: s, outRow: u, keyBase: `${d}_` })), u;
        });
      });
      break;
    }
    default:
      y = t;
  }
  return y;
}
const G = (n) => {
  let e = !1;
  return (typeof n == "string" || typeof n == "number") && (e = !Number.isNaN(new Date(n).getTime())), e;
}, $ = (n) => {
  const { field: e, value: t, fieldList: r } = n;
  let i = t;
  const o = r?.find((a) => a.name === e);
  if (o) {
    const a = o.type === te.DateOnly;
    J(o) && !a ? i = G(t) ? new Date(t).toISOString() : t : a && typeof t != "string" && (i = G(t) ? new Date(t).toISOString().split("T")[0] : t);
  }
  return i;
};
function je() {
  const n = () => ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
  return `${n() + n()}-${n()}-${n()}-${n()}-${n()}${n()}${n()}`;
}
export {
  Re as D,
  de as a,
  Ne as b,
  je as g,
  Ke as h,
  ke as m,
  Ve as s,
  Ie as t
};
