/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import "@arcgis/toolkit/intl";
import { memoize as X, isEmpty as ye, isEqual as me, cloneDeep as R } from "lodash-es";
import "luxon";
import { a$ as q, h as O, ay as C, _ as D, l as ue, ab as fe, bV as Se, d3 as w, bX as j, bY as J, b0 as _, aQ as Q, aw as G, be as H, bF as K, ba as pe, aS as ge, aR as xe } from "./index.js";
import "@arcgis/core/geometry/support/jsonUtils.js";
import "@arcgis/core/rest/support/AttributeBinsQuery.js";
import "@arcgis/core/rest/support/StatisticDefinition.js";
import "@arcgis/core/time/TimeExtent.js";
import "@arcgis/toolkit/type";
import { W as u, t as p, g as h } from "./series-types.js";
import { R as be } from "./rest-js-object-literals.js";
import "@arcgis/core/core/promiseUtils.js";
import "@arcgis/core/request.js";
import "./runtime.js";
import { g as P, r as he } from "./index4.js";
import "@arcgis/toolkit/dom";
import { u as V, d as Z, e as Fe, f as Ae, s as U, h as Be } from "./utils.js";
import { substitute as M } from "@arcgis/core/intl.js";
import { B as m } from "./common.js";
import "d3-array";
import "@arcgis/core/Graphic.js";
import "@arcgis/core/renderers/support/jsonUtils.js";
import "@arcgis/core/symbols/support/symbolUtils.js";
import "@esri/arcgis-html-sanitizer";
import "@arcgis/core/layers/support/FeatureFilter.js";
import "@arcgis/core/smartMapping/statistics/histogram.js";
import "@arcgis/core/smartMapping/statistics/summaryStatistics.js";
import "simple-statistics";
import "@arcgis/core/core/sql.js";
import "@arcgis/core/AttributeBinsGraphic.js";
import { g as Ce } from "./series-length.js";
import { c as z } from "./data.js";
import { g as W } from "./utils2.js";
const hs = (s, e) => e?.series?.[s]?.name, Fs = (s, e, t) => {
  const o = t;
  o?.series?.[e] !== void 0 && (o.series[e].name = s);
}, As = (s, e) => {
  let t;
  const o = e.series?.[s];
  return o?.type === u.BoxPlotSeries ? t = o.fillSymbol?.color : o?.type === u.LineSeries && (t = o.lineSymbol?.color), t;
}, we = (s) => {
  const e = s.series[0];
  return s.showMean === !0 && P(e) ? e.fillSymbol?.color : void 0;
}, Le = (s, e) => {
  const t = e.series[0];
  e.showMean === !0 && P(t) && (t.fillSymbol !== void 0 ? t.fillSymbol.color = s : t.fillSymbol = {
    ...q(),
    color: s
  });
}, Bs = (s, e, t) => {
  const i = t?.series?.[e];
  i?.type === u.BoxPlotSeries && i.fillSymbol !== void 0 ? i.fillSymbol.color = s : i?.type === u.LineSeries && i.lineSymbol !== void 0 && (i.lineSymbol.color = s, i.markerSymbol !== void 0 && (i.markerSymbol.color = s));
}, Te = (s) => s.series.map((e) => e.id), ve = (s, e) => {
  e?.series?.sort(
    (t, o) => s.findIndex((i) => t.id === i) - s.findIndex((i) => o.id === i)
  );
}, Cs = (s, e, t) => {
  const o = Te(t), [i] = o.splice(s, 1);
  o.splice(e, 0, i), ve(o, t);
}, ws = (s) => N(s) ? s.series.findIndex((e) => e.type === u.BoxPlotSeries) : -1;
function Me(s, e, t) {
  const o = le(s, e, t), i = Ce(s), r = D(s);
  return o === i || r;
}
function ee(s, e, t) {
  const o = T(s, e, t), i = W(1, s), r = D(s);
  return o === i || r;
}
function Ie(s) {
  const { config: e, layerInfo: t, newCategory: o } = s, { layerFieldsInfo: i } = t, r = L(e), l = o !== C ? w(i, r) : "", n = W(0, e), a = D(e);
  return l === n || a || n.trim() === "";
}
const L = (s) => O(s)?.x ?? C, Oe = X(
  (s) => {
    const e = O(s), t = e?.query?.where?.split("=")[0].trim() ?? "";
    return ue(e?.query?.where) ? "" : t;
  },
  (s) => O(s)?.query?.where
), se = X(
  // without memoizing, the worst case efficiency is O(series.length), best case is O(1)
  // with memoizing, the worst case efficiency for repeated calls becomes O(1)
  (s) => fe(s?.series).filter((e) => e !== ""),
  // using series array address as key, since a change to the numeric fields will always rebuild a fresh series array
  (s) => s?.series
), I = (s) => {
  const { category: e, seriesIndex: t, y: o, commonStrings: i } = s, l = {
    ...H(i, t),
    x: e === void 0 || e === "" ? C : e,
    y: o,
    query: {}
  };
  return R(l);
}, Pe = (s) => {
  const { category: e, seriesIndex: t, y: o, commonStrings: i } = s, l = {
    ...pe({ commonStrings: i, seriesIndex: t }),
    x: e === void 0 || e === "" ? C : e,
    y: o,
    query: {}
  };
  return R(l);
};
function te(s) {
  const { x: e, y: t, seriesTemplate: o, commonStrings: i } = s, { id: r, fillSymbol: l } = o ?? {}, n = H(i), a = r ?? `series_${Date.now()}`;
  return n.id = a, n.name = a, n.x = e, n.y = t, l ? n.fillSymbol = l : n.fillSymbol && (n.fillSymbol.color = K, n.fillSymbol?.outline && (n.fillSymbol.outline.color = K)), n;
}
function ie(s) {
  const { numericFields: e, config: t, layerInfo: o, category: i, commonStrings: r } = s, { layerFieldsInfo: l } = o, n = [], a = i ?? L(t);
  let y = !1;
  const d = z({
    fieldName: a,
    numericFields: e,
    fieldList: l
  });
  if (e.length === 0) {
    const f = I({
      y: "",
      seriesIndex: 0,
      category: a,
      commonStrings: r
    });
    n.push(f);
  } else if (d) {
    const f = e.length > 1 ? e : e[0], F = I({
      y: f,
      seriesIndex: 0,
      category: a,
      commonStrings: r
    }), x = e.length > 1 ? r.axisLabels.values : w(o.layerFieldsInfo, e[0]), g = {
      ...F,
      id: e[0],
      name: x,
      x: d ? C : a
    }, S = t.series.find((c) => me(f, c.y) && c.query?.where === void 0);
    S !== void 0 && (!t.colorMatch && "fillSymbol" in S && (g.fillSymbol = S.fillSymbol, y = !0), g.id = S.id, g.name = S.name), n.push(g);
  } else
    for (let f = 0; f < e.length; f += 1) {
      const F = e[f], x = I({
        y: F,
        seriesIndex: f,
        category: a,
        commonStrings: r
      }), g = w(l, F), S = {
        ...x,
        id: F,
        name: g,
        x: a
      }, c = t.series.find((B) => F === B.y);
      c !== void 0 && (!t.colorMatch && "fillSymbol" in c && (S.fillSymbol = c.fillSymbol, y = !0), S.id = c.id, S.name = c.name), n.push(S);
    }
  return t.series.length === 1 && n.length === 1 && !t.colorMatch && "fillSymbol" in t.series[0] && (n[0].fillSymbol = t.series[0].fillSymbol, y = !0), t.colorMatch = !y, n;
}
const oe = (s) => s?.query?.where?.split("=")[1].trim().replace(/'/gu, "") ?? "", Ve = (s) => s.series.some((e) => e.type === u.LineSeries) && s.series.some((e) => e.type === u.BoxPlotSeries);
function De(s, e, t) {
  const o = [];
  return s.forEach((i) => {
    if (i.type === u.BoxPlotSeries) {
      const { type: r, fillSymbol: l, ...n } = i;
      o.push({
        ...n,
        type: u.LineSeries,
        lineSymbol: {
          ...Q({ width: G }),
          ...l !== void 0 ? { color: l.color } : {}
        },
        markerSymbol: {
          ..._(),
          ...l !== void 0 ? { color: l.color } : {}
        }
      });
    }
  }), o.unshift(
    te({
      x: s[0].x,
      y: s[0].y,
      seriesTemplate: t,
      commonStrings: e
    })
  ), o;
}
function ke(s) {
  const e = s.slice(1), t = [];
  return e.forEach((o) => {
    if (o.type === u.LineSeries) {
      const { type: i, lineSymbol: r, markerSymbol: l, ...n } = o, a = q();
      t.push({
        ...n,
        type: u.BoxPlotSeries,
        fillSymbol: {
          ...a,
          color: r?.color
        }
      });
    }
  }), t;
}
function re(s) {
  const { formerSeries: e, newSeries: t, showMean: o } = s;
  let i = t;
  const r = (a) => a.type === (o ? u.LineSeries : u.BoxPlotSeries), l = e.filter(r).map((a) => a.id), n = t.filter(r).map((a) => a.id);
  return l.length === n.length && JSON.stringify(l) !== JSON.stringify(n) && (i = i.sort(
    (a, y) => l.findIndex((d) => d === a.id) - l.findIndex((d) => d === y.id)
  )), i;
}
async function Ne(s) {
  const { config: e, layerInfo: t, commonStrings: o, splitByField: i, numericFields: r, category: l } = s, { layer: n, layerFieldsInfo: a } = t, { fetchNullValues: y } = O(e)?.query ?? {}, d = l ?? L(e), { categoriesArray: f, splitByFieldType: F } = await Be({
    config: e,
    layer: n,
    layerFieldsInfo: a,
    xAxisField: Y(d) ? void 0 : d,
    aggregationFields: r,
    splitByField: i,
    fetchNullValues: y
  });
  let x = [], g = !1;
  const S = Ve(e);
  if (f.length === 0) {
    x = ie({
      numericFields: r,
      category: d,
      config: e,
      layerInfo: t,
      commonStrings: o
    });
    for (const c of x)
      c.query && (c.query.where = `${i}=''`);
  } else
    for (let c = 0; c < f.length; c += 1) {
      const B = f[c], v = {
        y: r,
        seriesIndex: c,
        category: d,
        commonStrings: o
      }, E = S ? Pe(v) : I(v), ae = {
        fieldName: i,
        fieldValueToMatch: B ?? "",
        subtypeInfo: j(t.layer),
        fieldList: t.layerFieldsInfo
      }, de = J(ae), k = `${B}`, A = {
        ...E,
        id: k,
        name: de ?? k,
        x: d === void 0 || d === "" ? C : d,
        query: {
          ...E.query,
          where: `${i}=${F === be.String && B !== null ? `'${B}'` : B}`
        }
      }, b = e.series.find((ce) => oe(ce) === k);
      b !== void 0 && (e.colorMatch || (A.type === u.BoxPlotSeries ? P(b) ? A.fillSymbol = b.fillSymbol : A.fillSymbol = {
        ...q(),
        color: b.lineSymbol?.color
      } : P(b) ? (A.lineSymbol = {
        ...Q({ width: G }),
        ...b.fillSymbol !== void 0 ? { color: b.fillSymbol.color } : {}
      }, A.markerSymbol = {
        ..._(),
        ...b.fillSymbol !== void 0 ? { color: b.fillSymbol.color } : {}
      }) : (A.lineSymbol = b.lineSymbol, A.markerSymbol = b.markerSymbol), g = !0), A.id = b.id, A.name = b.name), x.push(A);
    }
  if (x = re({
    formerSeries: e.series,
    newSeries: x,
    showMean: N(e)
  }), e.colorMatch = !g, N(e)) {
    const c = e.series.find((v) => v.type === u.BoxPlotSeries), B = te({
      x: d,
      y: r,
      seriesTemplate: c,
      commonStrings: o
    });
    x.unshift(B);
  }
  return x;
}
const Ls = async (s) => {
  const { splitByField: e, numericFields: t, category: o, config: i, layerInfo: r, commonStrings: l } = s, { layerFieldsInfo: n } = r, a = We(o, t, e), y = Se(n), d = L(i), f = Me(i, r, l), F = ee(i, r, l), x = Ie({ config: i, layerInfo: r, newCategory: o });
  let g = Z(i);
  if (!y.includes(d) && y.includes(o) ? g = ge() : y.includes(d) && !y.includes(o) && (g = xe()), g !== void 0 && Ae(i, l, g), $(a ? { newShowMeanLines: i.showMean, config: i, commonStrings: l } : { newShowMeanLines: !1, config: i, commonStrings: l }), i.series = a ? await Ne({
    config: i,
    layerInfo: r,
    splitByField: e,
    numericFields: t,
    category: o,
    commonStrings: l
  }) : ie({
    numericFields: t,
    config: i,
    layerInfo: r,
    category: o,
    commonStrings: l
  }), x) {
    const c = z({
      fieldName: o,
      numericFields: t,
      fieldList: n
    }) ? "" : w(n, o);
    V(i, 0, c);
  }
  if (F) {
    const S = T(i, r, l);
    V(i, 1, S);
  }
  i?.title !== void 0 && f && (i.title.content.text = le(i, r, l)), U({ config: i, value: null, axisIndex: 1, isMax: !1 }), U({ config: i, value: null, axisIndex: 1, isMax: !0 });
}, qe = (s) => s?.standardizeValues ?? !1;
function T(s, e, t) {
  const { layerFieldsInfo: o } = e;
  let r = qe(s) ? t.axisLabels.standardizedValues : t.axisLabels.values, l = s?.series?.map((n) => n.y).flat();
  return l = [...new Set(l)], l.length === 1 && (r = w(o, l[0])), r;
}
function le(s, e, t) {
  const { layerFieldsInfo: o } = e, i = L(s), r = se(s), l = Oe(s), n = r.map((d) => w(o ?? [], d)).join(", "), a = w(o ?? [], i);
  let y = "";
  return r.length > 0 && (i !== C && l !== "" ? y = M(t.titleLabels.distributionOfFieldsByCategoryAndSplitBy, {
    valueFields: n,
    category: a,
    splitByField: l
  }) : i !== C ? y = M(t.titleLabels.distributionOfFieldsByCategory, {
    valueFields: n,
    category: a
  }) : l !== "" ? y = M(t.titleLabels.distributionOfFieldsByCategory, {
    valueFields: n,
    category: l
  }) : y = M(t.titleLabels.distributionOfField, {
    valueField: n
  })), y;
}
const Ts = (s) => {
  const { series: e, splitByField: t, layerInfo: o, commonStrings: i } = s;
  let r = typeof e.y == "string" ? w(o.layerFieldsInfo, e.y) : i.axisLabels.values;
  ye(t) || (r = oe(e));
  const l = {
    fieldName: t,
    fieldValueToMatch: r ?? "",
    subtypeInfo: j(o.layer),
    fieldList: o.layerFieldsInfo
  };
  return r = J(l) ?? r, r ?? e.id;
}, vs = (s) => s?.showOutliers ?? !1, Ms = (s) => {
  const { newShowOutliers: e, config: t, layerInfo: o, commonStrings: i } = s;
  if (t.showOutliers = e, ee(t, o, i)) {
    const l = T(t, o, i);
    V(t, 1, l);
  }
}, Is = (s) => {
  const { newStandardizeValues: e, config: t, layerInfo: o, commonStrings: i } = s, r = W(1, t), l = T(t, o, i), n = D(t);
  if (t.standardizeValues = e, r === l || n) {
    const a = T(t, o, i);
    V(t, 1, a);
  }
}, ne = (s) => {
  let e = m.xAxisAsc;
  const t = s.orderOptions?.data?.orderType ?? p.Category, o = s.orderOptions?.data && "orderBy" in s.orderOptions.data ? s.orderOptions.data.orderBy : h.Ascending;
  switch (t) {
    case p.Category:
      e = Array.isArray(o) ? m.customSort : o === h.Ascending ? m.xAxisAsc : m.xAxisDesc;
      break;
    case p.Mean:
      e = o === h.Ascending ? m.meanAsc : m.meanDesc;
      break;
    case p.Median:
      e = o === h.Ascending ? m.medianAsc : m.medianDesc;
      break;
  }
  return e;
}, Os = (s, e, t) => {
  let o = p.Category, i = h.Ascending;
  if (e.orderOptions = e.orderOptions ?? {}, s === m.customSort && t !== void 0)
    e.orderOptions.data = {
      orderType: p.Category,
      orderBy: t
    };
  else {
    switch (s) {
      case m.xAxisAsc: {
        o = p.Category, i = h.Ascending;
        break;
      }
      case m.xAxisDesc: {
        o = p.Category, i = h.Descending;
        break;
      }
      case m.meanAsc: {
        o = p.Mean, i = h.Ascending;
        break;
      }
      case m.meanDesc: {
        o = p.Mean, i = h.Descending;
        break;
      }
      case m.medianAsc: {
        o = p.Median, i = h.Ascending;
        break;
      }
      case m.medianDesc: {
        o = p.Median, i = h.Descending;
        break;
      }
    }
    e.orderOptions.data = {
      orderType: o,
      orderBy: i
    };
  }
}, N = (s) => s.showMean ?? !1, $ = (s) => {
  const { newShowMeanLines: e = !1, config: t, commonStrings: o, previousMeanLinesBoxColor: i } = s, r = e ? i : we(t), l = (e ?? !1) !== (t.showMean ?? !1);
  if (t.showMean = e, l) {
    let n;
    e ? n = he(t.series) ? De(t.series, o) : t.series : n = ke(t.series), n && (n = re({
      formerSeries: t.series,
      newSeries: n,
      showMean: e
    }), t.series = n);
  }
  return e && i !== void 0 && Le(i, t), { previousMeanLinesBoxColor: r };
}, Ps = (s) => {
  let e = [];
  const t = s.orderOptions?.data?.orderType;
  return ne(s) === m.customSort && t === p.Category && (e = s.orderOptions?.data?.orderBy), e;
}, Vs = (s, e) => {
  e.orderOptions ??= {};
  const t = e.orderOptions?.data?.orderType;
  ne(e) === m.customSort && t === p.Category && (e.orderOptions.data = {
    orderType: p.Category,
    orderBy: s
  });
}, Ds = async (s, e) => {
  const t = L(s), o = Z(s)?.type, i = se(s);
  let r = [];
  return o === u.CategoryAxisFormat && (z({
    fieldName: t,
    numericFields: i,
    fieldList: e.layerFieldsInfo
  }) ? r = i : Y(t) || (r = await Fe({
    layerInfo: e,
    config: s,
    category: t
  }))), r;
}, Y = (s) => s.trim() === "" || s === C, ze = (s, e) => e.length > 1 && !Y(s), We = (s, e, t) => !ze(s, e) && t !== "";
export {
  ws as A,
  le as B,
  T as C,
  Ts as D,
  Ms as a,
  qe as b,
  Is as c,
  ne as d,
  Os as e,
  Ds as f,
  vs as g,
  Ps as h,
  Vs as i,
  N as j,
  $ as k,
  we as l,
  Le as m,
  L as n,
  Oe as o,
  se as p,
  We as q,
  Ls as r,
  ze as s,
  hs as t,
  Fs as u,
  As as v,
  Bs as w,
  Te as x,
  ve as y,
  Cs as z
};
