/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { css, html } from "lit";
import { createRef, ref } from "lit-html/directives/ref.js";
import { LitElement } from "@arcgis/lumina";
import BasemapGallery from "@arcgis/core/widgets/BasemapGallery";
import PortalBasemapsSource from "@arcgis/core/widgets/BasemapGallery/support/PortalBasemapsSource";
const styles = css`/** @license
 * Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

.display-block {
  display: block;
}`;
class MapBasemapGallery extends LitElement {
  constructor() {
    super(...arguments);
    this._basemapElement = createRef();
  }
  static {
    this.properties = { mapView: [0, {}, { attribute: false }], basemapConfig: [0, {}, { attribute: false }], basemapWidget: [0, {}, { attribute: false }] };
  }
  static {
    this.styles = styles;
  }
  willUpdate(changes) {
    if (changes.has("basemapConfig")) {
      void this.basemapConfigWatchHandler();
    }
    if (changes.has("mapView")) {
      void this.mapViewWatchHandler();
    }
  }
  async basemapConfigWatchHandler() {
    if (this.basemapWidget?.source) {
      const portalBasemapsSource = this.basemapWidget?.source;
      portalBasemapsSource.query = this.basemapConfig?.basemapGroupId ? `id:${this.basemapConfig.basemapGroupId}` : null;
      await portalBasemapsSource.refresh();
    }
  }
  async mapViewWatchHandler() {
    if (this.mapView !== void 0) {
      await this.mapView.when(() => {
        void this._initBaseMapGallery(this.mapView);
      });
    }
  }
  render() {
    return html`<div class="display-block" ${ref(this._basemapElement)}></div>`;
  }
  loaded() {
    if (this.mapView) {
      void this.mapViewWatchHandler();
    }
  }
  async _initBaseMapGallery(view) {
    if (view) {
      const source = new PortalBasemapsSource({
        query: this.basemapConfig?.basemapGroupId ? `id:${this.basemapConfig.basemapGroupId}` : null,
        filterFunction: this.basemapConfig ? (basemap) => {
          return basemap?.portalItem?.id && this.basemapConfig ? !this.basemapConfig.basemapIdsToFilter.includes(basemap?.portalItem?.id) : true;
        } : () => true
      });
      await source.refresh();
      if (!this.basemapWidget) {
        this.basemapWidget = new BasemapGallery({
          container: this._basemapElement.value,
          view,
          source
        });
      } else {
        this.basemapWidget.view = view;
        this.basemapWidget.source = source;
      }
    }
  }
}
customElement("map-basemap-gallery", MapBasemapGallery);
export {
  MapBasemapGallery
};
