/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { C as v } from "./chart-model.js";
import "@arcgis/toolkit/type";
import { e as B, W as O, a as m } from "./series-types.js";
import "@arcgis/toolkit/intl";
import { isEqual as W } from "lodash-es";
import "luxon";
import "d3-array";
import { bc as L, b1 as D, b7 as M, aV as P, aT as V, b as S, bR as N, _ as U } from "./index.js";
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/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 { l as h, j as d, k as f } from "./index4.js";
import "@arcgis/toolkit/dom";
import { g as I, u as R, s as _, c as E, b as k, a as X } from "./utils.js";
import { R as y, S as u, U as j, V as w, W as z, X as K, Y, Z as $, _ as G, $ as q, a0 as Z, a1 as H, a2 as J, I as T, a3 as x, a4 as A, a5 as Q, a6 as ee, T as te, a7 as C, a as ie } from "./serial-chart-data.js";
import { s as b, d as F, b as se } from "./data.js";
import { S as re } from "./common.js";
import { M as oe, a as ne, W as ae, b as le } from "./model-with-tooltips.js";
import { W as ge } from "./rotated-state.js";
import { W as ce } from "./data-labels-symbol.js";
import { W as pe, a as he, b as de } from "./axis-lines-symbol.js";
import { W as me, b as fe } from "./model-with-guides.js";
import { W as ye, a as ue, b as Se } from "./generate-x-axis-title.js";
import { m as o } from "./model-setter.js";
import { M as _e, a as Te } from "./model-with-y-guides.js";
import { M as xe } from "./model-with-x-guides.js";
const Ae = (s, e) => e.series?.[s].assignToSecondValueAxis ?? !1, Ce = (s) => {
  const { assign: e, seriesIndex: t, config: i, commonStrings: r } = s;
  e && i.axes !== void 0 && !i.axes?.[2] && (i.axes[2] = M(r, O.BarSeries)[1]), i.series && i.series.forEach((n, g) => {
    g === t ? n.assignToSecondValueAxis = e : g !== t && n?.assignToSecondValueAxis !== !0 && (n.assignToSecondValueAxis = !1);
  }), i.axes?.[2] && !i.series.some((n) => n.assignToSecondValueAxis) && i.axes.splice(2, 1);
}, be = (s, e) => e.series?.[s]?.name, Fe = (s, e, t) => {
  t.series?.[e] && (t.series[e].name = s);
}, ve = (s, e) => {
  const t = e.series?.[s];
  let i;
  return h(t) ? i = t.fillSymbol?.color : d(t) && (i = t.lineSymbol?.color), i;
}, Be = (s, e, t) => {
  const i = t.series?.[e];
  i && (h(i) ? i.fillSymbol = {
    ...L(e),
    ...i.fillSymbol,
    color: s
  } : d(i) && (i.lineSymbol = {
    ...D(e),
    ...i.lineSymbol,
    color: s
  }));
}, Ie = (s) => s.series.map((e) => e.id), Oe = (s, e) => {
  e.series?.sort(
    (t, i) => s.findIndex((r) => t.id === r) - s.findIndex((r) => i.id === r)
  );
}, We = (s) => s.stackedType ?? B.Side, Le = (s) => {
  const { newStackedType: e, config: t, layerInfo: i, commonStrings: r } = s;
  if (t.stackedType = e, i !== void 0) {
    const n = I({
      config: t,
      layerInfo: i,
      commonStrings: r,
      numericFields: u(t),
      aggregationType: y(t)
    });
    R(t, 1, n);
  }
  t.axes?.[1] && (e === B.Stacked100 ? t.axes[1].valueFormat = P() : t.axes[1].valueFormat = V()), _({ config: t, value: null, axisIndex: 1, isMax: !1 }), _({ config: t, value: null, axisIndex: 1, isMax: !0 });
}, De = (s) => {
  let e;
  const t = s.series.find(
    (i) => (h(i) || d(i) || f(i)) && i.nullCategory !== void 0
  );
  return (h(t) || d(t) || f(t)) && (e = t?.nullCategory), e;
}, Me = (s, e) => {
  s.series.forEach((t) => {
    (h(t) || d(t) || f(t)) && (t.nullCategory = e);
  });
};
var Pe = Object.defineProperty, Ve = Object.getOwnPropertyDescriptor, p = (s, e, t, i) => {
  for (var r = Ve(e, t), n = s.length - 1, g; n >= 0; n--)
    (g = s[n]) && (r = g(e, t, r) || r);
  return r && Pe(e, t, r), r;
};
class c extends v {
  /**
   * Time interval size for a temporal bar chart or line chart.
   */
  get temporalBinningSize() {
    return j(this._config);
  }
  set temporalBinningSize(e) {
    w(e, this._config);
  }
  /**
   * Time interval units for a temporal bar chart or line chart.
   */
  get temporalBinningUnit() {
    return z(this._config);
  }
  set temporalBinningUnit(e) {
    K(e, this._config);
  }
  /**
   * Time aggregation type for a temporal bar chart or line chart.
   */
  get timeAggregationType() {
    return Y(this._config);
  }
  set timeAggregationType(e) {
    $(e, this._config);
  }
  /**
   * Whether to trim incomplete time intervals for a temporal bar chart or line chart.
   */
  get trimIncompleteTimeInterval() {
    return G(this._config);
  }
  set trimIncompleteTimeInterval(e) {
    q(e, this._config);
  }
  /**
   * Policy which tells the chart how to handle null values.
   *
   * `null`: Break the bar where null values are present.
   * `zero`: Treats null values as zero.
   * `interpolate`:
   *   - Bar series: same effect as `null`
   *   - Line series: connects the lines across null values.
   */
  get nullPolicy() {
    return Z(this._config);
  }
  set nullPolicy(e) {
    H(e, this._config);
  }
  resetTimeInterval() {
    J(this._config);
  }
}
p([
  o
], c.prototype, "temporalBinningSize");
p([
  o
], c.prototype, "temporalBinningUnit");
p([
  o
], c.prototype, "timeAggregationType");
p([
  o
], c.prototype, "trimIncompleteTimeInterval");
p([
  o
], c.prototype, "nullPolicy");
p([
  o
], c.prototype, "resetTimeInterval");
var Ne = Object.defineProperty, Ue = Object.getOwnPropertyDescriptor, l = (s, e, t, i) => {
  for (var r = Ue(e, t), n = s.length - 1, g; n >= 0; n--)
    (g = s[n]) && (r = g(e, t, r) || r);
  return r && Ne(e, t, r), r;
};
class a extends v {
  constructor() {
    super(...arguments), this._xAxisField = "", this._aggregationType = m.Count, this._splitByField = "", this._numericFields = [];
  }
  // #endregion
  // ---------------------------------------------------------------------------------------------
  // #region Data Props
  // ---------------------------------------------------------------------------------------------
  /**
   * The x-axis field for the chart.
   */
  get xAxisField() {
    return this._xAxisField;
  }
  set xAxisField(e) {
    if (this._xAxisField = e, this.dirtyAsyncSetters.push("xAxisField"), e.trim() === "" || e === S) {
      const r = this.numericFields;
      this.resetConfig(), this.numericFields = r;
    }
    this.processSeriesRebuild();
  }
  /**
   * The aggregation type for the chart.
   */
  get aggregationType() {
    return this._aggregationType;
  }
  set aggregationType(e) {
    this._aggregationType = e, this.dirtyAsyncSetters.push("aggregationType"), this.processSeriesRebuild();
  }
  /**
   * The split-by field for the chart.
   */
  get splitByField() {
    return this._splitByField;
  }
  set splitByField(e) {
    this.layerInfo?.layerFieldsInfo && E(this.layerInfo?.layerFieldsInfo, e) && (this._splitByField = e, this.dirtyAsyncSetters.push("splitByField"), this.processSeriesRebuild());
  }
  /**
   * The numeric fields array for the chart.
   */
  get numericFields() {
    return this._numericFields;
  }
  set numericFields(e) {
    typeof e == "string" && (e = [e]);
    const t = k(this.layerInfo?.layerFieldsInfo ?? [], e), i = (this.xAxisField === "" || this.xAxisField === S) && e.length === 0;
    if (t && !i) {
      const r = N(this.layerInfo.layer).idField;
      e.length === 0 && r !== void 0 ? (e.push(r), this._aggregationType = m.Count) : this._aggregationType === m.Count && e.length >= 1 && r !== void 0 && !e.includes(r) && (this._aggregationType = m.Sum), this._numericFields = e, this.dirtyAsyncSetters.push("numericFields"), this.numericFields.length > 1 && (this._splitByField = "", this.dirtyAsyncSetters.push("splitByField")), this.processSeriesRebuild();
    } else i && this.resetConfig();
  }
  /**
   * Gets whether the series should be assigned to the second value axis.
   *
   * @param seriesIndex - The index of the series.
   */
  getAssignToSecondValueAxis(e) {
    return Ae(e, this._config);
  }
  setAssignToSecondValueAxis(e, t) {
    Ce({ assign: e, seriesIndex: t, config: this._config, commonStrings: this.commonStrings });
  }
  /**
   * Gets the sort order for the bar chart.
   */
  getSortOrder() {
    return T(this._config)[0];
  }
  setSortOrder(e, t) {
    b(e, this._config, t);
  }
  /**
   * Gets the list of the category values for the chart.
   */
  async getCustomSortValues() {
    return await x(this._config, this.layerInfo);
  }
  async setDataFilters(e) {
    super.setDataFilters(e);
    const t = T(this._config)[0];
    if (t === re.customSort) {
      this.startAsyncUpdate();
      const i = await x(this._config, this.layerInfo);
      b(t, this._config, i), this.finishAsyncUpdate();
    }
    this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange"));
  }
  /**
   * The list of values for custom sort.
   */
  get orderByList() {
    return F(this._config);
  }
  set orderByList(e) {
    !U(this._config) && A(e, this._config);
  }
  /**
   * Updates the series based on the layer filter.
   */
  async updateSeriesFromLayerFilter() {
    this.layerInfo !== void 0 && (this.dirtyAsyncSetters.push("updateSeriesFromLayerFilter"), await this.processSeriesRebuild());
  }
  /**
   * Whether temporal binning is enabled for all series in the config.
   *
   * **Note:** Setting this prop removes any existing temporal binning settings if `value` is set to `false`.
   */
  get binTemporalData() {
    return Q(this._config);
  }
  set binTemporalData(e) {
    ee(e, this._config);
  }
  // #endregion
  // ---------------------------------------------------------------------------------------------
  // #region Series Props
  // ---------------------------------------------------------------------------------------------
  /**
   * Gets the name of the series based on the series index.
   * If the index is out of bounds, it will return `undefined`.
   *
   * @param seriesIndex - The index of the series.
   */
  getSeriesName(e) {
    return be(e, this._config);
  }
  setSeriesName(e, t) {
    Fe(e, t, this._config);
  }
  /**
   * Gets the color of the series based on the series index. If the index is out of bounds, it will return `undefined`.
   *
   * @param seriesIndex - The index of the series.
   */
  getSeriesColor(e) {
    return ve(e, this._config);
  }
  setSeriesColor(e, t) {
    Be(e, t, this._config);
  }
  /**
   * Gets the order of the series array by returning an array of series ids.
   */
  getOrderOfSeries() {
    return Ie(this._config);
  }
  setOrderOfSeries(e) {
    Oe(e, this._config);
  }
  /**
   * How the bars (lines) should be placed when multiple series are rendered in the same chart, say when "split by" operation is executed on any field or
   * when multiple attributes/fields corresponding to the same set of classes are to be visualized.
   *
   * **Note**: Currently this property is only used for bar charts and line charts.
   */
  get stackedType() {
    return We(this._config);
  }
  set stackedType(e) {
    Le({
      newStackedType: e,
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings
    });
  }
  moveSeries(e, t) {
    const i = this.getOrderOfSeries(), [r] = i.splice(e, 1);
    i.splice(t, 0, r), this.setOrderOfSeries(i);
  }
  moveCustomSortValuesInOrderByList(e, t) {
    const i = F(this._config), [r] = i.splice(e, 1);
    i.splice(t, 0, r), A(i, this._config);
  }
  /**
   * The null category configuration of the chart.
   *
   * The following to be set on the model for the null category to be applied:
   * - `fetchNULLValues` to be set to `true` on the model.
   * - `colorMatch` to be set to `false` on the model
   */
  get nullCategory() {
    return De(this._config);
  }
  set nullCategory(e) {
    const t = this.nullCategory;
    W(t, e) || Me(this._config, e);
  }
  /**
   * Sets up the local props based on the current config.
   *
   * @private
   */
  setupLocalProps() {
    this._xAxisField = se(this._config), this._aggregationType = y(this._config), this._splitByField = te(this._config), this._numericFields = u(this._config);
  }
  resetConfig() {
    const e = this._config.id;
    this._config = this.generateDefaultConfig(), this._config.id = e, this.setupLocalProps(), this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange"));
  }
  /**
   * Determines if we can rebuild the series synchronously.
   * We can call the build new series function synchronously when either is true:
   * - there is no split-by field
   * - there are multiple numeric fields
   *
   * @private
   */
  canUseSynchronousSeriesRebuild() {
    return this.splitByField === "" || this.numericFields.length > 1;
  }
  /**
   * Processes the series rebuild.
   * If multiple async setters are called in succession, this function will only rebuild the series once after all setters have been called.
   * @private
   */
  async processSeriesRebuild() {
    if (this.layerInfo !== void 0) {
      const e = this.dirtyAsyncSetters[this.dirtyAsyncSetters.length - 1];
      if (this.canUseSynchronousSeriesRebuild())
        this.dirtyAsyncSetters = [], C(this.getBuildNewSeriesProps());
      else if (!this.asyncConfigUpdateInProgress) {
        for (this.startAsyncUpdate(); this.dirtyAsyncSetters.length > 0; )
          this.dirtyAsyncSetters = [], await C(this.getBuildNewSeriesProps());
        this.finishAsyncUpdate();
      }
      this.setupLocalProps(), this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange")), this.emitConfigUpdate(e);
    }
  }
  // #endregion
  // #region Private Methods
  /**
   * Gets the properties needed to build a new series array.
   */
  getBuildNewSeriesProps() {
    return {
      xAxisField: this.xAxisField,
      splitByField: this.splitByField,
      numericFields: this.numericFields,
      aggregationType: this.aggregationType,
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings
    };
  }
  // #endregion
  // ---------------------------------------------------------------------------------------------
  // #region Public Methods
  // ---------------------------------------------------------------------------------------------
  /**
   * Generate the proper chart title given the current config.
   */
  generateChartTitle() {
    return this.layerInfo !== void 0 ? ie(this._config, this.layerInfo, this.commonStrings) : "";
  }
  /**
   * Generate the proper y-axis title given the current config.
   */
  generateYAxisTitle() {
    return this.layerInfo !== void 0 ? I({
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings,
      numericFields: u(this._config),
      aggregationType: y(this._config)
    }) : "";
  }
  // #endregion
}
l([
  o
], a.prototype, "setAssignToSecondValueAxis");
l([
  o
], a.prototype, "setSortOrder");
l([
  o
], a.prototype, "setDataFilters");
l([
  o
], a.prototype, "orderByList");
l([
  o
], a.prototype, "binTemporalData");
l([
  o
], a.prototype, "setSeriesName");
l([
  o
], a.prototype, "setSeriesColor");
l([
  o
], a.prototype, "setOrderOfSeries");
l([
  o
], a.prototype, "stackedType");
l([
  o
], a.prototype, "moveSeries");
l([
  o
], a.prototype, "moveCustomSortValuesInOrderByList");
l([
  o
], a.prototype, "nullCategory");
l([
  o
], a.prototype, "resetConfig");
const Re = [
  _e,
  oe,
  ne,
  ae,
  ge,
  le,
  ce,
  xe,
  Te,
  c,
  ye,
  me,
  ue,
  fe,
  pe,
  he,
  de,
  Se
];
X(a, Re);
export {
  a as S,
  Fe as a,
  be as g,
  Be as s
};
