import { html as a } from "lit";
import { when as c, watch as p } from "@arcgis/core/core/reactiveUtils.js";
import { GenericController as u, useDirection as h, makeGenericController as n } from "@arcgis/lumina/controllers";
import { getSlotAssignedElements as f } from "@arcgis/toolkit/dom";
import { a as d } from "./slots.js";
import { createEvent as l } from "@arcgis/lumina";
/*! 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 */
class R extends u {
  //#endregion
  //#region Lifecycle
  constructor(o) {
    super(o), this.direction = h(), this.component.listen("arcgisViewClick", (r) => {
      this._handleOpenPopup(r.detail);
    }), this.component.listenOn(globalThis, "unhandledrejection", (r) => {
      if (r.reason?.name === "webgl:required") {
        const { name: e } = r.reason;
        this.webGLError = { name: e };
      }
    });
  }
  //#endregion
  //#region Private Methods
  hostLifecycle() {
    return c(() => this.component.view.popup?.visible, () => {
      this.popupRef?.visible && (this.popupRef.visible = !1);
    });
  }
  openPopup(o) {
    this.popupRef ? this.popupRef.open(o) : this.component.view.openPopup(o);
  }
  closePopup() {
    this.popupRef ? this.popupRef.visible = !1 : this.component.view.closePopup();
  }
  async _handleOpenPopup(o) {
    if (this.popupRef && !this.component.popupDisabled) {
      o.stopPropagation();
      const { location: r, promises: e } = await this.popupRef?.handleViewClick(o);
      e?.length && (this.component.view.closePopup(), this.popupRef?.open({ location: r, promises: e }));
    }
  }
  _handlePopupSlotChange(o) {
    const r = f(o.target, "arcgis-popup");
    !this.popupRef && r.length ? this.popupRef = r[0] : r.length || (this.popupRef = void 0);
  }
  _webGLErrorAlert() {
    return this.webGLError ? a`<calcite-alert icon=x-octagon kind=danger open .label=${this.webGLError.name}><div slot=title>Unable to display map. WebGL2 support is required.</div><div slot=message>Ensure that your browser and hardware meet the minimum requirements.</div><calcite-link slot=link href=https://esriurl.com/webgl-faq>https://esriurl.com/webgl-faq</calcite-link></calcite-alert>` : null;
  }
  //#endregion
  //#region Rendering
  render() {
    return d(this.direction, (o) => this._handlePopupSlotChange(o), this._webGLErrorAlert());
  }
}
const L = n((t, o) => {
  const r = l();
  return o.onLifecycle(
    () => p(
      () => {
        const { map: e } = t;
        if (!e || e.destroyed)
          return [];
        const i = [];
        return e.loadError && i.push(e), e.basemap?.loadError && i.push(e.basemap), e.ground.loadError && i.push(e.ground), [...e.allLayers, ...e.allTables].forEach((s) => {
          s.loadError && i.push(s);
        }), i;
      },
      (e) => {
        e.length && (t.loadErrorSources = e, r.emit());
      }
    )
  ), r;
}), y = n(
  (t, o) => {
    const r = l();
    return o.onLifecycle(
      () => p(
        () => t.view?.readyState,
        (e) => {
          (e === "map-content-error" || e === "rendering-error") && r.emit();
        }
      )
    ), r;
  }
);
export {
  R as U,
  y as a,
  L as u
};
