/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { substitute as D } from "@arcgis/core/intl.js";
import { cloneDeep as de, memoize as xe, isEmpty as Be } from "lodash-es";
import "@arcgis/toolkit/type";
import { w as be, a as b, d as Ae, t as v, m as X, g as ve, W as O, n as Ie, i as _ } from "./series-types.js";
import { R as ce, f as Le } from "./rest-js-object-literals.js";
import { V as Ve, cO as qe, o as j, dp as k, j as Ne, cM as ke, bS as Oe, c2 as we, ao as De, q as L, p as Re, dv as Pe, cI as Me, dw as ae, cj as Ee, dx as $e, cf as Xe, k as ye, _ as H, d3 as w, du as Ue, ds as U, bR as K, bX as ge, bY as me, by as We, dr as Z, l as Ye, b as W, bV as je, ba as He, bb as Ke, b5 as Qe, bd as ze, bU as re, aS as _e, aR as le, aT as Ge } from "./index.js";
import "@arcgis/toolkit/intl";
import "luxon";
import "d3-array";
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 { h as pe, l as V, o as Je, p as Ze, j as h, k as C, m as ne } from "./index4.js";
import "@arcgis/toolkit/dom";
import { s as I, u as P, d as fe, e as et, f as tt, g as Se, h as it, j as oe } from "./utils.js";
import { S as G } from "./common.js";
import "@esri/arcgis-html-sanitizer";
import { b as $, c as Fe, g as ee, e as ue, f as st } from "./data.js";
import { b as at, g as rt } from "./series-length.js";
import { g as te } from "./utils2.js";
import "@arcgis/core/Graphic.js";
import "@arcgis/core/renderers/support/jsonUtils.js";
import "@arcgis/core/symbols/support/symbolUtils.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/core/sql.js";
import "@arcgis/core/AttributeBinsGraphic.js";
import { c as lt } from "./index2.js";
function li(t, e) {
  let i;
  return e !== void 0 && (i = { ...e }, i.legend !== void 0 && (i.legend.position = t)), i;
}
function ni(t, e) {
  let i;
  return e !== void 0 && (i = { ...e }, i.legend !== void 0 && (i.legend.visible = t)), i;
}
function oi(t, e) {
  let i;
  return e !== void 0 && (i = { ...e, rotated: t }), i;
}
function nt(t) {
  let e;
  return t !== void 0 && (e = { ...t }, delete e.axes?.[0]?.minimum, delete e.axes?.[0]?.maximum, delete e.axes?.[1]?.minimum, delete e.axes?.[1]?.maximum), e;
}
function ui(t, e) {
  return e != null || pe(t);
}
async function ot(t) {
  const { queryObject: e, chartConfig: i, options: r, splitByField: a } = t;
  let s = [], l;
  const n = de(i), o = n.series[0], d = we({
    queryObject: e,
    chartConfig: n,
    runtimeDataFilters: r?.runtimeDataFilters,
    ignoreViewExtent: r?.ignoreViewExtent
  });
  if (d.outStatistics?.[0]?.outStatisticFieldName && (d.outStatistics[0].outStatisticFieldName = De.outStatistics.outStatisticFieldName), L(o) && Re({
    xAxisValueFormatType: Pe(n),
    setTimeBinningInfoWhenNotProvided: r?.setTimeBinningInfoWhenNotProvided,
    seriesConfig: o
  }) && Me(e)) {
    const { temporalBinning: f, x: c } = o, { uniqueQuery: p } = await ae({
      chartConfig: n,
      queryConfig: d,
      queryObject: e,
      options: r
    });
    l = await Ee({
      queryObject: e,
      dateField: c,
      splitByField: a,
      queryConfig: p,
      temporalBinning: f
    }), s = Array.from(
      new Set(
        l.flatMap((S) => {
          const { stackedAttributes: g } = S;
          let y = [];
          return g && (y = g.map((F) => F[a]).filter((F) => F !== void 0)), y;
        })
      ).values()
    );
  } else {
    const { uniqueQuery: f } = await ae({
      chartConfig: n,
      queryObject: e,
      queryConfig: d,
      options: r
    }), { elementCount: c } = await $e({
      chartConfig: n,
      queryObject: e,
      queryConfig: d,
      behaviorAfterLimit: r?.chartLimits?.behaviorAfterLimit,
      numberMaxOfFeaturesToFetch: f.num
    });
    l = await Xe({
      queryObject: e,
      queryConfig: f,
      featureCount: c
    }), s = Array.from(
      new Set(
        l.map((p) => p.attributes[a]).filter((p) => p !== void 0)
      ).values()
    );
  }
  return { allValues: s, preFetchedQueryResponse: l };
}
function ut(t, e) {
  const i = /* @__PURE__ */ new Map();
  return t.series.forEach((r) => {
    let a = ye({
      where: r.query?.where,
      // normalize the value so it matches what we get from getAllValues()
      // Refer to https://devtopia.esri.com/WebGIS/arcgis-charts/issues/8127
      normalize: !0,
      // we do not escape the characters so they match what we get from getAllValues()
      // Refer to https://devtopia.esri.com/WebGIS/arcgis-charts/issues/8078
      escape: !1
    });
    e && a !== null && (a = Number(a));
    const s = be([r]);
    s !== void 0 && i.set(s, [
      ...i.get(s) ?? [],
      a
    ]);
  }), i;
}
function dt(t) {
  const { allValues: e, configUniqueValuesPerSeriesType: i, initialConfigWasEmpty: r } = t, a = /* @__PURE__ */ new Map();
  return Array.from(i.keys()).forEach((s) => {
    const l = i.get(s) ?? [];
    let n = [];
    r ? n = e : e.forEach((o) => {
      l.includes(o) || n.push(o);
    }), a.set(s, n);
  }), a;
}
function ct(t) {
  const { chartConfig: e, missingValue: i, splitByField: r, seriesTemplate: a } = t, s = JSON.parse(
    JSON.stringify({
      ...e.series[0],
      ...a
    })
  );
  if (s.query) {
    if (s.id = `${i}-${e.series.length}-${Date.now()}`, s.name = `${i}`, s.query.outStatistics?.[0] !== void 0) {
      const n = s.id;
      s.query.outStatistics[0].outStatisticFieldName = n, "y" in s && (s.y = n);
    }
    const l = typeof i == "number" || i === null ? i : `'${i}'`;
    s.query.where = `${r}=${l}`;
  }
  return s;
}
async function yt(t) {
  const { updateSplitBySeries: e, config: i, options: r, queryObject: a } = t, { seriesTemplate: s } = e ?? {};
  let l;
  if (a && e !== void 0 && i !== void 0 && (i.series.length > 0 || s !== void 0) && e.addMissingSeries) {
    const n = i.series.length === 0;
    i.series.length === 0 && s !== void 0 && i.series.push(s);
    const o = j(i.series);
    if (o === k.BoxPlotMonoFieldAndSplitBy || o === k.BoxPlotMultiFieldsAndSplitBy || o === k.BoxPlotMonoFieldAndCategoryAndSplitBy || o === k.BarAndLineSplitBy || o === k.BarAndLineSplitByNoAggregation) {
      const d = Ne(i.series[0]?.query?.where), f = ke(a);
      await f.load();
      const c = f.getField(d)?.toJSON(), p = Oe(c), S = await ot({
        queryObject: a,
        chartConfig: i,
        splitByField: d,
        options: r
      }), { allValues: g } = S;
      if ({ preFetchedQueryResponse: l } = S, g.length > 0) {
        const y = ut(i, p), F = dt({
          allValues: g,
          configUniqueValuesPerSeriesType: y,
          initialConfigWasEmpty: n
        });
        Array.from(F.keys()).forEach((x) => {
          (F.get(x) ?? []).forEach((u) => {
            i.series.push(
              ct({
                chartConfig: i,
                seriesTemplate: s,
                missingValue: u,
                splitByField: d
              })
            );
          }), n && i.series.shift();
        });
      }
    }
  }
  return l;
}
function gt(t, e) {
  e && !pe(e) && (e = qe({ chartConfig: e, queryObject: t }));
}
async function di(t) {
  const { configToAlter: e, layer: i, addMissingSeries: r, updateSplitBySeries: a, chartContainer: s, queryObject: l, options: n } = t;
  let o = e, d;
  return o && (gt(i, o), o = await lt(o, i), r && (d = await yt({
    updateSplitBySeries: a,
    config: o,
    options: n,
    queryObject: l
  })), o = Ve(o, s)), { alteredConfig: o, preFetchedQueryResponse: d };
}
const ci = (t) => {
  let e;
  const i = t.series[0];
  return L(i) && (e = i.temporalBinning?.size), e;
}, mt = (t) => {
  let e;
  const i = t.series[0];
  return L(i) && (e = i.temporalBinning?.unit), e;
}, yi = (t) => {
  let e;
  const i = t.series[0];
  return L(i) && (e = i.temporalBinning?.timeAggregationType), e;
}, gi = (t) => {
  let e;
  const i = t.series[0];
  return L(i) && (e = i.temporalBinning?.trimIncompleteTimeInterval), e;
}, mi = (t) => {
  let e;
  const i = t.series[0];
  return L(i) && (e = i.temporalBinning?.nullPolicy), e;
}, pt = (t, e) => {
  e?.series?.forEach((i) => {
    L(i) && (i.temporalBinning ??= {}, i.temporalBinning.size = t);
  }), I({ config: e, value: null, axisIndex: 1, isMax: !1 }), I({ config: e, value: null, axisIndex: 1, isMax: !0 });
}, ft = (t, e) => {
  e?.series?.forEach((i) => {
    L(i) && (i.temporalBinning ??= {}, i.temporalBinning.nullPolicy = t);
  }), I({ config: e, value: null, axisIndex: 1, isMax: !1 }), I({ config: e, value: null, axisIndex: 1, isMax: !0 });
}, pi = (t, e) => {
  e.series?.forEach((i) => {
    L(i) && (i.temporalBinning ??= {}, i.temporalBinning.unit = t);
  }), I({ config: e, value: null, axisIndex: 1, isMax: !1 }), I({ config: e, value: null, axisIndex: 1, isMax: !0 });
}, fi = (t, e) => {
  e.series?.forEach((i) => {
    L(i) && (i.temporalBinning ??= {}, i.temporalBinning.timeAggregationType = t);
  }), I({ config: e, value: null, axisIndex: 1, isMax: !1 }), I({ config: e, value: null, axisIndex: 1, isMax: !0 });
}, Si = (t, e) => {
  e.series?.forEach((i) => {
    L(i) && (i.temporalBinning ??= {}, i.temporalBinning.trimIncompleteTimeInterval = t);
  }), I({ config: e, value: null, axisIndex: 1, isMax: !1 }), I({ config: e, value: null, axisIndex: 1, isMax: !0 });
}, Fi = (t) => {
  pt(0, t);
}, Ti = (t) => t.hideEmptyRowsAndColumns, hi = (t, e) => {
  t.hideEmptyRowsAndColumns = e;
}, Ci = (t, e) => {
  const i = t.series[0];
  i.heatRulesType = e;
}, xi = (t) => t.series[0].heatRulesType, Bi = (t, e) => {
  const i = t.series[0];
  i.gradientRules = e;
}, bi = (t) => t.series[0].gradientRules, Ai = (t) => {
  const { yField: e, config: i, layerInfo: r } = t, { layerFieldsInfo: a } = r, s = te(1, i), l = St(i), n = H(i), o = i.series[0];
  if (typeof e == "string" && l !== e && o && (o.y = e, nt(i), l === s || n)) {
    const d = w(a, e);
    P(i, 1, d);
  }
}, St = (t) => t.series[0].y ?? "", vi = (t) => {
  const { binning: e, config: i, commonStrings: r } = t, a = i.series[0];
  if (a.xTemporalBinning = e, e?.unit !== void 0 && e.unit in r.utilsStrings.heatChartCalendarDatePartsUnits) {
    const s = r.utilsStrings.heatChartCalendarDatePartsUnits[e.unit];
    P(i, 0, s);
  }
}, Ii = (t) => t.series[0].xTemporalBinning, Li = (t) => {
  const { binning: e, config: i, commonStrings: r } = t, a = i.series[0];
  if (a.yTemporalBinning = e, e?.unit !== void 0 && e.unit in r.utilsStrings.heatChartCalendarDatePartsUnits) {
    const s = r.utilsStrings.heatChartCalendarDatePartsUnits[e.unit];
    P(i, 1, s);
  }
}, Vi = (t) => t.series[0].yTemporalBinning, Ft = (t, e) => {
  e.orderOptions ??= {};
  const i = e.axes?.[0]?.valueFormat?.type === "category" ? v.Category : v.YValue, r = J(e)[0], a = e.axes?.[1]?.valueFormat?.type === "category" ? v.Category : v.YValue, s = J(e)[1], l = {
    ...e.orderOptions.data ?? {},
    orderType: v.MultiAxes
  };
  r === G.customSort && i === v.Category && (l.orderByX = [...t]), s === G.customSort && a === v.Category && (l.orderByY = [...t]), Object.keys(l).length > 1 && (e.orderOptions.data = l);
}, qi = (t, e) => {
  const i = e.series[0];
  i.gridLine && (i.gridLine.width = t);
}, Ni = (t) => t.series[0].gridLine?.width, ki = (t, e) => {
  const i = e.series[0], r = t?.length === 4 && t.every((a) => typeof a == "number");
  i.gridLine && r && (i.gridLine.color = t);
}, Oi = (t) => t.series[0].gridLine?.color, wi = (t, e) => {
  const i = e.legend;
  i && (i.position = t);
}, Di = (t) => t.legend?.position ?? Ae.Bottom, Ri = (t) => t.series[0].query?.outStatistics?.[0]?.onStatisticField, Pi = (t) => {
  const { field: e, config: i, layerInfo: r, commonStrings: a } = t, s = i.series[0].query?.outStatistics?.[0], l = se(i, r, a);
  s ? (s.onStatisticField = e, s.outStatisticFieldName = `${s.statisticType}_${e}`) : i.series[0].query = {
    outStatistics: [
      {
        onStatisticField: e,
        outStatisticFieldName: `${b.Count}_${e}`,
        statisticType: b.Count
      }
    ]
  }, i.title && l && (i.title.content.text = Q(i, r, a));
}, Mi = (t) => t.series[0].query?.outStatistics?.[0].statisticType ?? b.Count, Ei = (t) => {
  const { aggregationType: e, config: i, layerInfo: r, commonStrings: a } = t, s = i.series[0]?.query?.outStatistics?.[0], l = se(i, r, a);
  s && e !== b.NoAggregation && (s.statisticType = e, s.outStatisticFieldName = `${e}_${s.onStatisticField}`), (e === b.Count || e === b.Sum) && Tt(X.Zero, i), i.title && l && (i.title.content.text = Q(i, r, a));
}, $i = (t) => t.nullPolicy, Tt = (t, e) => {
  e.nullPolicy = t;
}, Xi = (t) => t.series[0].emptyCells, Ui = (t, e) => {
  e.series[0].emptyCells = t;
}, Wi = (t) => t.series[0].emptyCells?.text, Yi = (t, e) => {
  e.series[0].emptyCells ? e.series[0].emptyCells.text = t : e.series[0].emptyCells = { text: t };
}, ji = (t) => t.includeLeapDay, Hi = (t, e) => {
  e.includeLeapDay = t;
}, Ki = (t) => t.series[0].emptyCells?.symbol, Qi = (t, e) => {
  e.series[0].emptyCells ? e.series[0].emptyCells.symbol = t : e.series[0].emptyCells = { symbol: t };
};
function zi(t) {
  return t.viewType;
}
function _i(t, e) {
  Ue(e) || (e.viewType = t);
}
function Gi(t) {
  return t.series[0].classBreaksRules?.breaksCount ?? U.classBreaks.breaksCount;
}
function Ji(t, e) {
  e.series[0].classBreaksRules !== void 0 ? e.series[0].classBreaksRules.breaksCount = t ?? U.classBreaks.breaksCount : e.series[0].classBreaksRules = {
    breaksCount: t ?? U.classBreaks.breaksCount
  };
}
function Zi(t) {
  return t.series[0].classBreaksRules?.classificationMethod;
}
function es(t, e) {
  e.series[0].classBreaksRules !== void 0 ? e.series[0].classBreaksRules.classificationMethod = t : e.series[0].classBreaksRules = {
    classificationMethod: t
  }, e.series[0].classBreaksRules.breaksCount = e.series[0].classBreaksRules.breaksCount ?? U.classBreaks.breaksCount;
}
function ts(t, e) {
  e.firstDayOfWeek = t;
}
function is(t) {
  return t.firstDayOfWeek;
}
function M(t) {
  const { seriesIndex: e, xAxisField: i, aggregationType: r, config: a, layerInfo: s, commonStrings: l } = t, n = K(s?.layer).idField;
  let o;
  const d = a.series[e ?? 0];
  let f = !1, c = !1, p = !1;
  if (d === void 0) {
    const y = a.series;
    f = y.slice(0, e).every((F) => h(F)), c = y.slice(0, e).every((F) => C(F)), p = y.slice(0, e).every((F) => ne(F));
  } else
    f = h(d), c = C(d), p = ne(d);
  f ? o = He({
    commonStrings: l,
    seriesIndex: e,
    objectIDField: n
  }) : c ? o = Ke({
    commonStrings: l,
    seriesIndex: e,
    objectIDField: n
  }) : p ? o = Qe(l, n) : o = ze({
    seriesIndex: e,
    objectIDField: n,
    commonStrings: l
  });
  const S = a.series[0]?.dataLabels, g = {
    ...o,
    x: i,
    y: "",
    dataLabels: {
      ...o.dataLabels,
      type: o.dataLabels?.type ?? O.Text,
      content: { ...o.dataLabels?.content, ...S?.content, type: Le.TS },
      visible: S?.visible ?? !1
    }
  };
  if (g.query = {
    groupByFieldsForStatistics: [i]
  }, r !== b.NoAggregation && n !== void 0) {
    const y = `${r}_${n}_0`.toUpperCase();
    g.y = y, g.query.outStatistics = [
      {
        statisticType: r,
        onStatisticField: n,
        outStatisticFieldName: y
      }
    ];
  }
  if (L(a.series[0]) && L(g)) {
    let y;
    if (a?.series?.[0] !== void 0 && (y = j(a.series)), y !== k.BarAndLineNoAggregation && y !== k.BarAndLineSplitByNoAggregation) {
      let F = mt(a);
      F === _.Quarter && (F = _.Month), g.temporalBinning = {
        ...a.series[0]?.temporalBinning,
        unit: F
      };
    }
  }
  return de(g);
}
function Q(t, e, i) {
  const { layerFieldsInfo: r } = e, { idField: a, isTraditionalTypeOID: s } = K(e.layer), l = $(t), n = ie(t), o = he(t), d = Y(t), f = s && (n.length === 0 || n.includes(a)), c = We(o, i), p = f ? "" : n.map((u) => w(r ?? [], u)).join(", "), S = w(r ?? [], l), g = w(r ?? [], d), y = t.series.some((u) => V(u)), F = y ? "category" : "xAxisField", x = S;
  let T = "";
  if (Fe({
    fieldName: l,
    numericFields: n,
    fieldList: r
  }))
    T = D(i.titleLabels.aggrTypeOfAggrFields, {
      aggregationType: c,
      aggregationFields: p
    });
  else if (o === b.NoAggregation) {
    const u = f ? a : p;
    if (d === "") {
      const A = y ? "aggrTypeCategory" : "aggrTypeXAxisField";
      T = D(i.titleLabels[A], {
        aggregationType: u,
        [F]: x
      });
    } else {
      const A = y ? "aggrTypeCategoryBySplitBy" : "aggrTypeXAxisFieldBySplitBy";
      T = D(i.titleLabels[A], {
        aggregationType: u,
        [F]: x,
        splitByField: g
      });
    }
  } else if (o === b.Count)
    if (d === "") {
      const u = y ? "aggrTypeCategory" : "aggrTypeXAxisField";
      T = D(i.titleLabels[u], {
        aggregationType: c,
        [F]: x
      });
    } else {
      const u = y ? "aggrTypeCategoryBySplitBy" : "aggrTypeXAxisFieldBySplitBy";
      T = D(i.titleLabels[u], {
        aggregationType: c,
        [F]: x,
        splitByField: g
      });
    }
  else if (d !== "") {
    const u = y ? "aggrTypeOfAggrFieldByCategoryBySplitByField" : "aggrTypeOfAggrFieldByXAxisFieldBySplitByField";
    T = D(i.titleLabels[u], {
      aggregationType: c,
      aggregationField: p,
      [F]: x,
      splitByField: g
    });
  } else if (n.length > 0) {
    const u = y ? "aggrTypeOfAggrFieldsByCategory" : "aggrTypeOfAggrFieldsByXAxisField";
    T = D(i.titleLabels[u], {
      aggregationType: c,
      aggregationFields: p,
      [F]: x
    });
  }
  return T;
}
const Te = (t) => ye({
  where: t.query?.where,
  normalize: !1,
  escape: !1
}) ?? "", he = (t) => t?.series?.[0]?.query?.outStatistics?.[0]?.statisticType ?? b.NoAggregation, Y = xe(
  (t) => {
    const e = t?.series?.[0]?.query?.where?.split("=")[0].trim() ?? "";
    return Ye(t?.series?.[0]?.query?.where) ? "" : e;
  },
  (t) => t?.series?.[0]?.query?.where
), ie = (t) => {
  let e = [];
  if (Y(t) === "")
    e = t?.series.map((i) => {
      const r = i.query?.outStatistics?.map((a) => a.onStatisticField) ?? [];
      return r?.length > 0 ? r : i.y;
    }).flat() ?? [];
  else {
    const i = t?.series?.[0], r = i?.query?.outStatistics?.[0]?.onStatisticField ?? [i?.y].flat()[0];
    e = r !== void 0 ? [r] : [];
  }
  return [...new Set(e.filter((i) => i !== ""))];
};
function ht(t) {
  const { numericFields: e, aggregationType: i, config: r, layerInfo: a } = t;
  let s = !1;
  i === b.Count ? s = !0 : i === b.NoAggregation && (s = e.every((o) => {
    const d = a.layerFieldsInfo.find((f) => f.name === o);
    return d !== void 0 ? re(d) : !1;
  })), oe(r, 1, s);
  const l = r.series.some((o) => h(o)), n = r.series.some((o) => C(o));
  if (l || n) {
    const o = $(r), d = a.layerFieldsInfo.find((c) => c.name === o), f = d !== void 0 && re(d);
    oe(r, 0, f);
  }
}
const J = (t) => {
  let e;
  if (Z(t)) {
    const i = t.orderOptions?.data?.orderByX ?? [""], r = t.orderOptions?.data?.orderByY ?? [""], a = t.axes?.[0]?.valueFormat?.type === "category" ? v.Category : v.YValue, s = t.axes?.[1]?.valueFormat?.type === "category" ? v.Category : v.YValue;
    e = [ue(i, a), ue(r, s)];
  } else {
    const i = t.orderOptions?.data?.orderType ?? v.Category, r = t.orderOptions?.data && "orderBy" in t.orderOptions.data ? t.orderOptions.data.orderBy : ve.Ascending;
    e = [st(r, i)];
  }
  return e;
};
function Ct(t) {
  const { xAxisField: e, numericFields: i, aggregationType: r, config: a, layerInfo: s, commonStrings: l } = t, { layerFieldsInfo: n } = s;
  let o = !1;
  const d = M({
    config: a,
    xAxisField: e,
    aggregationType: r,
    layerInfo: s,
    seriesIndex: 0,
    commonStrings: l
  }), f = ee({
    config: a,
    xAxisField: e,
    numericFields: i,
    aggregationType: r,
    layer: s.layer,
    fieldList: n
  }).name, c = {
    ...d,
    id: `series_${Date.now()}`,
    name: l.axisLabels.values,
    x: W,
    y: f
  }, p = [];
  for (const g of i)
    p.push({
      onStatisticField: g,
      outStatisticFieldName: `${g}_0`.toUpperCase(),
      statisticType: r
    });
  c.query = { outStatistics: p };
  const S = a.series[0];
  return S !== void 0 && (c.id = S.id, (h(c) && h(S) || C(c) && C(S)) && E(c, S), a.colorMatch || (V(S) && V(c) ? c.fillSymbol = S.fillSymbol : (h(c) && h(S) || C(c) && C(S)) && (c.lineSymbol = S.lineSymbol, c.markerSymbol = S.markerSymbol), o = !0), c.id = S.id, S.name !== d.name && (c.name = S.name)), a.series.length === 1 && ((h(c) || C(c)) && E(c, a.series[0]), a.colorMatch || (V(c) ? c.fillSymbol = a.series[0].fillSymbol : (h(c) || C(c)) && (c.lineSymbol = a.series[0].lineSymbol, c.markerSymbol = a.series[0].markerSymbol), o = !0)), a.colorMatch = !o, [c];
}
function Ce(t) {
  const { xAxisField: e, numericFields: i, aggregationType: r, config: a, layerInfo: s, commonStrings: l } = t, { layerFieldsInfo: n } = s, o = K(s.layer).idField, d = [];
  let f = !1;
  for (let y = 0; y < i.length; y += 1) {
    const F = M({
      config: a,
      xAxisField: e,
      aggregationType: r,
      layerInfo: s,
      seriesIndex: y,
      commonStrings: l
    }), x = ee({
      config: a,
      xAxisField: e,
      numericFields: i,
      aggregationType: r,
      layer: s.layer,
      numericFieldIndex: y,
      fieldList: n
    }).name, T = i[y];
    let u = {
      ...F,
      query: {
        ...F.query
      }
    };
    i && T !== o && (u = {
      ...u,
      id: T,
      name: w(n, T)
    });
    let A = T;
    u.query !== void 0 && (r !== b.NoAggregation ? (A = x, u.query.outStatistics = [
      {
        ...F.query?.outStatistics?.[0] ?? {},
        onStatisticField: T,
        outStatisticFieldName: x,
        statisticType: r
      }
    ]) : delete u.query.outStatistics), u.y = A;
    const m = a.series.find(
      (N) => N.query?.outStatistics?.[0]?.onStatisticField === T && N.query.where === void 0 || N.query?.outStatistics === void 0 && (N.y === T || N.x === T)
    );
    if (m !== void 0 && typeof m.y == "string" && m.y.trim() !== "") {
      (h(u) && h(m) || C(u) && C(m)) && E(u, m), a.colorMatch || (V(u) && V(m) ? u.fillSymbol = m.fillSymbol : (h(u) && h(m) || C(u) && C(m)) && (u.lineSymbol = m.lineSymbol, u.markerSymbol = m.markerSymbol), f = !0);
      const N = M({
        config: a,
        xAxisField: e,
        aggregationType: r,
        layerInfo: s,
        seriesIndex: y,
        commonStrings: l
      });
      u.id = m.id, m.id !== N.id && m.name !== N.name && (u.id = m.id, u.name = m.name), u.type === O.HeatSeries && m.type === O.HeatSeries && (u.y = m.y, u.heatRulesType = m.heatRulesType);
    }
    d.push(u);
  }
  const c = d.filter((y) => V(y)), p = d.filter(
    (y) => h(y) || C(y)
  ), S = a.series.filter((y) => V(y)), g = a.series.filter(
    (y) => h(y) || C(y)
  );
  return S.length === 1 && c.length === 1 && !a.colorMatch && (V(S[0]) && V(c[0]) && (c[0].fillSymbol = S[0].fillSymbol), f = !0), g.length === 1 && p.length === 1 && ((h(g[0]) && h(p[0]) || C(g[0]) && C(p[0])) && E(p[0], g[0]), a.colorMatch || ((h(g[0]) && h(p[0]) || C(g[0]) && C(p[0])) && (p[0].lineSymbol = g[0].lineSymbol, p[0].markerSymbol = g[0].markerSymbol), f = !0)), a.colorMatch = !f, d;
}
async function xt(t) {
  const { splitByField: e, xAxisField: i, numericFields: r, aggregationType: a, config: s, layerInfo: l, commonStrings: n } = t, { layer: o, layerFieldsInfo: d } = l, { fetchNullValues: f } = s?.series?.[0]?.query ?? {}, c = i !== W ? i : void 0, { categoriesArray: p, splitByFieldType: S, numericalField: g } = await it({
    config: s,
    layer: o,
    layerFieldsInfo: d,
    xAxisField: c,
    aggregationFields: r,
    splitByField: e,
    fetchNullValues: f
  });
  let y = [], F = !1;
  if (p.length === 0) {
    y = Ce({
      xAxisField: i,
      numericFields: r,
      aggregationType: a,
      config: s,
      layerInfo: l,
      commonStrings: n
    });
    for (const x of y)
      x.query && (x.query.where = `${e}=''`);
  } else
    for (let x = 0; x < p.length; x += 1) {
      const T = p[x], u = ee({
        config: s,
        xAxisField: i,
        numericFields: r,
        aggregationType: a,
        layer: l.layer,
        seriesIndex: x,
        splitByField: e,
        fieldList: d
      }).name, A = M({
        config: s,
        xAxisField: i,
        aggregationType: a,
        layerInfo: l,
        seriesIndex: x,
        commonStrings: n
      }), m = {
        fieldName: e,
        fieldValueToMatch: T ?? "",
        subtypeInfo: ge(l.layer),
        fieldList: l.layerFieldsInfo
      }, N = me(m), R = `${T}`, B = {
        ...A,
        id: R,
        name: N ?? R,
        y: u,
        query: {
          ...A.query,
          where: `${e}=${S === ce.String && T !== null ? `'${T}'` : T}`,
          outStatistics: a !== b.NoAggregation ? [
            {
              ...A.query?.outStatistics?.[0] ?? {},
              onStatisticField: g,
              outStatisticFieldName: u,
              statisticType: a
            }
          ] : void 0
        }
      };
      a === b.NoAggregation && (B.query !== void 0 && (delete B.query.outStatistics, delete B.query.groupByFieldsForStatistics), B.y = g);
      const q = s.series.find(
        (z) => String(Te(z)).trim().replace(/'/gu, "") === R
      );
      q !== void 0 && ((h(B) && h(q) || C(B) && C(q)) && E(B, q), s.colorMatch || (V(B) && V(q) ? B.fillSymbol = q.fillSymbol : (h(B) && h(q) || C(B) && C(q)) && (B.lineSymbol = q.lineSymbol, B.markerSymbol = q.markerSymbol), F = !0), B.id = q.id, B.name = q.name), B.query && (B.query.fetchNullValues = s.series.some((z) => z.query?.fetchNullValues)), y.push(B);
    }
  return s.colorMatch = !F, y;
}
const ss = async (t) => {
  const { xAxisField: e, splitByField: i, numericFields: r, aggregationType: a, config: s, layerInfo: l, commonStrings: n } = t, { layerFieldsInfo: o } = l ?? {}, d = at(s), f = $(s), c = se(s, l, n), p = Bt(s, l, n), S = bt(s, l);
  let g = fe(s);
  const y = s.series.some((m) => V(m)), F = Je(s.series), x = Ze(s.series), T = Z(s), u = {};
  T && (u.classBreaksRules = s.series[0]?.classBreaksRules, u.xTemporalBinning = s.series[0]?.xTemporalBinning, u.yTemporalBinning = s.series[0]?.yTemporalBinning, u.emptyCells = s.series[0]?.emptyCells, u.nullPolicy = s.nullPolicy);
  let A = !1;
  if (o !== void 0) {
    const m = je(o);
    !m.includes(f) && m.includes(e) ? (g = _e(), A = !0) : m.includes(f) && !m.includes(e) && (y ? g = le() : g = Ge(), A = !0), o.find((R) => R.name === e)?.type === ce.String && (g = le()), g !== void 0 && tt(s, n, g);
  }
  if (d === 0 && (s.series = [
    M({ config: s, xAxisField: e, aggregationType: a, layerInfo: l, commonStrings: n })
  ]), s.series.forEach((m) => {
    m.x = e, m.query !== void 0 && (m.query.groupByFieldsForStatistics = [e]);
  }), i === "" || r.length > 1 ? Fe({
    fieldName: e,
    numericFields: r,
    fieldList: o
  }) ? s.series = Ct({
    config: s,
    layerInfo: l,
    xAxisField: e,
    numericFields: r,
    aggregationType: a,
    commonStrings: n
  }) : s.series = Ce({
    config: s,
    layerInfo: l,
    xAxisField: e,
    numericFields: r,
    aggregationType: a,
    commonStrings: n
  }) : (await Promise.resolve(), s.series = await xt({
    config: s,
    layerInfo: l,
    splitByField: i,
    xAxisField: e,
    numericFields: r,
    aggregationType: a,
    commonStrings: n
  })), S && s.series[0].type !== O.HeatSeries) {
    const m = w(o, e);
    P(s, 0, m);
  }
  if (p && s.series[0].type !== O.HeatSeries) {
    const m = Se({
      config: s,
      layerInfo: l,
      commonStrings: n,
      numericFields: r,
      aggregationType: a
    });
    P(s, 1, m);
  }
  if (s.title && c && (s.title.content.text = Q(s, l, n)), I({ config: s, value: null, axisIndex: 1, isMax: !1 }), I({ config: s, value: null, axisIndex: 1, isMax: !0 }), ht({ numericFields: r, aggregationType: a, config: s, layerInfo: l }), (F || x) && (I({ config: s, value: null, axisIndex: 0, isMax: !1 }), I({ config: s, value: null, axisIndex: 0, isMax: !0 }), A)) {
    const m = g?.type === O.DateAxisFormat ? X.Null : X.Interpolate;
    ft(m, s);
  }
  T && (s.series[0].classBreaksRules = u.classBreaksRules, s.series[0].xTemporalBinning = u.xTemporalBinning, s.series[0].yTemporalBinning = u.yTemporalBinning, s.series[0].emptyCells = u.emptyCells, s.nullPolicy = u.nullPolicy);
}, E = (t, e) => {
  t.markerVisible = e.markerVisible, t.lineSmoothed = e.lineSmoothed, t.showArea = e.showArea, t.areaColor = e.areaColor, t.stackNegativeValuesToBaseline = e.stackNegativeValuesToBaseline, e.lineSymbol && (t.lineSymbol = { ...e.lineSymbol, color: t.lineSymbol?.color }), e.markerSymbol && (t.markerSymbol = {
    ...e.markerSymbol,
    color: t.markerSymbol?.color
  });
}, as = (t) => {
  const { config: e, seriesIndex: i, layerInfo: r, commonStrings: a } = t, s = e.series?.[i], l = Y(e);
  let n;
  if (j(e.series) === k.BarAndLineFromFields)
    n = a.axisLabels.values;
  else if (Be(l)) {
    const d = s.query?.outStatistics?.[0]?.onStatisticField;
    d !== void 0 && d !== K(r.layer).idField && (n = w(r.layerFieldsInfo, d));
  } else {
    const d = String(Te(s)).trim().replace(/'/gu, ""), f = {
      fieldName: Y(e),
      fieldValueToMatch: d ?? "",
      subtypeInfo: ge(r.layer),
      fieldList: r.layerFieldsInfo
    };
    n = me(f) ?? d;
  }
  return n ?? s.id;
}, rs = (t, e) => {
  if (Z(e))
    Ft(t, e);
  else {
    e.orderOptions ??= {};
    const i = e.orderOptions?.data?.orderType;
    J(e)[0] === G.customSort && i === v.Category && (e.orderOptions.data = {
      orderType: v.Category,
      orderBy: [...t]
    });
  }
}, ls = async (t, e) => {
  const i = $(t), r = fe(t)?.type, a = ie(t), s = j(t.series);
  let l = [];
  return r === O.CategoryAxisFormat && (s === k.BarAndLineFromFields || i === W ? l = a.map((n) => w(e?.layerFieldsInfo, n)) : i.trim() !== "" && i !== W && e !== void 0 && (l = await et({
    layerInfo: e,
    config: t,
    category: i
  }))), l;
};
function se(t, e, i) {
  const r = Q(t, e, i), a = rt(t), s = H(t);
  return r === a || s;
}
function Bt(t, e, i) {
  const r = Se({
    config: t,
    layerInfo: e,
    commonStrings: i,
    numericFields: ie(t),
    aggregationType: he(t)
  }), a = te(1, t), s = H(t);
  return r === a || s;
}
function bt(t, e) {
  const i = w(e.layerFieldsInfo, $(t)), r = te(0, t), a = H(t);
  return i === r || a;
}
function ns(t) {
  return L(t.series[0]) ? t.series[0].binTemporalData ?? !0 : !1;
}
function os(t, e) {
  e?.series.forEach((i) => {
    (i?.type === O.BarSeries || i?.type === O.LineSeries) && (i.binTemporalData = t, t ? i.temporalBinning = {
      unit: _.Day,
      size: 0,
      timeAggregationType: Ie.Start,
      trimIncompleteTimeInterval: !0,
      nullPolicy: X.Null
    } : delete i.temporalBinning);
  });
}
export {
  Si as $,
  Mi as A,
  Ei as B,
  Di as C,
  wi as D,
  $i as E,
  Tt as F,
  Xi as G,
  Ui as H,
  J as I,
  Wi as J,
  Yi as K,
  ji as L,
  Hi as M,
  is as N,
  ts as O,
  Ki as P,
  Qi as Q,
  he as R,
  ie as S,
  Y as T,
  ci as U,
  pt as V,
  mt as W,
  pi as X,
  yi as Y,
  fi as Z,
  gi as _,
  Q as a,
  mi as a0,
  ft as a1,
  Fi as a2,
  ls as a3,
  rs as a4,
  ns as a5,
  os as a6,
  ss as a7,
  di as a8,
  ui as a9,
  oi as aa,
  li as ab,
  ni as ac,
  nt as ad,
  St as b,
  zi as c,
  _i as d,
  Ii as e,
  vi as f,
  as as g,
  Vi as h,
  Li as i,
  Gi as j,
  Ji as k,
  Zi as l,
  es as m,
  Ti as n,
  hi as o,
  Ni as p,
  qi as q,
  Oi as r,
  Ai as s,
  ki as t,
  xi as u,
  Ci as v,
  bi as w,
  Bi as x,
  Ri as y,
  Pi as z
};
