/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as C } from "../../chunks/runtime.js";
import "@arcgis/toolkit/intl";
import { isEqual as c, cloneDeep as d } from "lodash-es";
import "luxon";
import "d3-array";
import "@arcgis/toolkit/type";
import "../../chunks/series-types.js";
import { f as u } from "../../chunks/rest-js-object-literals.js";
import { bM as y, bO as I } from "../../chunks/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 { h as v } from "../../chunks/index4.js";
import "@arcgis/toolkit/dom";
import { LitElement as F, createEvent as f } from "@arcgis/lumina";
import { css as L, html as n } from "lit";
import { C as h } from "../../chunks/interfaces.js";
const S = L`:host{overflow-y:auto;overflow-x:hidden;margin-bottom:var(--arcgis-charts-intra-section-margin);margin-top:var(--arcgis-charts-intra-section-margin);height:100%}`;
class b extends F {
  constructor() {
    super(...arguments), this.onModelConfigChange = async (e) => {
      const o = e?.detail.config ?? this.model.config;
      this.layer === void 0 && await this.updateFetchLayerFieldsInfo(o);
    }, this.popoverPlacement = "leading", this.arcgisChartsConfigNoSeriesUpdate = f(), this.arcgisChartsConfigWebChartChange = f();
  }
  static {
    this.properties = { layerFieldsInfo: 16, model: 0, popoverPlacement: 1, seriesColor: 0, calculatedMinMaxBounds: 0, page: 1, layer: 0, usePopupTemplateFieldsInfo: 5, filterBySelection: 5, chartData: 0 };
  }
  static {
    this.styles = S;
  }
  async load() {
    this.seriesColor !== void 0 && this.seriesColorChange(this.seriesColor), await this.updateFetchLayerFieldsInfo(this.model?.config), this.modelChange(this.model);
  }
  willUpdate(e) {
    let i = !1;
    e.has("model") && this.modelChange(this.model, e.get("model")), e.has("layer") && this.layer !== void 0 && !c(e.get("layer"), this.layer) && (i = !0), e.has("usePopupTemplateFieldsInfo") && this.usePopupTemplateFieldsInfo !== e.get("usePopupTemplateFieldsInfo") && (i = !0), e.has("seriesColor") && this.seriesColorChange(this.seriesColor), this.layerFieldsInfo === void 0 && (i = !0), i && this.updateFetchLayerFieldsInfo();
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.model.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  modelChange(e, i) {
    i?.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange), e?.addEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  seriesColorChange(e) {
    const { slices: i, groupedSlices: o, groupOfSlices: r } = e?.get(0) ?? {};
    let t = [];
    this.filterBySelection === !0 ? t = this.model.slices?.map((a) => i?.find((s) => s.sliceId === a.sliceId) ?? o?.find((s) => s.sliceId === a.sliceId) ?? a) ?? [] : t = (i ?? []).concat(o ?? []), t.forEach((a, s) => {
      const p = this.model.slices?.find((g) => g.sliceId === t[s].sliceId);
      p && (t[s] = { ...t[s], ...p });
      const l = t[s].fillSymbol;
      l !== void 0 && !c(l?.outline?.color, l?.color) && (l.outline = {
        ...l.outline ?? {},
        type: u.SLS,
        color: l.color
      });
    });
    const m = (o ?? []).map((a) => a.sliceId);
    o !== void 0 && !c(m, this.model.groupedSliceIds) && (this.model.groupedSliceIds = d(m)), c(t, this.model.slices) || (this.model.slices = d(t)), r !== void 0 && !c(r, this.model.sliceGrouping) && (this.model.sliceGrouping = d(r));
  }
  async updateFetchLayerFieldsInfo(e) {
    const i = e ?? this.model.config;
    let o = this.layer;
    if (!o) {
      const r = v(i) ? i.iLayer : void 0;
      r && (o = await y(r));
    }
    o && (this.layerFieldsInfo = await I(o, this.usePopupTemplateFieldsInfo));
  }
  render() {
    let e;
    switch (this.page) {
      case h.Data:
        e = n`<arcgis-charts-config-pie-chart-data .model=${this.model} .layerFieldsInfo=${this.layerFieldsInfo}></arcgis-charts-config-pie-chart-data>`;
        break;
      case h.Slices:
        e = n`<arcgis-charts-config-pie-chart-slices .model=${this.model} .chartData=${this.chartData} .popoverPlacement=${this.popoverPlacement}></arcgis-charts-config-pie-chart-slices>`;
        break;
      case h.Format:
        e = n`<arcgis-charts-config-format .model=${this.model}></arcgis-charts-config-format>`;
        break;
      case h.General:
        e = n`<arcgis-charts-config-general .model=${this.model}></arcgis-charts-config-general>`;
        break;
      default:
        e = "";
        break;
    }
    return e;
  }
}
C("arcgis-charts-config-pie-chart", b);
export {
  b as ArcgisChartsConfigPieChart
};
