/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as i } from "../../chunks/runtime.js";
import { css as t, html as p } from "lit";
import { createRef as m, ref as r } from "lit-html/directives/ref.js";
import { LitElement as o } from "@arcgis/lumina";
import l from "@arcgis/core/widgets/BasemapGallery";
import n from "@arcgis/core/widgets/BasemapGallery/support/PortalBasemapsSource";
const d = t`.display-block{display:block}`;
class h extends o {
  constructor() {
    super(...arguments), this._basemapElement = m();
  }
  static {
    this.properties = { mapView: 0, basemapConfig: 0, basemapWidget: 0 };
  }
  static {
    this.styles = d;
  }
  willUpdate(a) {
    a.has("basemapConfig") && this.basemapConfigWatchHandler(), a.has("mapView") && this.mapViewWatchHandler();
  }
  async basemapConfigWatchHandler() {
    if (this.basemapWidget?.source) {
      const a = this.basemapWidget?.source;
      a.query = this.basemapConfig?.basemapGroupId ? `id:${this.basemapConfig.basemapGroupId}` : null, await a.refresh();
    }
  }
  async mapViewWatchHandler() {
    this.mapView !== void 0 && await this.mapView.when(() => {
      this._initBaseMapGallery(this.mapView);
    });
  }
  render() {
    return p`<div class="display-block" ${r(this._basemapElement)}></div>`;
  }
  loaded() {
    this.mapView && this.mapViewWatchHandler();
  }
  async _initBaseMapGallery(a) {
    if (a) {
      const e = new n({
        query: this.basemapConfig?.basemapGroupId ? `id:${this.basemapConfig.basemapGroupId}` : null,
        filterFunction: this.basemapConfig ? (s) => s?.portalItem?.id && this.basemapConfig ? !this.basemapConfig.basemapIdsToFilter.includes(s?.portalItem?.id) : !0 : () => !0
      });
      await e.refresh(), this.basemapWidget ? (this.basemapWidget.view = a, this.basemapWidget.source = e) : this.basemapWidget = new l({
        container: this._basemapElement.value,
        view: a,
        source: e
      });
    }
  }
}
i("map-basemap-gallery", h);
export {
  h as MapBasemapGallery
};
