import { c as m } from "../../chunks/runtime.js";
import { css as f, html as n } from "lit";
import { keyed as d } from "lit/directives/keyed.js";
import { ArcgisUtilityNetworkAssociationList as g } from "../arcgis-utility-network-association-list/customElement.js";
import { stripHTML as u } from "@arcgis/core/applications/Components/stringUtils.js";
import { renderElement as p, nothing as y, safeClassMap as w } from "@arcgis/lumina";
import { formatNumber as $, substitute as b } from "@arcgis/core/intl.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 _ = f`:host{position:relative;flex:1 1 auto}.list-item--hidden{display:none}.feature-observer{bottom:var(--calcite-spacing-xxl);position:relative;text-align:center;z-index:2}.loading-container{display:flex;justify-content:center;padding:var(--calcite-spacing-md) 0;width:100%}.notice-container{margin-bottom:var(--calcite-spacing-xs)}`, A = "nested";
class F extends g {
  constructor() {
    super(...arguments), this.displayCount = 3, this.flowType = "feature-utility-network-association-type";
  }
  static {
    this.properties = { description: 1, displayCount: 9, endIndex: 32, flowItems: 0, flowType: 1, map: 0, spatialReference: 0, timeZone: 1, view: 0 };
  }
  static {
    this.styles = _;
  }
  get endIndex() {
    const { currentFeaturePage: t, featuresPerPage: e, maxFeatureCount: s } = this;
    return Math.min(t * e, s);
  }
  _featureViewModelMatch(t, e) {
    const s = t.graphic, i = s?.layer;
    let o = null;
    i?.type === "subtype-sublayer" && i.parent ? o = i.parent.globalIdField ?? null : i && "globalIdField" in i && (o = i.globalIdField);
    const l = o ? s?.attributes[o] : null, r = e.graphic, a = r?.layer;
    let c = null;
    a?.type === "subtype-sublayer" && a.parent ? c = a.parent.globalIdField ?? null : a && "globalIdField" in a && (c = a.globalIdField);
    const h = c ? r?.attributes[c] : null;
    return l && h && l === h;
  }
  _findFlowItem(t) {
    return this.flowItems?.findIndex((e) => {
      if (e.flowType !== "feature-association")
        return !1;
      const s = e.viewModel;
      return this._featureViewModelMatch(s, t);
    }) ?? -1;
  }
  _formatPercentAlong(t) {
    const { percentAlong: e } = t;
    return e == null ? "" : $(e, {
      style: "percent",
      maximumFractionDigits: 2
    });
  }
  async _handleFeatureClick(t, e, s) {
    const { flowItems: i } = this;
    if (t) {
      i?.removeAll();
      return;
    }
    if (e < 0 || !i) {
      await this._selectAssociation(s);
      return;
    }
    for (; i.length > e + 1; )
      i.pop();
  }
  _isConnectivityAssociation(t) {
    const { associationType: e } = t;
    return e === "connectivity" || e === "junction-junction-connectivity" || e === "junction-edge-from-connectivity" || e === "junction-edge-midspan-connectivity" || e === "junction-edge-to-connectivity";
  }
  _isConnectivityMidspanAssociation(t) {
    return t.associationType === "junction-edge-midspan-connectivity";
  }
  _isParentFeature(t) {
    const e = this.flowItems?.getItemAt(0);
    if (!e)
      return !1;
    const s = e.parentFeatureViewModel;
    return this._featureViewModelMatch(s, t);
  }
  async _selectAssociation(t) {
    const { flowItems: e } = this;
    e && e.push(p(d(this.parentFeatureViewModel, n`<arcgis-feature .flowItems=${e} .flowType=${"feature-utility-network-associations"} .graphic=${t.graphic} .map=${this.map} show-utility-network-content .spatialReference=${this.spatialReference} .timeZone=${this.timeZone} .view=${this.view}></arcgis-feature>`)));
  }
  _showAllAssociations(t) {
    const { flowItems: e, description: s } = this;
    if (!e || !t)
      return;
    this.showAllEnabled = !0;
    const i = t?.title ?? "";
    e.push(p(n`<arcgis-feature-utility-network-association-list .associationViewModels=${this.associationViewModels} .description=${s} .flowItems=${e} .flowType=${this.flowType} .messages=${this.messages} .parentFeatureViewModel=${this.parentFeatureViewModel} .selectedLayer=${t} .showAllEnabled=${this.showAllEnabled} title=${i ?? y}></arcgis-feature-utility-network-association-list>`));
  }
  render() {
    const { associationViewModels: t, state: e } = this, s = this.selectedLayer?.title ?? this.messages?.noTitle ?? "";
    return n`<div>${e === "loading" || e === "querying" ? this._renderLoading() : n`<calcite-list .displayMode=${A} .filterEnabled=${this.showAllEnabled && !!this.selectedLayer} .filterLabel=${this.messages?.associationFilterPlaceholder} .filterPlaceholder=${this.messages?.associationFilterPlaceholder} .label=${s}>${this.showAllEnabled && this.selectedLayer ? n`${this._renderFeatureCountWarning()}${this._renderAssociatedFeatureListPage()}${this._renderFeatureObserver()}` : Array.from(t.keys(), (i) => this._renderTypeList(i))}</calcite-list>`}</div>`;
  }
  _renderAssociatedFeature(t) {
    const { featureViewModel: e, title: s } = t, i = this.state === "loading", o = this._findFlowItem(e), l = o < 0 && this._isParentFeature(e), r = l || o >= 0, a = `associated-feature-${t.association.globalId}`;
    return n`<calcite-list-item class=${w(i ? "list-item--hidden" : "")} .description=${u(t.terminalName ?? "")} .label=${u(s)} @calciteListItemSelect=${async () => await this._handleFeatureClick(l, o, e)}>${this._isConnectivityAssociation(t.association) ? this._renderConnectivityIcon(t.association.associationType, a) : null}${this._isConnectivityMidspanAssociation(t.association) ? n`<calcite-chip .label=${this._formatPercentAlong(t.association)} scale=s slot=content-end>${this._formatPercentAlong(t.association)}</calcite-chip>` : null}${this._renderChevronIconNode(r)}</calcite-list-item>`;
  }
  _renderAssociatedFeatureList(t) {
    return t.toArray().map((e) => this._renderAssociatedFeature(e));
  }
  _renderAssociatedFeatureListPage() {
    const t = this.associationViewModels.get(this.selectedLayer).slice(0, this.endIndex);
    return [...this._renderTooltips(t), ...this._renderAssociatedFeatureList(t)];
  }
  _renderChevronIconNode(t) {
    return n`<calcite-icon flip-rtl .icon=${t ? "move-up" : "chevron-right"} scale=s slot=content-end></calcite-icon>`;
  }
  _renderItemTooltip(t) {
    const { tooltipReferenceMap: e } = this, s = `associated-feature-${t.association.globalId}`;
    return this._isConnectivityAssociation(t.association) ? n`<calcite-tooltip overlay-positioning=fixed .referenceElement=${e.get(s)}>${this.getConnectivityTooltip(t.association.associationType)}</calcite-tooltip>` : null;
  }
  _renderTypeList(t) {
    const { messages: e } = this, { displayCount: s } = this, i = this.associationViewModels.get(t), o = i.slice(0, s), l = o.length < i.length, r = b(e?.numberRecords ?? "", { number: i.length.toString() }), a = e?.showAll ?? "";
    return d("show-all", n`<calcite-list-item expanded .label=${t.title} .value=${t.id}><calcite-chip .label=${String(i.length)} scale=s slot=content-end>${i.length}</calcite-chip><calcite-list .group=${t.id} .label=${t.title ?? ""}>${[this._renderTooltips(o), this._renderAssociatedFeatureList(o)]}${l ? d("show-all-item", n`<calcite-list-item .description=${r} .label=${a} @calciteListItemSelect=${() => this._showAllAssociations(t)}><calcite-icon icon=list scale=s slot=content-end></calcite-icon></calcite-list-item>`) : null}</calcite-list></calcite-list-item>`);
  }
  _renderTooltips(t) {
    return t.toArray().map((e) => this._renderItemTooltip(e));
  }
}
m("arcgis-feature-utility-network-association-list", F);
export {
  F as ArcgisFeatureUtilityNetworkAssociationList
};
