import { c } from "../../chunks/runtime.js";
import { repeat as l } from "lit/directives/repeat.js";
import { LitElement as u, createEvent as n } from "@arcgis/lumina";
import { u as d } from "../../chunks/useT9n.js";
import { css as h, html as s } from "lit";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.7 */
const g = h`:host{min-width:300px}.arcgis-utility-network-trace-inputs calcite-block{--calcite-block-border-color: transparent;margin:0;background-color:transparent}.arcgis-utility-network-trace-inputs calcite-block calcite-label:last-child{--calcite-label-margin-bottom: 0}.arcgis-utility-network-trace-inputs calcite-notice{padding:var(--calcite-spacing-xs)}`, o = "map-components:utility-network-trace-inputs";
class w extends u {
  constructor() {
    super(...arguments), this.messages = d({ blocking: !0 }), this.traceConfigurations = [], this._state = "ready", this.arcgisTraceInputsChange = n(), this.arcgisUtilityNetworkChange = n();
  }
  static {
    this.properties = { traceConfigurations: 16, utilityNetworks: 16, _state: 16, view: 0, selectedUtilityNetwork: 0, selectedTraceConfiguration: 0, referenceElement: 1 };
  }
  static {
    this.styles = g;
  }
  load() {
    this._setupUtilityNetwork();
  }
  willUpdate(t) {
    const { utilityNetworks: e, selectedUtilityNetwork: i } = this;
    (t.has("utilityNetworks") || !e || e.length === 0 || !i || !e?.includes(i)) && this._handleUtilityNetworksChange(), t.has("selectedUtilityNetwork") && (this.arcgisUtilityNetworkChange.emit(), this._setupTraceConfigurations()), t.has("selectedTraceConfiguration") && (this.arcgisTraceInputsChange.emit(), this._handleTraceConfigChange());
  }
  _handleSubnetworkTraceConfigChange(t) {
    if (!t.traceConfiguration) {
      console.error(`${o} - Trace configuration not found`);
      return;
    }
  }
  _selectTraceConfigurationByGuid(t) {
    const e = this.selectedUtilityNetwork?.sharedNamedTraceConfigurations.find((i) => i.globalId === t);
    if (!e) {
      console.error(`${o} - Trace configuration not found`);
      return;
    }
    this.selectedTraceConfiguration = e;
  }
  _handleTraceConfigChange() {
    const { selectedTraceConfiguration: t } = this;
    if (t)
      switch (t.traceType) {
        case "subnetwork":
          this._handleSubnetworkTraceConfigChange(t);
          break;
        default:
          console.error(`${o} - Unknown trace configuration type`);
      }
  }
  async _setupTraceConfigurations() {
    await this.selectedUtilityNetwork?.load(), this.traceConfigurations = this.selectedUtilityNetwork?.sharedNamedTraceConfigurations || [], this.selectedTraceConfiguration = this.traceConfigurations.at(0);
  }
  _handleUtilityNetworksChange() {
    const { utilityNetworks: t } = this;
    if (!t || t.length === 0) {
      this._state = "error", console.error(`${o} - No utility networks found in the map`);
      return;
    }
    (!this.selectedUtilityNetwork || !t.includes(this.selectedUtilityNetwork)) && (this.selectedUtilityNetwork = t.at(0));
  }
  async _setupUtilityNetwork() {
    if (this.view?.map) {
      const t = this.view.map;
      await t.load(), this.utilityNetworks = t.utilityNetworks || void 0;
    } else
      this._state = "disabled", console.error(`${o} - No view or map available to load utility networks`);
  }
  renderError(t, e = !1) {
    return s`<calcite-notice .closable=${e} kind=danger open scale=s width=full><div slot=message>${t}</div></calcite-notice>`;
  }
  renderLoader() {
    return s`<calcite-loader label=loading></calcite-loader>`;
  }
  renderTraceSelection() {
    const { messages: t, traceConfigurations: e } = this, i = t.labels.traceConfiguration;
    if (e.length === 0)
      return this.renderError(t.errors.noTraceConfigurationError, !0);
    const r = l(e, (a) => a.globalId, (a) => s`<calcite-option .value=${a.globalId}>${a.title}</calcite-option>`);
    return s`<calcite-label>${i}<calcite-select .label=${i} .value=${this.selectedTraceConfiguration?.globalId || void 0} @calciteSelectChange=${(a) => {
      this._selectTraceConfigurationByGuid(a.target.value);
    }}>${r}</calcite-select></calcite-label>`;
  }
  renderUtilityNetworkSelection() {
    const { messages: t } = this, e = t.labels.utilityNetwork, i = l(this.utilityNetworks, (r) => r.id, (r) => s`<calcite-option .value=${r.id}>${r.title}</calcite-option>`);
    return s`<calcite-label>${e}<calcite-select .label=${e} .value=${this.selectedUtilityNetwork?.id} @calciteSelectChange=${(r) => {
      this.selectedUtilityNetwork?.id !== r.target.value && (this.selectedUtilityNetwork = this.utilityNetworks?.find((a) => a.id === r.target.value));
    }}>${i}</calcite-select></calcite-label>`;
  }
  renderContent() {
    const { messages: t, utilityNetworks: e } = this;
    return !e || e.length === 0 ? this.renderError(t.errors.noUtilityNetworkError) : s`${e.length === 1 ? null : this.renderUtilityNetworkSelection()}${this.selectedUtilityNetwork?.loaded ? this.renderTraceSelection() : this.renderLoader()}`;
  }
  render() {
    const { messages: t, _state: e } = this;
    return s`<div class="arcgis-utility-network-trace-inputs"><calcite-block expanded .label=${t.labels.traceInputs}>${e === "disabled" ? this.renderError(t.errors.noMapError) : this.renderContent()}</calcite-block></div>`;
  }
}
c("arcgis-utility-network-trace-inputs", w);
export {
  w as ArcgisUtilityNetworkTraceInputs
};
