/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as p } from "../../chunks/runtime.js";
import { cloneDeep as r } from "lodash-es";
import { d as g } from "../../chunks/default.js";
import { g as l, H as m } from "../../chunks/resource.js";
import { u as f } from "../../chunks/useT9n.js";
import { css as A, html as a } from "lit";
import { createRef as x, ref as C } from "lit/directives/ref.js";
import { LitElement as v, renderElement as b, safeClassMap as c } from "@arcgis/lumina";
import { G as s } from "../../chunks/common.js";
import { p as h, q as u } from "../../chunks/type-guards.js";
import "@arcgis/toolkit/type";
import "../../chunks/series-types.js";
const P = A`:host{display:flex;flex-direction:column;width:var(--arcgis-charts-config-calcite-block-default-width);margin:var(--arcgis-charts-config-calcite-block-default-margin)}.add-guide{display:flex;justify-content:center;padding:.5rem}.add-guide-btn{width:100%}.no-guides{border-radius:2rem}.no-messages{text-align:center}.guide-icon{padding-left:.5rem;padding-top:.5rem;width:1.5rem;height:1.5rem}`;
class $ extends v {
  constructor() {
    super(...arguments), this._messages = f(), this.addGuideButton = x(), this.guideTypePopover = null, this.addXGuide = (e) => {
      if (h(this.model)) {
        const t = this.getUniqueName(this._messages.xAxisGuide ?? ""), i = this.model.getXGuidesLength() ?? 0;
        this.model.addXAxisGuide(t, i), this.model.setGuideStyle(r(g.style), i, s.X), this.latestGuidePosition = l(i, s.X, this.model), this.togglePopOver(e);
      }
    }, this.addYGuide = (e) => {
      if (u(this.model)) {
        const t = this.getUniqueName(this._messages.yAxisGuide ?? ""), i = this.model.getYGuidesLength() ?? 0;
        this.model.addYAxisGuide(t, i), this.model.setGuideStyle(r(g.style), i, s.Y), this.latestGuidePosition = l(i, s.Y, this.model), this.togglePopOver(e);
      }
    }, this.togglePopOver = (e) => {
      e.stopImmediatePropagation(), this.enableXAxisGuide && this.enableYAxisGuide && (this.showAddButton = !this.showAddButton);
    }, this.onModelConfigChange = () => {
      this.requestUpdate();
    }, this.closeAddPopOver = (e) => {
      e.stopImmediatePropagation(), this.showAddButton = !0;
    }, this.showAddButton = !0, this.latestGuidePosition = 0, this.open = !1;
  }
  static {
    this.properties = { showAddButton: 16, latestGuidePosition: 16, messageOverrides: 0, model: 0, timeZone: 1, calculatedMinMaxBounds: 0, enableXAxisGuide: 5, enableYAxisGuide: 5, open: 5 };
  }
  static {
    this.styles = P;
  }
  load() {
    this.modelChange(this.model);
  }
  willUpdate(e) {
    e.has("model") && this.modelChange(this.model, e.get("model"));
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.model.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  modelChange(e, t) {
    t?.removeEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange), e?.addEventListener("arcgisChartsModelConfigChange", this.onModelConfigChange);
  }
  getAllGuidesLength() {
    const e = h(this.model) ? this.model.getXGuidesLength() ?? 0 : 0, t = u(this.model) ? this.model.getYGuidesLength() ?? 0 : 0;
    return e + t;
  }
  getUniqueName(e, t = 1) {
    const i = `${e} ${t}`;
    let d = !1;
    if (h(this.model)) {
      const o = this.model.getXGuidesLength() ?? 0;
      for (let n = 0; n < o && !d; n += 1)
        this.model.getGuideName(n, s.X) === i && (d = !0);
    }
    if (u(this.model)) {
      const o = this.model.getYGuidesLength() ?? 0;
      for (let n = 0; n < o && !d; n += 1)
        this.model.getGuideName(n, s.Y) === i && (d = !0);
    }
    return d ? this.getUniqueName(e, t + 1) : i;
  }
  setChangedGuide(e) {
    const { index: t, orientation: i, isDelete: d } = e.detail, o = l(t, i, this.model);
    d && o !== void 0 && o <= this.latestGuidePosition && this.latestGuidePosition > 0 && (this.latestGuidePosition -= 1);
  }
  setupGuidePopover(e) {
    e.stopImmediatePropagation(), this.togglePopOver(e);
  }
  renderGuideTypePopover() {
    const e = this.getAllGuidesLength();
    this.guideTypePopover = b(a`<arcgis-charts-config-guides-type-popover .enableXAxisGuide=${this.enableXAxisGuide} .enableYAxisGuide=${this.enableYAxisGuide} .referenceElement=${this.addGuideButton.value} .placement=${e === 0 ? "bottom" : "top"} .open=${!this.showAddButton} @arcgisChartsAddXAxisGuide=${this.addXGuide} @arcgisChartsAddYAxisGuide=${this.addYGuide} @arcgisChartsConfigPopoverClose=${this.closeAddPopOver}></arcgis-charts-config-guides-type-popover>`), !this.showAddButton && !document.body.contains(this.guideTypePopover) && this.guideTypePopover !== null && document.body.appendChild(this.guideTypePopover);
  }
  renderGuide(e, t) {
    let i;
    return (this.enableXAxisGuide && t === s.X || this.enableYAxisGuide && t === s.Y) && (i = a`<arcgis-charts-config-guide .model=${this.model} .timeZone=${this.timeZone} .calculatedMinMaxBounds=${this.calculatedMinMaxBounds} .index=${e} .orientation=${t} .latestGuidePosition=${this.latestGuidePosition} @arcgisChartsConfigGuideChange=${this.setChangedGuide}></arcgis-charts-config-guide>`), i;
  }
  renderGuides() {
    const e = [];
    if (h(this.model)) {
      const t = this.model.getXGuidesLength() ?? 0;
      for (let i = 0; i < t; i += 1)
        e.push(this.renderGuide(i, s.X));
    }
    if (u(this.model)) {
      const t = this.model.getYGuidesLength() ?? 0;
      for (let i = 0; i < t; i += 1)
        e.push(this.renderGuide(i, s.Y));
    }
    return e;
  }
  renderAddGuideButton(e) {
    return a`<calcite-fab slot=fab appearance=outline-fill kind=neutral text-enabled .text=${this._messages.addGuide} .label=${this._messages.addGuide} @click=${e} scale=s></calcite-fab>`;
  }
  render() {
    const t = this.getAllGuidesLength() > 0, i = this.showAddButton && this.enableXAxisGuide && this.enableYAxisGuide, d = this.enableXAxisGuide && !this.enableYAxisGuide, o = !this.enableXAxisGuide && this.enableYAxisGuide;
    return a`${this.renderGuideTypePopover()}${t ? this.renderGuides() : a`<div class=${c(m.noMessages)}>${this._messages.noGuidesMsg}</div>`}<div class=${c(m.addGuide)} ${C(this.addGuideButton)}>${i && this.renderAddGuideButton(this.setupGuidePopover) || ""}${d && this.renderAddGuideButton(this.addXGuide) || ""}${o && this.renderAddGuideButton(this.addYGuide) || ""}</div>`;
  }
}
p("arcgis-charts-config-guides", $);
export {
  $ as ArcgisChartsConfigGuides
};
