/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as a } from "../../chunks/runtime.js";
import { c as r } from "../../chunks/mapViewUtils.js";
import { LitElement as c, createEvent as s, noShadowRoot as l } from "@arcgis/lumina";
import { css as h } from "lit";
import n from "@arcgis/core/layers/GraphicsLayer.js";
import p from "@arcgis/core/widgets/Sketch/SketchViewModel.js";
const m = h`@layer{:host{display:block;pointer-events:all!important}}`;
class d extends c {
  constructor() {
    super(...arguments), this.selectedLayerId = "", this.tool = "rectangle", this.drawingComplete = s(), this.featuresSelected = s();
  }
  static {
    this.properties = { mapView: 0, selectedLayerId: 1, tool: 1 };
  }
  static {
    this.styles = m;
  }
  static {
    this.shadowRootOptions = l;
  }
  willUpdate(e) {
    e.has("mapView") && this.mapViewWatchHandler();
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this._initSketchViewModel();
    });
  }
  async toggleSelectionTool(e, t) {
    if (this._graphicsLayer?.removeAll(), this._selectionTool?.state === "active" && this._selectionTool.cancel(), t) {
      const i = e ?? this.tool;
      await this._selectionTool?.create(i);
    }
  }
  async loaded() {
    await this.init();
  }
  render() {
    return "";
  }
  async init() {
    this.mapView && this._initSketchViewModel();
  }
  _initSketchViewModel() {
    this._graphicsLayer = new n({
      listMode: "hide"
    }), this._selectionTool = new p({
      layer: this._graphicsLayer,
      view: this.mapView
    }), this._selectionTool && this._selectionTool.on("create", (e) => {
      if (e.state === "complete") {
        this.drawingComplete.emit();
        const t = e.graphic?.geometry;
        t && this._selectFeatures(t), this._graphicsLayer && this._graphicsLayer.removeAll();
      }
    }), this.mapView?.map && this._graphicsLayer && this.mapView.map.layers.add(this._graphicsLayer);
  }
  async _selectFeatures(e) {
    const t = this.mapView && await r(this.mapView, this.selectedLayerId);
    try {
      const i = { geometry: e }, o = t && await t.queryObjectIds(i);
      this.featuresSelected.emit(o);
    } catch (i) {
      console.error("Error selecting features:", i);
    }
  }
}
a("selection-tool", d);
export {
  d as SelectionTool
};
