/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import "@arcgis/toolkit/intl";
import { memoize as E, isEmpty as R, isNull as $, cloneDeep as U } from "lodash-es";
import "luxon";
import "d3-array";
import "@arcgis/toolkit/type";
import { a as m, g as b, t as F, W as _ } from "./series-types.js";
import { f as z } from "./rest-js-object-literals.js";
import { d as O, bR as A, aX as G, by as M, d3 as P, _ as V, bh as X, a as w } from "./index.js";
import "@arcgis/core/Graphic.js";
import "@arcgis/core/renderers/support/jsonUtils.js";
import "@arcgis/core/symbols/support/symbolUtils.js";
import { substitute as h } from "@arcgis/core/intl.js";
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/request.js";
import "@arcgis/core/core/sql.js";
import "@arcgis/core/AttributeBinsGraphic.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/core/core/promiseUtils.js";
import "./runtime.js";
import "@arcgis/toolkit/dom";
import { g as L, u as q } from "./utils.js";
import { a as y, P as g } from "./common.js";
import { g as k } from "./data.js";
import { g as H } from "./series-length.js";
import { g as J } from "./utils2.js";
function v(s) {
  const { config: t, layerInfo: e, mode: i, commonStrings: r } = s, a = e !== void 0 ? D({ config: t, layerInfo: e, commonStrings: r, mode: i }) : "", l = H(t), d = V(t);
  return a === l || d;
}
function B(s) {
  const { config: t, layerInfo: e, mode: i, commonStrings: r } = s, a = e !== void 0 ? L({
    config: t,
    layerInfo: e,
    commonStrings: r,
    numericFields: f(t),
    aggregationType: T(t, i)
  }) : "", l = J(1, t), d = V(t);
  return a === l || d;
}
const I = (s) => s.series?.[0]?.x ?? "";
function Q(s, t) {
  const e = s.series?.[0];
  return [`${t === y.Category ? e.y : w} ${b.Descending}`];
}
const K = (s, t) => {
  let e = g.valueDesc;
  const i = s.orderOptions?.data?.orderType ?? Q(s, t), r = s.orderOptions?.data && "orderBy" in s.orderOptions.data ? s.orderOptions.data.orderBy : b.Descending;
  return i === F.Category && Array.isArray(r) ? e = g.customSort : i === F.Category ? r === b.Ascending ? e = g.labelAsc : e = g.labelDesc : r === b.Ascending ? e = g.valueAsc : e = g.valueDesc, e;
}, f = E(
  // 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) => {
    let t = [];
    return t = s.series?.[0]?.query?.outStatistics?.map(
      (e) => e.onStatisticField
    ) ?? [s.series?.[0]?.y ?? ""], [...new Set(t.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
), T = (s, t) => {
  const e = f(s);
  let i;
  return t === y.Fields && e.length === 0 ? i = m.Sum : i = s.series?.[0]?.query?.outStatistics?.[0]?.statisticType ?? m.NoAggregation, i;
};
function Y(s) {
  const { config: t, layerInfo: e, mode: i, commonStrings: r } = s, a = A(e?.layer).idField, l = X(r, a), d = I(t), c = t.series[0]?.dataLabels, o = {
    ...l,
    x: i === y.Category ? d : O,
    slices: t.series?.[0]?.slices,
    dataLabels: {
      ...l.dataLabels,
      type: l.dataLabels?.type ?? _.Text,
      content: { ...l.dataLabels?.content, ...c?.content, type: z.TS },
      visible: c?.visible ?? !1
    },
    innerRadiusSize: t.series?.[0]?.innerRadius,
    sliceGrouping: t.series?.[0]?.sliceGrouping,
    dataLabelsCharacterLimit: t.series?.[0]?.dataLabelsCharacterLimit,
    numericValueFormat: t.series?.[0]?.numericValueFormat,
    percentValueFormat: t.series?.[0]?.percentValueFormat,
    displayCategoryOnDataLabel: t.series?.[0]?.displayCategoryOnDataLabel,
    displayNumericValueOnDataLabel: t.series?.[0]?.displayNumericValueOnDataLabel,
    displayPercentageOnDataLabel: t.series?.[0]?.displayPercentageOnDataLabel
  }, n = T(t, i);
  if (n !== m.NoAggregation && a !== void 0) {
    const u = `${n}_${a}_0`.toUpperCase();
    o.y = i === y.Category ? u : w, o.query = {
      outStatistics: i === y.Category ? [
        {
          statisticType: n,
          onStatisticField: a,
          outStatisticFieldName: u
        }
      ] : []
    };
  }
  return i === y.Category && o.query !== void 0 ? o.query.groupByFieldsForStatistics = [d] : delete o.query?.groupByFieldsForStatistics, U(o);
}
function D(s) {
  const { config: t, layerInfo: e, mode: i, commonStrings: r } = s, { layerFieldsInfo: a } = e, { idField: l, isTraditionalTypeOID: d } = A(e.layer), c = I(t), o = f(t), n = T(t, i), u = d && (o.length === 0 || o.includes(l)), p = M(n, r), x = u ? "" : o.map((W) => P(a ?? [], W)).join(", "), S = P(a ?? [], c);
  let C = "", N;
  return i === y.Fields ? N = o.length === 0 : N = c === "", N ? C = r.utilsStrings.chartType.pieChart : n === m.NoAggregation ? C = h(r.titleLabels.aggrFieldsByCategory, {
    aggregationFields: u ? l : x,
    category: S
  }) : n === m.Count ? C = h(r.titleLabels.aggrTypeCategory, {
    aggregationType: p,
    category: S
  }) : o.length > 0 && (S === O || R(S) || $(S) ? C = h(r.titleLabels.aggrTypeOfAggrFields, {
    aggregationType: p,
    aggregationFields: x
  }) : C = h(r.titleLabels.aggrTypeOfAggrFieldsByCategory, {
    aggregationType: p,
    aggregationFields: x,
    category: S
  })), C;
}
const Be = (s) => {
  const { newSortOrderKind: t, config: e, orderByList: i } = s;
  e.orderOptions ??= {}, t === g.customSort && i !== void 0 ? e.orderOptions.data = {
    orderType: F.Category,
    orderBy: [...i]
  } : e.orderOptions.data = {
    orderType: t === g.labelAsc || t === g.labelDesc ? F.Category : F.YValue,
    orderBy: t === g.labelAsc || t === g.valueAsc ? b.Ascending : b.Descending
  };
}, Ie = (s) => {
  const { newCategory: t, config: e, layerInfo: i, mode: r, commonStrings: a } = s, l = e.series?.length ?? 0, d = v({ config: e, layerInfo: i, commonStrings: a, mode: r }), c = B({ config: e, layerInfo: i, commonStrings: a, mode: r });
  if (e.series.forEach((o) => {
    o.x = r === y.Category ? t : O, o.query !== void 0 && t !== O ? o.query.groupByFieldsForStatistics = [t] : delete o.query?.groupByFieldsForStatistics;
  }), l === 0) {
    const o = [Y({ config: e, layerInfo: i, mode: r, commonStrings: a })];
    e.series = o;
  }
  if (i !== void 0) {
    if (c) {
      const n = L({
        config: e,
        layerInfo: i,
        numericFields: f(e),
        aggregationType: T(e, r),
        commonStrings: a
      });
      q(e, 1, n);
    }
    e.title && d && (e.title.content.text = D({ config: e, layerInfo: i, commonStrings: a, mode: r }));
    const o = r === y.Fields ? f(e).filter((n) => n !== A(i.layer).idField) : f(e);
    Z({ newNumericFields: o, config: e, layerInfo: i, mode: r, commonStrings: a });
  }
};
function j(s) {
  const { numericFields: t, aggregationType: e, config: i, layerInfo: r, mode: a, commonStrings: l } = s, { layerFieldsInfo: d } = r, c = [], o = Y({
    config: i,
    layerInfo: r,
    mode: a,
    commonStrings: l
  }), n = {
    ...o,
    query: {
      ...o.query
    }
  };
  t.length <= 1 ? n.query.where = o.query?.where : delete n.query.where;
  const u = k({
    config: i,
    xAxisField: I(i),
    numericFields: t,
    aggregationType: e,
    mode: a,
    layer: r.layer,
    fieldList: d
  }).name;
  if (e !== m.NoAggregation)
    if (a === y.Category) {
      if (t.length > 0) {
        const p = t[0];
        n.query.outStatistics = [
          {
            ...o.query?.outStatistics?.[0] ?? {},
            onStatisticField: p,
            outStatisticFieldName: u,
            statisticType: e
          }
        ];
      }
    } else
      for (const p of t)
        n.query.outStatistics?.push({
          onStatisticField: p,
          outStatisticFieldName: p,
          statisticType: e
        });
  else
    n.query = {};
  n.y = u, c.push(n), i.series = c;
}
const Pe = (s) => {
  const { newAggregationType: t, config: e, layerInfo: i, mode: r, commonStrings: a } = s, l = f(e), d = v({ config: e, layerInfo: i, commonStrings: a, mode: r }), c = B({ config: e, layerInfo: i, commonStrings: a, mode: r });
  if (j({
    config: e,
    layerInfo: i,
    numericFields: l,
    aggregationType: t,
    commonStrings: a,
    mode: r
  }), c) {
    const p = L({
      config: e,
      layerInfo: i,
      commonStrings: a,
      numericFields: f(e),
      aggregationType: T(e, r)
    });
    q(e, 1, p);
  }
  e.title && d && (e.title.content.text = D({ config: e, layerInfo: i, commonStrings: a, mode: r }));
  const o = e.series?.[0]?.displayCategoryOnDataLabel, n = e.series?.[0]?.displayNumericValueOnDataLabel, u = e.series?.[0]?.displayPercentageOnDataLabel;
  e.legend = {
    ...e.legend ?? G(),
    displayNumericValue: n,
    displayPercentage: u,
    displayCategory: o
  };
}, Z = (s) => {
  let { newNumericFields: t } = s;
  const { config: e, layerInfo: i, mode: r, commonStrings: a } = s, l = A(i?.layer).idField;
  let d = T(e, r);
  const c = v({ config: e, layerInfo: i, commonStrings: a, mode: r }), o = B({ config: e, layerInfo: i, commonStrings: a, mode: r });
  if (t = t === "" ? [] : t, t = typeof t == "string" ? [t] : t, l !== void 0 && (t.length === 0 && r === y.Category ? (t.push(l), d = m.Count) : d === m.Count && t.length >= 1 && !t.includes(l) && (d = m.Sum)), i !== void 0) {
    if (j({
      config: e,
      layerInfo: i,
      commonStrings: a,
      numericFields: t,
      aggregationType: d,
      mode: r
    }), o) {
      const n = L({
        config: e,
        layerInfo: i,
        commonStrings: a,
        numericFields: f(e),
        aggregationType: T(e, r)
      });
      q(e, 1, n);
    }
    e.title && c && (e.title.content.text = D({ config: e, layerInfo: i, commonStrings: a, mode: r }));
  } else
    e.series.forEach((n) => {
      delete n.query, n.y = t[0];
    });
}, Ve = (s, t) => {
  let e = [];
  const i = s.orderOptions?.data?.orderType, r = K(s, t), a = s.orderOptions?.data && "orderBy" in s.orderOptions.data ? s.orderOptions.data.orderBy : void 0;
  return r === g.customSort && i === F.Category && Array.isArray(a) && (e = a), e;
}, we = (s) => {
  const { values: t, config: e, mode: i } = s;
  e.orderOptions ??= {};
  const r = e.orderOptions?.data?.orderType;
  K(e, i) === g.customSort && r === F.Category && (e.orderOptions.data = {
    orderType: F.Category,
    orderBy: [...t]
  });
};
export {
  f as a,
  T as b,
  Z as c,
  Pe as d,
  K as e,
  Be as f,
  I as g,
  Ve as h,
  we as i,
  D as j,
  Ie as s
};
