import { c } from "../../chunks/runtime.js";
import { LitElement as y, createEvent as d } from "@arcgis/lumina";
import { html as o } from "lit";
import { u } from "../../chunks/useT9n.js";
/*! 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 l = "map-components:arcgis-utility-network-result-feature-list";
class h extends y {
  constructor() {
    super(...arguments), this._layerByLayerId = /* @__PURE__ */ new Map(), this._messages = u({ blocking: !0 }), this._compareByLayerTitle = ([e], [t]) => {
      const s = this._getLayerBySourceId(e), r = this._getLayerBySourceId(t), i = s?.title ?? "", a = r?.title ?? "";
      return i.localeCompare(a, void 0, { numeric: !0 });
    }, this._state = "ready", this.elements = [], this.layers = [], this.arcgisSelectedLayerChange = d(), this._renderLayerListItem = ([e, t]) => {
      const { utilityNetwork: s, _messages: r } = this, i = s.getLayerIdBySourceId(e), a = this._getLayerByLayerId(i), n = a?.title || r.unknown;
      return o`<calcite-list-item .label=${n} @calciteListItemSelect=${() => this._selectedLayer = a}><calcite-chip .label=${r.labels.numberOfElements} slot=content-end>${t.toString()}</calcite-chip><calcite-icon icon=chevron-right slot=content-end></calcite-icon></calcite-list-item>`;
    };
  }
  static {
    this.properties = { _selectedLayer: 16, _state: 16, elements: 0, layers: 0, messageOverrides: 0, selectedLayer: 32, utilityNetwork: 0 };
  }
  get _sourceIdToFeatureCount() {
    const e = /* @__PURE__ */ new Map();
    return this.elements.forEach((t) => {
      const s = e.get(t.networkSourceId);
      s == null ? e.set(t.networkSourceId, 1) : e.set(t.networkSourceId, s + 1);
    }), e;
  }
  get selectedLayer() {
    return this._selectedLayer;
  }
  willUpdate(e) {
    e.has("_selectedLayer") && this.arcgisSelectedLayerChange.emit(), (e.has("layers") || e.has("utilityNetwork")) && this._loadDependencies();
  }
  load() {
    this._loadDependencies();
  }
  loaded() {
    this.elements.length === 0 && console.warn(`${l} was rendered without any elements set for the \`elements\` property.`), this.utilityNetwork || console.warn(`${l} was rendered without the required \`utilityNetwork\` property.`), this.layers.length === 0 && console.warn(`${l} was rendered without any layers set for the \`layers\` property.`);
  }
  _getLayerBySourceId(e) {
    const t = this.utilityNetwork.getLayerIdBySourceId(e);
    return this._getLayerByLayerId(t);
  }
  _getLayerByLayerId(e) {
    if (e == null)
      return;
    const { _layerByLayerId: t, layers: s } = this;
    if (t.get(e))
      return t.get(e);
    const r = s.find((i) => "layerId" in i && i.layerId === e);
    return r && t.set(e, r), r;
  }
  async _loadDependencies() {
    const { layers: e, utilityNetwork: t } = this;
    if (!t)
      return;
    t.loaded || (this._state = "loading", await t.load());
    const s = e.filter((r) => !r.loaded && t.isUtilityLayer(r)).map((r) => r.load());
    s.length > 0 && (this._state = "loading", await Promise.allSettled(s)), this._state = "ready";
  }
  render() {
    if (!this.utilityNetwork)
      return;
    const { _messages: e } = this, t = Array.from(this._sourceIdToFeatureCount.entries()).sort(this._compareByLayerTitle).map(this._renderLayerListItem);
    return o`<calcite-panel .loading=${this._state === "loading"}><calcite-list .label=${e.labels.traceResultElementsByLayer}>${t}</calcite-list></calcite-panel>`;
  }
}
c("arcgis-utility-network-result-feature-list", h);
export {
  h as ArcgisUtilityNetworkResultFeatureList
};
