/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as g } from "../../chunks/runtime.js";
import { css as _, html as r } from "lit";
import * as d from "@arcgis/core/geometry/operators/projectOperator.js";
import f from "@arcgis/core/portal/Portal.js";
import b from "@arcgis/core/geometry/SpatialReference.js";
import { LitElement as v, createEvent as m, nothing as C } from "@arcgis/lumina";
import { locationToAddress as y } from "@arcgis/core/rest/locator.js";
import { substitute as $ } from "@arcgis/core/intl.js";
import { u as R } from "../../chunks/useT9n.js";
import { watch as p } from "@arcgis/core/core/reactiveUtils.js";
const x = _`calcite-notice{padding:var(--calcite-space-lg)}calcite-block{padding-inline-end:var(--calcite-spacing-xxs)}calcite-block[expanded]{margin:0}`;
class w extends v {
  constructor() {
    super(...arguments), this.messages = R(), this._abortController = null, this._location = null, this._results = null, this.headingLevel = 3, this.portal = f.getDefault(), this.autoDestroyDisabled = !1, this.hideCoordinates = !1, this.arcgisReverseGeocodeResultsChange = m(), this.arcgisReverseGeocodeCopy = m();
  }
  static {
    this.properties = { _abortController: 16, _location: 16, _results: 16, headingLevel: 9, portal: 0, location: 0, messageOverrides: 0, autoDestroyDisabled: 5, hideCoordinates: 5, reverseGeocodeResults: 32 };
  }
  static {
    this.styles = x;
  }
  get reverseGeocodeResults() {
    return this._results;
  }
  async destroy() {
    await this.manager.destroy(), this._cancelQueries();
  }
  loaded() {
    this.manager.onLifecycle(() => [
      p(() => [this.portal, this.location], () => {
        this._update();
      }, { initial: !0 }),
      p(() => this._results, () => {
        this.arcgisReverseGeocodeResultsChange.emit();
      })
    ]);
  }
  _cancelQueries() {
    this._abortController?.abort(), this._abortController = null;
  }
  async _update() {
    const { location: e, messages: t } = this;
    if (this._cancelQueries(), !e) {
      this._location = null, this._results = null;
      return;
    }
    const s = new AbortController(), i = s.signal;
    this._abortController = s;
    const o = () => i.aborted || this._abortController !== s;
    if (!d.isLoaded() && (await d.load(), o()))
      return;
    const c = d.execute(e, b.WGS84) ?? e;
    if (o() || (this._location = c, await this.portal?.load({ signal: i }), o()))
      return;
    const l = this.portal?.helperServices?.geocode?.filter((a) => a.reverseGeocode ?? !0);
    if (!l) {
      if (o())
        return;
      this._results = null;
      return;
    }
    const n = l.map(async (a) => await y(a.url, { location: e }, { signal: i })), u = await Promise.allSettled(n);
    o() || (this._abortController = null, this._results = l.map((a, h) => ({
      name: a.name ?? t?.untitledService ?? "",
      address: (u[h].status === "fulfilled" ? u[h].value.address : null) ?? t?.noResultFound ?? "",
      url: a.url
    })));
  }
  _copyTextToClipboard(e) {
    navigator.clipboard.writeText(e), this.arcgisReverseGeocodeCopy.emit({ data: e });
  }
  _renderMoreResults(e) {
    const { messages: t } = this;
    return !this._abortController && e?.length ? r`<calcite-block collapsible scale=s .headingLevel=${this.headingLevel} .heading=${$(t?.showMoreResults ?? "", { count: e.length })}><calcite-list scale=s display-mode=flat interaction-mode=static selection-mode=none .label=${t?.moreResults ?? ""}>${e?.map((s) => this._renderItem(s))}</calcite-list></calcite-block>` : null;
  }
  _renderCopyAction({ text: e, value: t }) {
    return r`<calcite-action .text=${e} title=${e ?? C} slot=actions-end icon=copy @click=${() => this._copyTextToClipboard(t)}></calcite-action>`;
  }
  _renderCoordinatesItem() {
    const { _location: e, messages: t } = this;
    if (!e || this.hideCoordinates)
      return null;
    const { latitude: s, longitude: i } = e, o = s != null && i != null ? `${i.toFixed(6)}, ${s.toFixed(6)}` : null;
    return o ? r`<calcite-list-item .label=${o} .description=${t.coordinates ?? ""}>${this._renderCopyAction({
      text: t.copyCoordinates ?? "",
      value: o
    })}</calcite-list-item>` : null;
  }
  _renderItem(e) {
    const { messages: t } = this;
    return r`<calcite-list-item .label=${e.address} .description=${e.name}>${this._renderCopyAction({ text: t.copyAddress ?? "", value: e.address ?? "" })}</calcite-list-item>`;
  }
  render() {
    const { _abortController: e, _location: t, _results: s, messages: i } = this;
    if (!t)
      return r`<calcite-notice scale=s open kind=warning icon><div slot=message>${i?.noLocation ?? ""}</div></calcite-notice>`;
    const o = s?.[0], c = s && s.length > 1 ? s.slice(1) : void 0, l = this._renderCoordinatesItem(), n = o ? this._renderItem(o) : null;
    return !e && !n && !l ? r`<calcite-notice scale=s open kind=info><div slot=message>${i?.noResultFound ?? ""}</div></calcite-notice>` : r`<calcite-list .loading=${!!e} display-mode=flat interaction-mode=static selection-mode=none .label=${i.label ?? ""} scale=s>${l}${n}</calcite-list>${this._renderMoreResults(c)}`;
  }
}
g("arcgis-reverse-geocode-list", w);
export {
  w as ArcgisReverseGeocodeList
};
