/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { capitalize as y } from "lodash-es";
import "@arcgis/toolkit/type";
import { g as l, t as n, w as F, W as d, a as f } from "./series-types.js";
import { c0 as x, b as $, d as L, ay as N, dr as I, o as H, bR as _, cM as M, dp as P, c as W, a as v } from "./index.js";
import "@arcgis/toolkit/intl";
import "luxon";
import "d3-array";
import "@esri/arcgis-html-sanitizer";
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 "@arcgis/core/request.js";
import "./runtime.js";
import "@arcgis/toolkit/dom";
import "@arcgis/core/intl.js";
import { H as u, S as o, a as U } from "./common.js";
function E(e, r) {
  let t;
  return r === n.Category && Array.isArray(e) ? t = o.customSort : r === n.Category ? t = e === l.Ascending ? o.xAxisAsc : o.xAxisDesc : e === l.Ascending ? t = o.yAxisAsc : t = o.yAxisDesc, t;
}
function ne(e, r) {
  let t;
  return r === n.Category && Array.isArray(e) ? t = u.customSort : t = e === l.Ascending ? u.Asc : u.Desc, t;
}
const K = (e) => {
  const r = e.orderOptions?.data && "orderBy" in e.orderOptions.data ? e.orderOptions.data.orderBy : l.Ascending;
  return E(
    r,
    e.orderOptions?.data?.orderType ?? n.Category
  );
}, V = (e) => {
  const r = F(e.series);
  let t = !1;
  return (r === d.BarSeries || r === d.LineSeries) && (t = K(e) === o.customSort), t;
}, le = (e) => {
  let r = [];
  const t = e.orderOptions?.data?.orderType;
  return V(e) && t === n.Category && (r = e.orderOptions?.data?.orderBy), r;
}, ce = (e, r, t, s) => {
  const i = F(r.series), m = I(r);
  r.orderOptions ??= {};
  let a = m ? [""] : l.Ascending;
  if (m)
    e === u.customSort && t !== void 0 ? a = [...t] : a = e === u.Desc ? l.Descending : l.Ascending, s ? r.orderOptions.data = {
      ...r.orderOptions.data,
      orderByY: a,
      orderType: n.MultiAxes
    } : r.orderOptions.data = {
      ...r.orderOptions.data,
      orderByX: a,
      orderType: n.MultiAxes
    };
  else if (i === d.BarSeries || i === d.LineSeries) {
    let c = n.Category;
    e === o.customSort && t !== void 0 ? (a = [...t], r.orderOptions.data = {
      orderType: c,
      orderBy: a
    }) : (c = e === o.xAxisAsc || e === o.xAxisDesc ? n.Category : n.YValue, a = e === o.xAxisAsc || e === o.yAxisAsc ? l.Ascending : l.Descending, r.orderOptions.data = {
      orderType: c,
      orderBy: a
    });
  }
}, pe = (e) => e?.series?.[0]?.x ?? "", me = (e) => {
  const {
    config: r,
    xAxisField: t,
    numericFields: s,
    aggregationType: i,
    mode: m,
    layer: a,
    numericFieldIndex: c,
    seriesIndex: h,
    splitByField: S = "",
    fieldList: O
  } = e, T = F(r.series), b = H(r.series), { idField: D, isTraditionalTypeOID: B } = _(M(a)), C = B && s.length === 1 && s[0] === D;
  let p = "y", A = "y";
  switch (T) {
    case d.PieSeries: {
      i !== f.NoAggregation ? m === U.Category ? (p = `${i}_${s[0]}_0`.toUpperCase(), A = C ? y(f.Count) : y(`${i} of ${s[0]}`)) : p = v : p = s[0];
      break;
    }
    case d.BarSeries:
    case d.LineSeries:
    case d.ComboLineAndBarSeries:
    case d.RadarSeries:
    case d.HeatSeries: {
      if (S === "" || s.length > 1)
        j({
          fieldName: t,
          numericFields: s,
          fieldList: O ?? []
        }) || b === P.BarAndLineFromFields ? p = W : (p = `${i}_${s[c ?? 0]}_${c ?? 0}`.toUpperCase(), A = C ? y(f.Count) : y(`${i} of ${s[c ?? 0]}`));
      else {
        const g = s[0];
        p = `${i}_${g}_${h ?? 0}`.toUpperCase(), A = C ? y(f.Count) : y(`${i} of ${g}`);
      }
      break;
    }
  }
  return {
    name: p,
    description: A
  };
};
function Y(e) {
  const { fieldName: r, fieldList: t } = e, s = x(t ?? [], r);
  return r === $ || r === L || r === N || r === void 0 || r.trim() === "" || s === void 0;
}
function j(e) {
  const { fieldName: r, fieldList: t, numericFields: s } = e, i = s.filter((m) => x(t ?? [], m)?.type !== "esriFieldTypeOID");
  return Y({ fieldName: r, fieldList: t }) && (i?.length ?? 0) > 0;
}
export {
  K as a,
  pe as b,
  j as c,
  le as d,
  ne as e,
  E as f,
  me as g,
  ce as s,
  Y as x
};
