/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as y } from "../../chunks/runtime.js";
import { d, a as m, c as p } from "../../chunks/mapViewUtils.js";
import * as o from "@arcgis/core/intl";
import { u as f } from "../../chunks/useT9n.js";
import { css as L, html as a } from "lit";
import { LitElement as u, createEvent as h, noShadowRoot as w } from "@arcgis/lumina";
import * as g from "@arcgis/core/core/reactiveUtils";
async function _(n, e) {
  const { style: i = "decimal", places: t = 2 } = e || {}, s = o.convertNumberFormatToIntlOptions({
    places: t,
    digitSeparator: !0
  });
  return s.style = i, o.formatNumber(n, s);
}
const x = L`@layer{:host{display:block}.notice-msg{width:calc(100% - 20px);padding:10px}.layer-name{font-weight:500;word-break:break-word}.feature-count{padding:0 10px}}`;
class I extends u {
  constructor() {
    super(), this.layers = [], this.showFeatureCount = !0, this.showNextIcon = !1, this.applyLayerViewFilter = !1, this.filterByMapExtent = !1, this._noLayersToDisplay = !1, this._mapLayerIds = [], this._isLoading = !1, this._translations = f({ name: "layer-list", blocking: !0 }), this.layerSelect = h(), this.layersListLoaded = h(), this.listenOn(window, "selectedLanguageUpdated", this.selectedLanguageUpdated);
  }
  static {
    this.properties = { mapView: 0, layers: 0, showFeatureCount: 5, showNextIcon: 5, applyLayerViewFilter: 5, filterByMapExtent: 5, _noLayersToDisplay: 16, _mapLayerIds: 16, _isLoading: 16, _translations: 16 };
  }
  static {
    this.styles = x;
  }
  static {
    this.shadowRootOptions = w;
  }
  willUpdate(e) {
    e.has("filterByMapExtent") && this.hasUpdated && this.filterByMapExtentWatchHandler(this.filterByMapExtent);
  }
  filterByMapExtentWatchHandler(e) {
    e ? this.initializeMapExtentFilter() : this._filterByMapExtentHandle && this._filterByMapExtentHandle.remove();
  }
  async refresh() {
    await this.setLayers();
  }
  async selectedLanguageUpdated(e) {
    await this.setLayers();
  }
  async load() {
    this.filterByMapExtent && this.initializeMapExtentFilter();
  }
  async loaded() {
    await this.setLayers();
  }
  render() {
    return a`${this._isLoading && a`<calcite-loader label scale=m></calcite-loader>` || ""}${!this._isLoading && this.mapView && this._noLayersToDisplay && a`<calcite-notice class="notice-msg" icon=layers-reference kind=danger open><div slot=title>${this._translations.error}</div><div slot=message>${this._translations.noLayerToDisplayErrorMsg}</div></calcite-notice>` || ""}${!this._isLoading && this.mapView && a`<calcite-list label selection-appearance=border selection-mode=none>${this.filterByMapExtent && this._mapLayerIds.length > 0 && this.getExtentNotice() || ""}${this.renderLayerList()}</calcite-list>` || ""}`;
  }
  initializeMapExtentFilter() {
    this._filterByMapExtentHandle = g.when(() => this.mapView?.stationary, () => {
      this.setLayers();
    }, {
      initial: !0
    });
  }
  async setLayers() {
    this.mapView && (this._isLoading = !0, await this.initLayerHash(), this._isLoading = !1);
  }
  async initLayerHash() {
    const e = [];
    if (this.mapView) {
      this._layerItemsHash = await d(this.mapView, !0);
      const i = await m(this.mapView);
      for (const t of i)
        if (t?.type === "feature" && this.showFeatureCount) {
          const s = t.createQuery();
          if (t?.definitionExpression && (s.where = s.where + " AND " + t.definitionExpression), this.applyLayerViewFilter) {
            const r = await p(this.mapView, t.id);
            r?.filter?.where && (s.where = s.where ? s.where + " AND " + r.filter.where : r.filter.where);
          }
          s.geometry = this.filterByMapExtent ? this.mapView.extent : null;
          const l = t.queryFeatureCount(s);
          e.push(l), l.then(async (r) => {
            const c = Number.isNaN(r) ? "" : await _(r, {
              places: 0,
              style: "decimal"
            });
            this._layerItemsHash && (this._layerItemsHash[t.id].formattedFeatureCount = c);
          });
        }
      await Promise.all(e).then(() => {
        const t = this._layerItemsHash && this.getLayersToBeShownInList(this._layerItemsHash);
        this._mapLayerIds = t?.reverse() || [], this.handleNoLayersToDisplay();
      }, () => {
        this._mapLayerIds = [], this.handleNoLayersToDisplay();
      });
    }
  }
  handleNoLayersToDisplay() {
    this._noLayersToDisplay = !(this._mapLayerIds.length > 0), this.layersListLoaded.emit(this._mapLayerIds);
  }
  getLayersToBeShownInList(e) {
    const i = this.layers?.length > 0 ? this.layers : [];
    return Object.keys(e).reduce((t, s) => (i.indexOf(s) > -1 && t.push(s), t), []);
  }
  renderLayerList() {
    return this._mapLayerIds.length > 0 ? this._mapLayerIds.reduce((e, i) => {
      if (this._layerItemsHash && this._layerItemsHash[i]) {
        const t = this.getLayerListItem(i);
        t && e.push(t);
      }
      return e;
    }, []) : void 0;
  }
  getLayerListItem(e) {
    const i = this._layerItemsHash && this._layerItemsHash[e].name, t = this._layerItemsHash && this._layerItemsHash[e].formattedFeatureCount;
    return a`<calcite-list-item @calciteListItemSelect=${() => {
      this.onLayerItemSelected(e);
    }}><div class="layer-name" slot=content-start>${i}</div>${this.showFeatureCount && t !== void 0 && t !== "" && a`<div class="feature-count" slot=content-end>${`(${t})`}</div>` || ""}${this.showNextIcon && a`<calcite-icon flip-rtl icon=chevron-right scale=s slot=content-end></calcite-icon>` || ""}</calcite-list-item>`;
  }
  getExtentNotice() {
    return a`<calcite-notice class="notice-msg" icon=extent-filter kind=brand open scale=s><div slot=message>${this._translations.extentFilterMsg}</div></calcite-notice>`;
  }
  onLayerItemSelected(e) {
    this._layerItemsHash && this.layerSelect.emit({ layerId: e, layerName: this._layerItemsHash[e]?.name });
  }
}
y("layer-list", I);
export {
  I as LayerList
};
