/* 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, createEvent } from "@arcgis/lumina";
import FloorFilter from "@arcgis/core/widgets/FloorFilter";
import * as reactiveUtils from "@arcgis/core/core/reactiveUtils";
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.
 */

:host {
  display: block;
}`;
class MapFloorFilter extends LitElement {
  constructor() {
    super(...arguments);
    this.enabled = false;
    this._floorFilterElement = createRef();
    this.facilityChanged = createEvent();
    this.levelChanged = createEvent();
    this.siteChanged = createEvent();
  }
  static {
    this.properties = { enabled: [5, {}, { type: Boolean }], floorFilterWidget: [0, {}, { attribute: false }], mapView: [0, {}, { attribute: false }] };
  }
  static {
    this.styles = styles;
  }
  willUpdate(changes) {
    if (changes.has("mapView")) {
      void this.mapViewWatchHandler();
    }
    if (changes.has("enabled")) {
      void this.enabledWatchHandler();
    }
  }
  async mapViewWatchHandler() {
    this.mapView && await this._initFloorFilter(this.mapView);
  }
  async enabledWatchHandler() {
    if (this.enabled) {
      this.mapView && await this._initFloorFilter(this.mapView);
    } else if (!this.enabled) {
      this._destroyWidget();
    }
  }
  render() {
    return html`<div ${ref(this._floorFilterElement)}></div>`;
  }
  async loaded() {
    if (this.mapView && !this.floorFilterWidget) {
      await this._initFloorFilter(this.mapView);
    }
  }
  _destroyWidget() {
    if (this.floorFilterWidget) {
      this.floorFilterWidget.destroy();
      this.floorFilterWidget = void 0;
    }
    if (this._floorFilterElement.value) {
      this._floorFilterElement.value.remove();
    }
  }
  _initContainer() {
    this._destroyWidget();
    if (this._floorFilterElement) {
      this._floorFilterElement.value = document.createElement("div");
    }
  }
  async _initFloorFilter(view) {
    const webMap = view?.map;
    webMap && await webMap.when(() => {
      if (view && this.enabled && webMap?.floorInfo) {
        this._initContainer();
        this.floorFilterWidget = new FloorFilter({
          container: this._floorFilterElement.value,
          view
        });
        this._facilityHandle?.remove();
        this._facilityHandle = reactiveUtils?.watch(() => this.floorFilterWidget?.facility, (facility) => {
          if (facility) {
            this.facilityChanged.emit(facility);
          }
        });
        this._levelHandle?.remove();
        this._levelHandle = reactiveUtils.watch(() => this.floorFilterWidget?.level, (level) => {
          if (level) {
            this.levelChanged.emit(level);
          }
        });
        this._siteHandle?.remove();
        this._siteHandle = reactiveUtils.watch(() => this.floorFilterWidget?.site, (site) => {
          if (site) {
            this.siteChanged.emit(site);
          }
        });
      }
    });
  }
}
customElement("map-floor-filter", MapFloorFilter);
export {
  MapFloorFilter
};
