/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as b } from "../../chunks/runtime.js";
import "@arcgis/toolkit/type";
import { W as l } from "../../chunks/series-types.js";
import { debounce as m } from "lodash-es";
import { g as f, s as N } from "../../chunks/store.js";
import { i as c, m as M, U as a, a as x, s as g } from "../../chunks/chart-ui-utils2.js";
import { A as B } from "../../chunks/axis-model-wrapper.js";
import { u as p } from "../../chunks/useT9n.js";
import { css as v, html as s } from "lit";
import { createRef as r, ref as h } from "lit/directives/ref.js";
import { LitElement as C, renderElement as I, safeClassMap as d } from "@arcgis/lumina";
const o = {
  linkContainer: "link-container",
  label: "label",
  mb2: "mb-2"
}, E = v`.hide{visibility:hidden}.am5-html-container{z-index:101!important}.am5-layer-1000{z-index:1000!important}.am5-layer-30{z-index:100!important}.show{display:block}.notifyPanel{flex:0 1 auto}.disable-interactions{pointer-events:none}.dim-text{color:var(--arcgis-charts-dim-text)}calcite-block{width:var(--arcgis-charts-config-calcite-block-width, var(--arcgis-charts-config-calcite-block-default-width));height:var(--arcgis-charts-config-calcite-block-height, var(--arcgis-charts-config-calcite-block-default-height));border:0px;-webkit-box-shadow:0 0 0 0;box-shadow:0 0}.label{display:flex;justify-content:space-between}.mb-2{margin-bottom:var(--arcgis-charts-config-margin-minor)}.link-container{display:flex;justify-content:flex-end;margin-top:-.75rem}`;
class L extends C {
  constructor() {
    super(...arguments), this._messages = p(), this.commonStrings = p({ name: "common", blocking: !0 }), this.axisModel = new B(), this.minBoundInputElement = r(), this.maxBoundInputElement = r(), this.showLogScaleSwitch = r(), this.defaultMinBound = Number.NaN, this.defaultMaxBound = Number.NaN, this.onMaxBoundsChange = () => {
      const t = this.maxBoundInputElement.value?.value;
      let e = this.maxBoundInputElement.value?.status;
      if (t !== void 0 && t !== "") {
        const n = this.maxBoundInputElement.value?.min === void 0 ? this.axisModel.getMinBound() : Math.max(this.maxBoundInputElement.value.min, this.axisModel.getMinBound()), i = Number.parseFloat(t);
        if (i !== this.axisModel.getMinBound() && c(i, n, this.maxBoundInputElement.value?.max)) {
          if (i !== this.axisModel.getMaxBound()) {
            this.axisModel.setMaxBound(i);
            const u = this.getAxisBoundsFromStateStore();
            u && (u.currMaxBound = i);
          }
          e = a.IDLE;
        } else
          e = a.INVALID;
      } else
        e = a.IDLE;
      this.maxBoundInputElement.value && M({
        inputElement: this.maxBoundInputElement.value,
        status: e
      }), e !== a.INVALID && this.minBoundInputElement.value?.status === a.INVALID && this.onMinBoundsChange(), this.requestUpdate();
    }, this.onMaxBoundsChangeDebounced = m(this.onMaxBoundsChange, x.DebounceTimer), this.onMinBoundsChange = () => {
      const t = this.minBoundInputElement.value?.value;
      let e = this.minBoundInputElement.value?.status;
      if (t !== void 0 && t !== "") {
        const n = this.minBoundInputElement.value?.max === void 0 ? this.axisModel.getMaxBound() : Math.min(this.minBoundInputElement.value.max, this.axisModel.getMaxBound()), i = Number.parseFloat(t);
        if (i !== this.axisModel.getMaxBound() && c(i, this.minBoundInputElement.value?.min, n)) {
          if (i !== this.axisModel.getMinBound()) {
            this.axisModel.setMinBound(i);
            const u = this.getAxisBoundsFromStateStore();
            u && (u.currMinBound = i);
          }
          e = a.IDLE;
        } else
          e = a.INVALID;
      } else
        e = a.IDLE;
      this.minBoundInputElement.value && M({
        inputElement: this.minBoundInputElement.value,
        status: e
      }), e !== a.INVALID && this.maxBoundInputElement.value?.status === a.INVALID && this.onMaxBoundsChange(), this.updateEnableLogarithmic(this.axisModel.getLogarithmic() && (this.dataMinValue ?? Number.NaN) > 0), this.requestUpdate();
    }, this.onMinBoundsChangeDebounced = m(this.onMinBoundsChange, x.DebounceTimer), this.onModelConfigChange = () => {
      this.requestUpdate();
    }, this.disableMaxBoundResetBtn = !0, this.disableMinBoundResetBtn = !0, this.isXAxis = !1, this.axisTitle = "";
  }
  static {
    this.properties = { disableMaxBoundResetBtn: 16, disableMinBoundResetBtn: 16, messageOverrides: 0, model: 0, isXAxis: 5, axisTitle: 1, maxBoundMaxLimit: 9, maxBoundMinLimit: 9, minBoundMinLimit: 9, minBoundMaxLimit: 9, showMaxBoundOption: 5, showMinBoundOption: 5, showEnableLogOption: 5, dataMaxValue: 9, dataMinValue: 9, initialMaxBound: 9, initialMinBound: 9 };
  }
  static {
    this.styles = E;
  }
  load() {
    this.modelChange(this.model), this.dataMinValue !== void 0 && this.dataMinValueChange(this.dataMinValue, Number.NaN), this.dataMaxValue !== void 0 && this.dataMaxValueChange(this.dataMaxValue, Number.NaN);
  }
  willUpdate(t) {
    t.has("model") && this.modelChange(this.model, t.get("model")), t.has("dataMinValue") && this.dataMinValueChange(this.dataMinValue ?? Number.NaN, t.get("dataMinValue")), t.has("dataMaxValue") && this.dataMaxValueChange(this.dataMaxValue ?? Number.NaN, t.get("dataMaxValue")), t.has("initialMaxBound") && this.initialMaxBoundChange(), t.has("initialMinBound") && this.initialMinBoundChange();
  }
  updated() {
    this.disableResetButtons();
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.model.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  modelChange(t, e) {
    this.axisModel = new B(this.model, this.isXAxis), e?.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange), t?.addEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  dataMinValueChange(t, e) {
    Number.isNaN(e) && !Number.isNaN(t) && (this.defaultMinBound = t);
  }
  dataMaxValueChange(t, e) {
    Number.isNaN(e) && !Number.isNaN(t) && (this.defaultMaxBound = t);
  }
  initialMaxBoundChange() {
    Number.isNaN(this.axisModel.getMaxBound()) || this.axisModel.setMaxBound(this.axisModel.getMaxBound());
  }
  initialMinBoundChange() {
    Number.isNaN(this.axisModel.getMinBound()) || this.axisModel.setMinBound(this.axisModel.getMinBound());
  }
  getAxisBoundsFromStateStore() {
    let t;
    const e = N.chartAxesBoundDataMap, n = this.model.chartId;
    if (n) {
      const i = f({
        chartId: n,
        isXAxis: this.isXAxis,
        isLogarithmic: this.axisModel.getLogarithmic()
      });
      t = e[n]?.[i];
    }
    return t;
  }
  disableResetButtons() {
    const t = this.getAxisBoundsFromStateStore();
    t && (this.disableMaxBoundResetBtn = t.defaultMaxBound === t.currMaxBound, this.disableMinBoundResetBtn = t.defaultMinBound === t.currMinBound);
  }
  onShowLogScaleMouseEnter() {
    this.showLogScaleTooltip?.remove(), this.showLogScaleSwitch.value?.disabled && (this.showLogScaleTooltip = I(s`<calcite-tooltip .referenceElement=${this.showLogScaleSwitch.value ?? document.body} open>${this.commonStrings.utilsStrings.tooltips?.logDisabled ?? ""}</calcite-tooltip>`), document.body.appendChild(this.showLogScaleTooltip));
  }
  onShowLogScaleMouseLeave() {
    this.showLogScaleTooltip?.remove();
  }
  onCategoryFormatChange(t) {
    this.axisModel.setValueFormat(t.target.categoryFormat);
  }
  updateEnableLogarithmic(t) {
    t !== this.axisModel.getLogarithmic() && (this.axisModel.setLogarithmic(t), this.requestUpdate());
  }
  onNumberFormatChange(t) {
    this.axisModel.setValueFormat({
      type: l.NumberAxisFormat,
      intlOptions: t.detail
    }), this.requestUpdate();
  }
  onRevertMaxBoundsToDefault() {
    if (this.maxBoundInputElement.value?.status === a.INVALID || this.axisModel.getMaxBound() !== this.defaultMaxBound) {
      const t = this.getAxisBoundsFromStateStore();
      this.axisModel.getMinBound() > this.defaultMaxBound && this.axisModel.setMinBound(t?.defaultMinBound ?? Number.NaN), this.axisModel.setMaxBound(t?.defaultMaxBound ?? Number.NaN), this.requestUpdate();
    }
  }
  onRevertMinBoundsToDefault() {
    (this.minBoundInputElement.value?.status === a.INVALID || this.axisModel.getMinBound() !== this.defaultMinBound) && (this.axisModel.getMaxBound() < this.defaultMinBound && this.axisModel.setMaxBound(Number.NaN), this.axisModel.setMinBound(Number.NaN), this.requestUpdate());
  }
  enableLogScale(t) {
    const { checked: e } = t.target;
    this.axisModel.setLogarithmic(e && (this.dataMinValue ?? Number.NaN) > 0), this.requestUpdate();
  }
  render() {
    return s`<calcite-block .hidden=${this.axisModel.getValueFormat()?.type === l.DateAxisFormat} expanded .heading=${this.axisTitle}>${this.renderAxis()}</calcite-block>`;
  }
  renderAxis() {
    let t = null;
    const e = { ...this.axisModel.getValueFormat() };
    switch (this.axisModel.getValueFormat()?.type) {
      case l.CategoryAxisFormat:
        t = s`<arcgis-charts-config-category-format .categoryFormat=${e} @arcgisChartsConfigCategoryFormatChange=${this.onCategoryFormatChange}></arcgis-charts-config-category-format>`;
        break;
      case l.NumberAxisFormat:
        t = s`<div>${this.renderShowLogScale()}${this.renderMinBound()}${this.renderMaxBound()}<arcgis-charts-config-number-format .numberFormat=${{ ...e.intlOptions }} @arcgisChartsConfigNumberFormatChange=${this.onNumberFormatChange}></arcgis-charts-config-number-format></div>`;
        break;
      case l.DateAxisFormat:
        t = null;
        break;
    }
    return t;
  }
  renderMaxBound() {
    let t;
    if (!this.showMaxBoundOption)
      t = null;
    else {
      const e = g(this.axisModel.getMaxBound(), this.initialMaxBound);
      t = s`<div><calcite-label class=${d(o.label)}>${this._messages.maxBounds}<calcite-input type=number number-button-type=none .min=${this.maxBoundMinLimit ?? Number.MIN_SAFE_INTEGER} .max=${this.maxBoundMaxLimit ?? Number.MAX_SAFE_INTEGER} step=any .value=${e} @calciteInputInput=${this.onMaxBoundsChangeDebounced} ${h(this.maxBoundInputElement)}></calcite-input></calcite-label><div class=${d(o.linkContainer)}><calcite-button .disabled=${this.disableMaxBoundResetBtn} @click=${this.onRevertMaxBoundsToDefault} appearance=transparent icon-end=reset>${this._messages.revertToDefault}</calcite-button></div></div>`;
    }
    return t;
  }
  renderMinBound() {
    let t;
    if (!this.showMinBoundOption)
      t = null;
    else {
      const e = g(this.axisModel.getMinBound(), this.initialMinBound ?? void 0);
      t = s`<div><calcite-label class=${d(o.label)}>${this._messages.minBounds}<calcite-input type=number number-button-type=none .min=${this.minBoundMinLimit ?? Number.MIN_SAFE_INTEGER} .max=${this.minBoundMaxLimit ?? Number.MAX_SAFE_INTEGER} step=any .value=${e} @calciteInputInput=${this.onMinBoundsChangeDebounced} ${h(this.minBoundInputElement)}></calcite-input></calcite-label><div class=${d(o.linkContainer)}><calcite-button .disabled=${this.disableMinBoundResetBtn} appearance=transparent icon-end=reset @click=${this.onRevertMinBoundsToDefault}>${this._messages.revertToDefault}</calcite-button></div></div>`;
    }
    return t;
  }
  renderShowLogScale() {
    let t;
    return this.showEnableLogOption && (t = s`<calcite-label layout=inline-space-between class=${d({
      [o.label]: !0,
      [o.mb2]: !0
    })}>${this._messages.logScale}<div @mouseenter=${this.onShowLogScaleMouseEnter} @mouseleave=${this.onShowLogScaleMouseLeave}><calcite-switch .checked=${this.axisModel.getLogarithmic() && (this.dataMinValue ?? Number.NaN) > 0} .disabled=${(this.dataMinValue ?? Number.NaN) <= 0} @calciteSwitchChange=${this.enableLogScale} ${h(this.showLogScaleSwitch)}></calcite-switch></div></calcite-label>`), t;
  }
}
b("arcgis-charts-config-axis", L);
export {
  L as ArcgisChartsConfigAxis
};
