/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { html as h } from "lit";
import { when as p, watch as s } from "@arcgis/core/core/reactiveUtils.js";
import { GenericController as u, useDirection as c, makeGenericController as a } from "@arcgis/lumina/controllers";
import { getSlotAssignedElements as m } from "@arcgis/toolkit/dom";
import { createRef as f } from "lit/directives/ref.js";
import { S as E } from "./slots.js";
import { createEvent as l } from "@arcgis/lumina";
class _ extends u {
  //#endregion
  //#region Lifecycle
  constructor(e) {
    super(e), this.direction = c(), this.popupRef = f(), this.attributionHeight = 0, this._uiContainer = null, this.component.listen("arcgisViewClick", (o) => {
      this._handleOpenPopup(o.detail);
    }), this.component.listenOn(globalThis, "unhandledrejection", (o) => {
      if (o.reason?.name === "webgl:required") {
        const { name: t } = o.reason;
        this.webGLError = { name: t };
      }
    });
  }
  //#endregion
  //#region Private Methods
  hostLifecycle() {
    return [
      p(() => this.component.view.ready, () => {
        this._assignDefaultPopup();
      }),
      // Close component popup when view popup is opened
      p(() => this.component.view.popup?.visible, () => {
        this.popupElement?.open && (this.popupElement.open = !1);
      })
    ];
  }
  async openPopup(e) {
    if (this._assignDefaultPopup(), this.closePopup(), this.popupElement) {
      await this.popupElement.componentOnReady(), this.popupElement.open = !0;
      const { fetchFeatures: o } = e;
      delete e.fetchFeatures, Object.assign(this.popupElement, e), o && this.popupElement.fetchFeatures();
    } else
      await this.component.view.openPopup(e);
  }
  closePopup() {
    this.popupElement && (this.popupElement.open = !1), this.component.view.closePopup();
  }
  _assignDefaultPopup() {
    this.popupRef.value && !this.popupElement && (this.popupElement = this.popupRef.value, this.popupElement.referenceElement = this.component.el, this.popupElement.view = this.component.view);
  }
  handleAttributionHeightChange(e) {
    if (!(this.attributionHeight === e || !this.component.el.shadowRoot)) {
      if (this.attributionHeight = e, !this._uiContainer) {
        const o = this.component.el.shadowRoot.querySelector(".arcgis-map-container-ui");
        this._uiContainer = o;
      }
      this._uiContainer?.style.setProperty("--arcgis-internal-attribution-space-top", `${this.attributionHeight}px`);
    }
  }
  async _handleOpenPopup(e) {
    if (this._assignDefaultPopup(), this.popupElement && !this.component.popupDisabled) {
      e.stopPropagation();
      const { location: o, promises: t } = await this.popupElement.handleViewClick(e);
      t?.length && this.openPopup({ location: o, promises: t });
    }
  }
  _handlePopupSlotChange(e) {
    const o = m(e.target, "arcgis-popup");
    !this.popupElement && o.length ? this.popupElement = o[0] : o.length || (this.popupElement = void 0);
  }
  _webGLErrorAlert() {
    return this.webGLError ? h`<arcgis-webgl-error .errorName=${this.webGLError.name}></arcgis-webgl-error>` : null;
  }
  //#endregion
  //#region Rendering
  render() {
    return E({ direction: this.direction, popupComponentEnabled: this.component.popupComponentEnabled, handlePopupSlotChange: (e) => this._handlePopupSlotChange(e), webGLErrorElement: this._webGLErrorAlert(), popupElement: this.popupRef });
  }
}
const v = a((i, e) => {
  const o = l();
  return e.onLifecycle(
    () => s(
      () => {
        const { map: t } = i;
        if (!t || t.destroyed)
          return [];
        const n = [];
        return t.loadError && n.push(t), t.basemap?.loadError && n.push(t.basemap), t.ground.loadError && n.push(t.ground), [...t.allLayers, ...t.allTables].forEach((r) => {
          r.loadError && n.push(r);
        }), n;
      },
      (t) => {
        t.length && (i.loadErrorSources = t, o.emit());
      }
    )
  ), o;
}), L = a(
  (i, e) => {
    const o = l();
    return e.onLifecycle(
      () => s(
        () => i.view?.readyState,
        (t) => {
          (t === "map-content-error" || t === "rendering-error") && o.emit();
        }
      )
    ), o;
  }
);
export {
  _ as U,
  L as a,
  v as u
};
