/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { C as A } from "./chart-model.js";
import { B as P } from "./common.js";
import { W as D, a as T, b as V } from "./axis-lines-symbol.js";
import { W as N, b as E } from "./model-with-guides.js";
import { W as R, a as U, b as z } from "./generate-x-axis-title.js";
import { M as X, a as j, W as K, b as Y } from "./model-with-tooltips.js";
import { W as k } from "./rotated-state.js";
import { W as G } from "./data-labels-symbol.js";
import { g as q, s as g, a as J, b as f } from "./series.js";
import { b as H, c as Q, a as Z } from "./utils.js";
import { s as $, g as ee, a as te, b as se, c as ie, d as y, e as p, f as u, h as m, i as S, j as oe, k as re, l as ne, m as L, n as ae, o as le, p as he, q as ce, r as _, t as de, u as C, v as ge, w as B, x as fe, y as ye, z as pe, A as ue, B as me, C as Se, D as M } from "./box-plot-data.js";
import { m as r } from "./model-setter.js";
import { log as F } from "@arcgis/toolkit/log";
import { W as Le } from "./series-types.js";
import { ay as b, bl as _e, be as Ce, aJ as Be, ar as Me, bF as x, av as v, aw as w } from "./index.js";
import { j as l } from "./index4.js";
import { M as Fe, a as be } from "./model-with-y-guides.js";
var xe = Object.defineProperty, ve = Object.getOwnPropertyDescriptor, n = (c, e, t, s) => {
  for (var o = ve(e, t), a = c.length - 1, h; a >= 0; a--)
    (h = c[a]) && (o = h(e, t, o) || o);
  return o && xe(e, t, o), o;
};
class i extends A {
  constructor() {
    super(...arguments), this._chartType = Le.BoxPlotSeries, this._category = b, this._splitByField = "", this._numericFields = [];
  }
  async setup(e) {
    await this.loadConfigFromLayer(e), this.setupLocalProps(), e.config !== void 0 && await this.updateSeriesFromLayerFilter(), typeof this.resolveSetup == "function" && this.resolveSetup();
  }
  // ---------------------------------------------------------------------------------------------
  // #region Data Props
  // ---------------------------------------------------------------------------------------------
  /**
   * The category field for the box plot.
   */
  get category() {
    return this._category;
  }
  set category(e) {
    const t = e.trim() === "";
    this._category = t ? b : e, this.dirtyAsyncSetters.push("category"), this.processSeriesRebuild();
  }
  /**
   * The numeric fields array for the box plot.
   */
  get numericFields() {
    return this._numericFields;
  }
  set numericFields(e) {
    const { layerFieldsInfo: t } = this.layerInfo;
    typeof e == "string" && (e = [e]), H(t, e) ? (this._numericFields = e, this.dirtyAsyncSetters.push("numericFields"), $(this._category, this._numericFields) && (this._splitByField = "", this.dirtyAsyncSetters.push("splitByField")), this.processSeriesRebuild()) : F("error", "box-plot-model", "Invalid `numericFields`");
  }
  /**
   * The split-by field for the box plot.
   */
  get splitByField() {
    return this._splitByField;
  }
  set splitByField(e) {
    Q(this.layerInfo.layerFieldsInfo, e) ? (this._splitByField = e, this.dirtyAsyncSetters.push("splitByField"), this.processSeriesRebuild()) : F("error", "box-plot-model", "Invalid `splitByField`");
  }
  /**
   * Whether outliers are shown.
   */
  get showOutliers() {
    return ee(this._config);
  }
  set showOutliers(e) {
    te({
      newShowOutliers: e,
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings
    }), this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange"));
  }
  /**
   * Whether values are standardized as z-scores.
   */
  get standardizeValues() {
    return se(this._config);
  }
  set standardizeValues(e) {
    ie({
      newStandardizeValues: e,
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings
    }), this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange"));
  }
  /**
   * Gets the sort order of the box plot data.
   */
  getSortOrder() {
    return y(this._config);
  }
  setSortOrder(e, t) {
    p(e, this._config, t);
  }
  /**
   * Gets the list of the category values for the chart.
   */
  async getCustomSortValues() {
    return await u(this._config, this.layerInfo);
  }
  async setDataFilters(e) {
    super.setDataFilters(e);
    const t = y(this._config);
    if (t === P.customSort) {
      this.startAsyncUpdate();
      const s = await u(this._config, this.layerInfo);
      p(t, this._config, s), this.finishAsyncUpdate();
    }
    this.dispatchEvent(new CustomEvent("arcgisChartsModelDataChange"));
  }
  /**
   * The list of category values for custom sort.
   */
  get orderByList() {
    return m(this._config);
  }
  set orderByList(e) {
    S(e, this._config);
  }
  /**
   * Updates the series based on the layer filter.
   */
  async updateSeriesFromLayerFilter() {
    this.dirtyAsyncSetters.push("updateSeriesFromLayerFilter"), await this.processSeriesRebuild();
  }
  // ---------------------------------------------------------------------------------------------
  // #region  Series Props
  // ---------------------------------------------------------------------------------------------
  /**
   * Whether mean lines are shown.
   */
  get showMeanLines() {
    return oe(this._config);
  }
  set showMeanLines(e) {
    const { previousMeanLinesBoxColor: t } = re({
      newShowMeanLines: e,
      config: this._config,
      commonStrings: this.commonStrings,
      previousMeanLinesBoxColor: this.previousMeanLinesBoxColor
    });
    this.previousMeanLinesBoxColor = t;
  }
  /**
   * The color of the boxes when showing mean lines.
   *
   * Only applicable when mean lines are shown.
   */
  get meanLinesBoxColor() {
    return ne(this._config);
  }
  set meanLinesBoxColor(e) {
    L(e, this._config);
  }
  // ---------------------------------------------------------------------------------------------
  // #region Protected Methods
  // ---------------------------------------------------------------------------------------------
  /**
   * Sets up the local props based on the current config.
   * @private
   */
  setupLocalProps() {
    this._category = ae(this._config), this._splitByField = le(this._config), this._numericFields = he(this._config);
  }
  /** @private */
  generateDefaultConfig() {
    const e = _e(this.commonStrings);
    return e.series = [Ce(this.commonStrings)], e;
  }
  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 !ce(this.category, this.numericFields, this.splitByField);
  }
  /**
   * 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 = [], _(this.getBuildNewSeriesProps());
      else if (!this.asyncConfigUpdateInProgress) {
        for (this.startAsyncUpdate(); this.dirtyAsyncSetters.length > 0; )
          this.dirtyAsyncSetters = [], await _(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 {
      category: this.category,
      splitByField: this.splitByField,
      numericFields: this.numericFields,
      config: this._config,
      layerInfo: this.layerInfo,
      commonStrings: this.commonStrings
    };
  }
  // #endregion
  // ---------------------------------------------------------------------------------------------
  // #region Series Methods
  // ---------------------------------------------------------------------------------------------
  /**
   * 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 de(e, this._config);
  }
  setSeriesName(e, t) {
    C(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 ge(e, this._config);
  }
  setSeriesColor(e, t) {
    B(e, t, this._config);
  }
  /**
   * Gets the line style of the series based on the series index. If the index is out of bounds, it will return `undefined`.
   *
   * **Note**: Only applicable when mean lines are shown.
   *
   * @param seriesIndex - The index of the series.
   */
  getSeriesLineStyle(e) {
    const t = this._config.series[e];
    if (this.showMeanLines === !0 && l(t))
      return q(t);
  }
  setSeriesLineStyle(e, t) {
    const s = this._config.series[t];
    this.showMeanLines === !0 && l(s) && g(e, s);
  }
  /**
   * Gets the line width of the series based on the series index. If the index is out of bounds, it will return `undefined`.
   *
   * **Note**: Only applicable when mean lines are shown.
   *
   * @param seriesIndex - The index of the series.
   */
  getSeriesLineWidth(e) {
    const t = this._config.series[e];
    if (this.showMeanLines === !0 && l(t))
      return J(t);
  }
  setSeriesLineWidth(e, t) {
    const s = this._config.series[t];
    this.showMeanLines === !0 && l(s) && f(e, s);
  }
  /**
   * The order of the series array by returning an array of series ids.
   */
  get orderOfSeries() {
    return fe(this._config);
  }
  set orderOfSeries(e) {
    ye(e, this._config);
  }
  moveSeries(e, t) {
    pe(e, t, this._config);
  }
  /**
   * The index of the box plot series in the series array in config object when show mean is true.
   *
   * **Note**: There is only one box plot series when show mean is `true`. The function returns `-1` if show mean is `false`.
   */
  get boxPlotSeriesIndexWhenMeanLinesShown() {
    return ue(this._config);
  }
  moveCustomSortValuesInOrderByList(e, t) {
    const s = m(this._config), [o] = s.splice(e, 1);
    s.splice(t, 0, o), S(s, this._config);
  }
  // ---------------------------------------------------------------------------------------------
  // #region General Methods
  // ---------------------------------------------------------------------------------------------
  /**
   * Generate the proper chart title given the current config.
   */
  generateChartTitle() {
    return me(this._config, this.layerInfo, this.commonStrings);
  }
  /**
   * Generate the proper y-axis title given the current config.
   */
  generateYAxisTitle() {
    return Se(this._config, this.layerInfo, this.commonStrings);
  }
  resetSeriesStyling() {
    for (let e = this.showMeanLines === !0 ? 1 : 0; e < this.seriesLength; e += 1) {
      const t = this.getSeriesId(e), s = Be({
        colorIndex: e,
        opacity: Me
      }), o = this._config.series[e];
      B(s, e, this._config);
      const a = M({
        series: o,
        splitByField: this._splitByField,
        layerInfo: this.layerInfo,
        commonStrings: this.commonStrings
      });
      C(a ?? t, e, this._config), this.showMeanLines === !0 && l(o) && (g(v, o), f(w, o));
    }
    this.showMeanLines === !0 && L(x, this._config), this._config.colorMatch = !0;
  }
  /**
   * Returns whether the series styling is able to be reset.
   */
  resetAvailable() {
    const e = !this.colorMatch, t = this.showMeanLines === !0 && this.meanLinesBoxColor !== x;
    let s = !1, o = !1;
    if (!e)
      for (let a = this.showMeanLines === !0 ? 1 : 0; a < this.seriesLength; a += 1) {
        const h = this._config.series[a], d = M({
          series: h,
          splitByField: this._splitByField,
          layerInfo: this.layerInfo,
          commonStrings: this.commonStrings
        }), W = this.getSeriesName(a);
        if (d !== void 0 && d !== W) {
          s = !0;
          break;
        }
        if (this.showMeanLines === !0 && l(h)) {
          const I = this.getSeriesLineStyle(a), O = this.getSeriesLineWidth(a);
          if (I !== v || O !== w) {
            o = !0;
            break;
          }
        }
      }
    return e || t || s || o;
  }
}
n([
  r
], i.prototype, "showOutliers");
n([
  r
], i.prototype, "standardizeValues");
n([
  r
], i.prototype, "setSortOrder");
n([
  r
], i.prototype, "setDataFilters");
n([
  r
], i.prototype, "orderByList");
n([
  r
], i.prototype, "showMeanLines");
n([
  r
], i.prototype, "meanLinesBoxColor");
n([
  r
], i.prototype, "resetConfig");
n([
  r
], i.prototype, "setSeriesName");
n([
  r
], i.prototype, "setSeriesColor");
n([
  r
], i.prototype, "setSeriesLineStyle");
n([
  r
], i.prototype, "setSeriesLineWidth");
n([
  r
], i.prototype, "orderOfSeries");
n([
  r
], i.prototype, "moveSeries");
n([
  r
], i.prototype, "moveCustomSortValuesInOrderByList");
n([
  r
], i.prototype, "resetSeriesStyling");
const we = [
  Fe,
  X,
  j,
  K,
  k,
  Y,
  G,
  be,
  R,
  N,
  U,
  E,
  D,
  T,
  V,
  z
];
Z(i, we);
export {
  i as BoxPlotModel
};
