/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { c as getFeatureLayerView } from "../../chunks/mapViewUtils.js";
import { LitElement, createEvent, noShadowRoot } from "@arcgis/lumina";
import { css } from "lit";
import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
import SketchViewModel from "@arcgis/core/widgets/Sketch/SketchViewModel";
const styles = css`@layer{
/** @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;
  pointer-events: all !important;
}
}`;
class SelectionTool extends LitElement {
  constructor() {
    super(...arguments);
    this.selectedLayerId = "";
    this.tool = "rectangle";
    this.drawingComplete = createEvent();
    this.featuresSelected = createEvent();
  }
  static {
    this.properties = { mapView: [0, {}, { attribute: false }], selectedLayerId: 1, tool: 1 };
  }
  static {
    this.styles = styles;
  }
  static {
    this.shadowRootOptions = noShadowRoot;
  }
  willUpdate(changes) {
    if (changes.has("mapView")) {
      this.mapViewWatchHandler();
    }
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this._initSketchViewModel();
    });
  }
  async toggleSelectionTool(tool, isActive) {
    this._graphicsLayer?.removeAll();
    if (this._selectionTool?.state === "active") {
      this._selectionTool.cancel();
    }
    if (isActive) {
      const selectedTool = tool ?? this.tool;
      this._selectionTool?.create(selectedTool);
    }
  }
  async loaded() {
    await this.init();
  }
  render() {
    return "";
  }
  async init() {
    if (this.mapView) {
      this._initSketchViewModel();
    }
  }
  _initSketchViewModel() {
    this._graphicsLayer = new GraphicsLayer({
      listMode: "hide"
    });
    this._selectionTool = new SketchViewModel({
      layer: this._graphicsLayer,
      view: this.mapView
    });
    if (this._selectionTool) {
      this._selectionTool.on("create", (e) => {
        if (e.state === "complete") {
          this.drawingComplete.emit();
          const queryGeometry = e.graphic?.geometry;
          if (queryGeometry) {
            void this._selectFeatures(queryGeometry);
          }
          if (this._graphicsLayer) {
            this._graphicsLayer.removeAll();
          }
        }
      });
    }
    if (this.mapView?.map && this._graphicsLayer) {
      this.mapView.map.layers.add(this._graphicsLayer);
    }
  }
  async _selectFeatures(geometry) {
    const layerView = this.mapView && await getFeatureLayerView(this.mapView, this.selectedLayerId);
    try {
      const query = { geometry };
      const ids = layerView && await layerView.queryObjectIds(query);
      this.featuresSelected.emit(ids);
    } catch (error) {
      console.error("Error selecting features:", error);
    }
  }
}
customElement("selection-tool", SelectionTool);
export {
  SelectionTool
};
