/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as f } from "../../chunks/runtime.js";
import { keyed as C } from "lit/directives/keyed.js";
import "@arcgis/toolkit/intl";
import "lodash-es";
import "luxon";
import "d3-array";
import "@arcgis/toolkit/type";
import { a as u } from "../../chunks/series-types.js";
import { d as c, d3 as m, bR as b } from "../../chunks/index.js";
import "@arcgis/core/Graphic.js";
import "@arcgis/core/renderers/support/jsonUtils.js";
import "@arcgis/core/symbols/support/symbolUtils.js";
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 "@arcgis/toolkit/dom";
import { r as v } from "../../chunks/popover-ui-utils.js";
import { a as g } from "../../chunks/common.js";
import { P as s } from "../../chunks/interfaces.js";
import { u as y } from "../../chunks/useT9n.js";
import { css as F, html as a } from "lit";
import { createRef as n, ref as d } from "lit/directives/ref.js";
import { LitElement as $, createEvent as p, renderElement as P, safeClassMap as o } from "@arcgis/lumina";
import { x as E } from "../../chunks/data.js";
const l = {
  label: "label",
  fieldList: "field-list",
  fab: "fab"
}, k = F`:host{display:flex;flex-direction:column;width:var(--arcgis-charts-config-calcite-block-default-width);margin:var(--arcgis-charts-config-calcite-block-default-margin);margin-top:var(--arcgis-charts-top-section-margin)}.label,.disabled-label{display:flex;margin-left:var(--arcgis-charts-config-margin-minor);margin-right:var(--arcgis-charts-config-margin-minor);margin-bottom:var(--arcgis-charts-config-margin-minor);justify-content:space-between}.field-list{max-height:50vh;overflow-y:auto}.disabled-label{pointer-events:none}.fab{display:flex;margin-left:var(--arcgis-charts-config-margin-minor);margin-right:var(--arcgis-charts-config-margin-minor);margin-bottom:1.5rem;justify-content:center}`;
class w extends $ {
  constructor() {
    super(...arguments), this._messages = y(), this.pieChartPopover = null, this.sortOrderElement = n(), this.categoryElement = n(), this.numericFieldsElement = n(), this.onPieChartDataChange = (e) => {
      try {
        if (e.detail !== null && e.detail?.value !== void 0) {
          const { contentKind: t } = e.detail, i = [...e.detail.value];
          switch (t) {
            case s.aggregation:
              this.model.aggregationType = i[0] !== u.NoAggregation ? i[0] : "";
              break;
            case s.category: {
              const r = i[0]?.trim() || "";
              this.updateMode(r);
              const h = this.model.numericFields;
              h.length > 1 && r !== c && (this.model.numericFields = [h[0]]), this.model.category = r, this.model.colorMatch = !0;
              break;
            }
            case s.numericFields:
              i.length > 1 && (this.updateMode(c), this.model.category = c), this.model.numericFields = i;
              break;
            case s.sortOrder:
              this.model.setSortOrder(i[0]);
              break;
            default:
              break;
          }
        }
      } catch (t) {
        throw t instanceof Error && this.arcgisChartsConfigDataChangeError.emit({ error: t, model: this.model }), t;
      }
      this.requestUpdate();
    }, this.onModelConfigChange = () => {
      this.requestUpdate();
    }, this.popoverClose = (e) => {
      this.pieChartPopover === e?.target && this.removePieChartDataPopover();
    }, this.layerFieldsInfo = [], this.arcgisChartsConfigDataChangeError = p(), this.arcgisChartsConfigPieChartDataNumericFieldValidate = p();
  }
  static {
    this.properties = { messageOverrides: 0, model: 0, layerFieldsInfo: 0 };
  }
  static {
    this.styles = k;
  }
  load() {
    this.modelChange(this.model), this.onModelConfigChange();
  }
  willUpdate(e) {
    e.has("model") && this.modelChange(this.model, e.get("model"));
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.removePieChartDataPopover(), this.model.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  modelChange(e, t) {
    t?.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange), e?.addEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  buildNumericFieldItem(e) {
    return C(e, a`<calcite-list-item .label=${m(this.layerFieldsInfo, e)} .value=${e} closable @calciteListItemClose=${this.onRemoveNumericField}></calcite-list-item>`);
  }
  onSortOrderSelect(e) {
    this.contentKind = s.sortOrder, this.setupPopover(e);
  }
  onCategorySelect(e) {
    this.contentKind = s.category, this.setupPopover(e);
  }
  onAddNumericFields(e) {
    this.contentKind = s.numericFields, this.setupPopover(e);
  }
  onRemoveNumericField(e) {
    const t = e.target.value;
    this.model.numericFields = this.model.numericFields.filter((i) => i !== t);
  }
  onShowDataLabelsChange(e) {
    this.model.dataLabelsVisibility = e.target.checked;
  }
  updateMode(e) {
    const t = e !== c && e.trim() !== "";
    this.model.mode = t ? g.Category : g.Fields;
  }
  setupPopover(e) {
    e.stopImmediatePropagation(), this.removePieChartDataPopover();
    let t, i, r;
    switch (this.contentKind) {
      case s.category:
        t = this.categoryElement.value, i = this._messages.categoryHeader, r = this.model.category;
        break;
      case s.numericFields:
        t = this.numericFieldsElement.value, i = this._messages.numericFieldsMultipleHeader, r = this.model.numericFields;
        break;
      case s.sortOrder:
        t = this.sortOrderElement.value, i = this._messages.sortOrderHeader, r = this.model.getSortOrder();
        break;
    }
    this.pieChartPopover = P(a`<arcgis-charts-config-pie-chart-popover .headingTitle=${i} .referenceElement=${t} open .contentKind=${this.contentKind} .layerFieldsInfo=${this.layerFieldsInfo} .selectedContent=${r} @arcgisChartsConfigPieChartDataPopoverChange=${this.onPieChartDataChange} @arcgisChartsConfigPopoverClose=${this.popoverClose}></arcgis-charts-config-pie-chart-popover>`), !document.body.contains(this.pieChartPopover) && this.pieChartPopover !== null && document.body.appendChild(this.pieChartPopover);
  }
  getCorrectedCategoryValue() {
    return E({
      fieldName: this.model.category,
      fieldList: this.layerFieldsInfo
    }) ? "" : m(this.layerFieldsInfo, this.model.category);
  }
  removePieChartDataPopover() {
    this.pieChartPopover !== null && (this.pieChartPopover.open = !1, this.pieChartPopover = v(this.pieChartPopover));
  }
  renderSorting() {
    return a`<arcgis-charts-config-field-select .label=${this._messages[this.model.getSortOrder()]} @click=${this.onSortOrderSelect} ${d(this.sortOrderElement)}></arcgis-charts-config-field-select>`;
  }
  renderCategory() {
    const e = this.getCorrectedCategoryValue();
    return a`<arcgis-charts-config-field-select .label=${e} @click=${this.onCategorySelect} ${d(this.categoryElement)}></arcgis-charts-config-field-select>`;
  }
  renderNumericFields(e) {
    const t = b(this.model.layer).idField, i = [];
    for (const r of e)
      r !== t && i.push(this.buildNumericFieldItem(r));
    return a`<calcite-list label class=${o(l.fieldList)} ${d(this.numericFieldsElement)}>${i}</calcite-list>`;
  }
  render() {
    const e = this.renderCategory(), t = this.renderSorting(), i = this.renderNumericFields([...this.model.numericFields]);
    return a`<calcite-label class=${o(l.label)}>${this._messages.category}${e}</calcite-label>${[
      a`<calcite-label class=${o(l.label)}>${this._messages.numericFields}${i}</calcite-label>`,
      a`<div class=${o(l.fab)}><calcite-fab appearance=outline-fill kind=neutral scale=m .label=${this._messages.selectNumericFields} text-enabled .text=${this._messages.selectNumericFields} icon=plus @click=${this.onAddNumericFields}></calcite-fab></div>`
    ]}<calcite-label class=${o(l.label)} layout=inline-space-between>${this._messages.showDataLabels}<calcite-switch .checked=${this.model.dataLabelsVisibility} @calciteSwitchChange=${this.onShowDataLabelsChange}></calcite-switch></calcite-label><calcite-label class=${o(l.label)}>${this._messages.sortOrder}${t}</calcite-label>`;
  }
}
f("arcgis-charts-config-pie-chart-data", w);
export {
  w as ArcgisChartsConfigPieChartData
};
