/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as W } from "../../chunks/runtime.js";
import { u as L } from "../../chunks/useT9n.js";
import { css as S, html as r, nothing as f } from "lit";
import { createRef as n, ref as h } from "lit-html/directives/ref.js";
import { LitElement as F, createEvent as $, safeClassMap as a } from "@arcgis/lumina";
import E from "@arcgis/core/widgets/Expand";
import C from "@arcgis/core/widgets/Home";
const x = S`:host{display:block}.display-none{display:none}.display-flex{display:flex}.margin-top-1-2{margin-top:.5rem}.margin-left-1-2{margin-left:.5rem}.margin-right-1-2{margin-right:.5rem}.square-32{width:32px;height:32px}.square-40{width:40px;height:40px}.square-48{width:48px;height:48px}.border-bottom{border-bottom:1px solid var(--calcite-color-border-3)}.box-shadow{box-shadow:0 1px 2px #0000004d}.margin-bottom-1-2{margin-bottom:.5rem}.danger-icon-color{--calcite-icon-color: var(--calcite-color-status-danger)}`;
class M extends F {
  constructor() {
    super(), this.enabledLayerIds = [], this.enableLegend = !1, this.enableLayerList = !1, this.enableFloorFilter = !1, this.enableFullscreen = !1, this.enableSearch = !1, this.enableBasemap = !1, this.enableHome = !1, this.enableSingleExpand = !1, this.zoomToolsSize = "m", this.layout = "vertical", this.mapWidgetsSize = "m", this.enableMapPicker = !1, this.enableMapLayerPicker = !1, this.position = "top-leading", this.stackTools = !0, this.enableSelectionTool = !1, this.toolOrder = ["home", "legend", "search", "fullscreen", "floorfilter", "layers-list", "selectiontool", "map-picker", "map-layer-picker"], this.selectionTools = ["rectangle-select", "lasso-select", "clear-selection"], this.mapInfos = [], this.defaultLayerId = "", this.isMobile = !1, this.selectedFeaturesIds = [], this.selectedWebMapId = "", this.showOnlyUpdatableLayers = !1, this.visibilityIcon = "eyeball", this._hasFloorInfo = !1, this._translations = L({ name: "map-tools", blocking: !0 }), this._showTools = !0, this._showBasemapWidget = !1, this._showFloorFilter = !1, this._showFullscreen = !1, this._showLegendWidget = !1, this._showSearchWidget = !1, this._showLayersListWidget = !1, this._rectangleSelectionToolActive = !1, this._lassoSelectionToolActive = !1, this._showEllipsisTools = !0, this._ellipsisTools = [], this._isValidWebMap = !0, this._showSelectionTools = !1, this._basemapElement = n(), this._floorFilterElement = n(), this._fullscreenElement = n(), this._layerListWidgetElement = n(), this._legendElement = n(), this._searchElement = n(), this._widgets = [], this._selectionToolElement = n(), this._mapTools = [], this._mapTool = n(), this.clearSelection = $(), this.listenOn(window, "mapChanged", this.mapChanged), this.listenOn(window, "layerSelectionChange", this._layerSelectionChange);
  }
  static {
    this.properties = { basemapConfig: 0, enabledLayerIds: 0, enableLegend: 5, enableLayerList: 5, enableFloorFilter: 5, enableFullscreen: 5, enableSearch: 5, enableBasemap: 5, enableHome: 5, enableSingleExpand: 5, zoomToolsSize: 1, layout: 1, mapView: 0, mapWidgetsSize: 1, enableMapPicker: 5, enableMapLayerPicker: 5, position: 1, searchConfiguration: 0, stackTools: 5, enableSelectionTool: 5, toolOrder: 0, selectionTools: 0, selectedLayer: 0, mapInfos: 0, defaultLayerId: 1, isMobile: 5, parentComponentHeight: 9, parentComponentWidth: 9, selectedFeaturesIds: 0, selectedWebMapId: 1, showOnlyUpdatableLayers: 5, mapInfo: 0, visibilityIcon: 1, _hasFloorInfo: 16, _translations: 16, _showTools: 16, _showBasemapWidget: 16, _showFloorFilter: 16, _showFullscreen: 16, _showLegendWidget: 16, _showSearchWidget: 16, _showLayersListWidget: 16, _rectangleSelectionToolActive: 16, _lassoSelectionToolActive: 16, _showEllipsisTools: 16, _ellipsisTools: 16, _isValidWebMap: 16, _showSelectionTools: 16 };
  }
  static {
    this.styles = x;
  }
  willUpdate(s) {
    s.has("mapView") && this.mapViewWatchHandler(), s.has("parentComponentWidth") && this.parentComponentWidth && this.parentComponentWidthWatchHandler(this.parentComponentWidth), s.has("_showBasemapWidget") && (this.hasUpdated || this._showBasemapWidget !== !1) && this._showBasemapWidgetWatchHandler(this._showBasemapWidget), s.has("_showLayersListWidget") && (this.hasUpdated || this._showLayersListWidget !== !1) && this._showLayersListWidgetWatchHandler(this._showLayersListWidget), s.has("_showFloorFilter") && (this.hasUpdated || this._showFloorFilter !== !1) && this._showFloorFilterWatchHandler(this._showFloorFilter), s.has("_showFullscreen") && (this.hasUpdated || this._showFullscreen !== !1) && this._showFullscreenWatchHandler(this._showFullscreen), s.has("_showLegendWidget") && (this.hasUpdated || this._showLegendWidget !== !1) && this._showLegendWidgetWatchHandler(this._showLegendWidget), s.has("_showSearchWidget") && (this.hasUpdated || this._showSearchWidget !== !1) && this._showSearchWidgetWatchHandler(this._showSearchWidget);
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this.enableHome && this._initHome(), this.parentComponentWidth && this._updateTools(this.parentComponentWidth);
    });
  }
  async parentComponentWidthWatchHandler(s) {
    this._updateTools(s);
  }
  async _showBasemapWidgetWatchHandler(s) {
    const e = this._basemapElement.value ? this._basemapElement.value.basemapWidget : void 0, t = document.dir === "rtl" ? "rtl" : "ltr";
    e && (s ? (this.mapView?.ui.add(e, {
      position: this.position,
      index: 1
    }), e.container?.style.setProperty("direction", t)) : this.mapView?.ui.remove(e));
  }
  async _showLayersListWidgetWatchHandler(s) {
    const e = this._layerListWidgetElement.value ? this._layerListWidgetElement.value.layerListWidget : void 0, t = document.dir === "rtl" ? "rtl" : "ltr";
    e && (s ? (this.mapView?.ui.add(e, {
      position: this.position,
      index: 1
    }), e.container?.style.setProperty("direction", t)) : this.mapView?.ui.remove(e));
  }
  async _showFloorFilterWatchHandler(s) {
    const e = this._floorFilterElement.value ? this._floorFilterElement.value.floorFilterWidget : void 0, t = document.dir === "rtl" ? "rtl" : "ltr";
    e && (s ? (this.mapView?.ui.add(e, {
      position: this.position,
      index: 1
    }), e.container?.style.setProperty("direction", t)) : this.mapView?.ui.remove(e));
  }
  async _showFullscreenWatchHandler(s) {
    const e = this._fullscreenElement.value ? this._fullscreenElement.value.fullscreenWidget : void 0;
    e && (s ? e.viewModel.state === "ready" && e.viewModel.enter() : e.viewModel.state === "active" && e.viewModel.exit());
  }
  async _showLegendWidgetWatchHandler(s) {
    const e = this._legendElement.value ? this._legendElement.value.legendWidget : void 0, t = document.dir === "rtl" ? "rtl" : "ltr";
    e && (s ? (this.mapView?.ui.add(e, {
      position: this.position,
      index: 1
    }), e.container?.style.setProperty("direction", t)) : this.mapView?.ui.remove(e));
  }
  async _showSearchWidgetWatchHandler(s) {
    const e = this._searchElement.value ? this._searchElement.value.searchWidget : void 0, t = document.dir === "rtl" ? "rtl" : "ltr";
    e && (s ? (this.mapView?.ui.add(e, {
      position: this.position,
      index: 1
    }), e.container?.style.setProperty("direction", t)) : this.mapView?.ui.remove(e));
  }
  async mapChanged(s) {
    const e = s.detail.isValid;
    this._isValidWebMap = e, this._showTools || this._toggleTools();
  }
  async _layerSelectionChange(s) {
    this._showTools || this._toggleTools();
  }
  async load() {
    this._mapTools = [...this.toolOrder];
  }
  render() {
    this._setZoomToolsSize();
    const s = this.layout === "horizontal" ? "display-flex" : "", e = this._getToggleIcon(), t = !this._showTools && !this._showSelectionTools ? "display-none" : s, o = this._showEllipsisTools ? "" : "display-none", l = this._showSearchWidget ? "" : "display-none", i = this._showBasemapWidget ? "" : "display-none", c = this._showLegendWidget ? "" : "display-none", d = this._showFloorFilter ? "" : "display-none", p = this._showFullscreen ? "" : "display-none", m = this._showLayersListWidget ? "" : "display-none", w = this._showTools ? this._translations.collapse : this._translations.expand, u = !this.enableBasemap && !this.enableFullscreen && !this.enableLegend && !this.enableLayerList && !this.enableSearch && !this.enableMapPicker && !this.enableMapLayerPicker ? "display-none" : "", y = document.dir === "rtl" ? "margin-right-1-2" : "margin-left-1-2", b = this.enableSingleExpand ? this.layout === "horizontal" ? y : "margin-top-1-2" : "", T = this._ellipsisTools.length > 0 && this._showTools ? "" : "display-none", _ = this.stackTools && this._ellipsisTools.length === 0 ? "box-shadow" : "", v = this._showSelectionTools ? this.selectionTools : this._mapTools;
    return r`<div class=${`${u} ${s}`}>${this.enableSingleExpand ? r`<div class=${a(_)}>${this._getActionGroup(e, !1, w || "", () => this._toggleTools())}</div>` : void 0}<div class=${`${b} ${_} ${t}`}>${this._getMapWidgets(v)}</div><div><div class=${a(T)}>${this._getActionGroup("ellipsis", !1, "", () => this._toggleEllipsisTools())}<div class=${a(o)}>${this._getMapWidgets(this._ellipsisTools)}</div></div></div></div><map-basemap-gallery .basemapConfig=${this.basemapConfig} class=${a(i)} .mapView=${this.mapView} ${h(this._basemapElement)}></map-basemap-gallery><map-search class=${a(l)} .mapView=${this.mapView} result-graphic-enabled .searchConfiguration=${this.searchConfiguration} ${h(this._searchElement)}></map-search><map-legend class=${a(c)} .mapView=${this.mapView} ${h(this._legendElement)}></map-legend><map-fullscreen class=${a(p)} .mapView=${this.mapView} @fullscreenStateChange=${(g) => this._fullscreenStateChange(g.detail)} ${h(this._fullscreenElement)}></map-fullscreen><layer-list-widget class=${a(m)} .mapView=${this.mapView} @fullscreenStateChange=${(g) => this._fullscreenStateChange(g.detail)} .visibilityIcon=${this.visibilityIcon} ${h(this._layerListWidgetElement)}></layer-list-widget><map-floor-filter class=${a(d)} .enabled=${this.enableFloorFilter} .mapView=${this.mapView} ${h(this._floorFilterElement)}></map-floor-filter><selection-tool .mapView=${this.mapView} @drawingComplete=${() => {
      this._rectangleSelectionToolActive = !1, this._lassoSelectionToolActive = !1;
    }} .selectedLayerId=${this.selectedLayer?.id} ${h(this._selectionToolElement)}></selection-tool>${this.enableMapPicker && r`<map-picker class=mapPickerClass .height=${(this.parentComponentHeight ?? 0) - 25} id=map-picker .mapInfos=${this.mapInfos} reference-element=map .selectedWebMapId=${this.selectedWebMapId} show-map-list-in-popover></map-picker>` || ""}${this.enableMapLayerPicker && this.selectedLayer && r`<map-layer-picker appearance=transparent .defaultLayerId=${this.defaultLayerId} .enabledLayerIds=${this.enabledLayerIds} .height=${(this.parentComponentHeight ?? 0) - 25} .isMobile=${this.isMobile} .mapView=${this.mapView} .showOnlyUpdatableLayers=${this.showOnlyUpdatableLayers} reference-element=layers scale=s .selectedIds=${this.selectedLayer ? [this.selectedLayer.id] : []} show-layers-list-in-popover show-tables type=dropdown></map-layer-picker>` || ""}`;
  }
  _initHome() {
    this.enableHome && (this._homeWidget = new C({
      view: this.mapView
    }));
  }
  _setZoomToolsSize() {
    const s = document.getElementsByClassName("esri-zoom")[0]?.children[0], e = document.getElementsByClassName("esri-zoom")[0]?.children[1];
    if (s && e) {
      const t = this.zoomToolsSize === "s" ? "32px" : this.zoomToolsSize === "m" ? "40px" : "48px";
      s.style.width = t, s.style.height = t, e.style.width = t, e.style.height = t;
    }
    if (this.layout === "horizontal") {
      const t = document.dir || "ltr", o = t === "ltr" ? "esri-ui-top-left" : "esri-ui-top-right", l = t === "ltr" ? 0 : this.parentComponentWidth - 62;
      let i = t === "ltr" ? this._showTools ? "ltr" : "rtl" : this._showTools ? "rtl" : "ltr";
      document.getElementsByClassName(o)[0]?.firstElementChild?.tagName === "MAP-TOOLS" && !this._showTools && (i = t), document.getElementsByClassName("esri-zoom")[0]?.setAttribute("style", `position: absolute; top: 50px; left: ${l}px`), this._showSelectionTools || document.getElementsByClassName(o)[0]?.setAttribute("style", `flex-direction: row; gap: 5px; align-items: flex-start; direction: ${i} !important; z-index: 1;`);
    }
  }
  _getToggleIcon() {
    const s = document.dir || "ltr";
    return this.layout === "horizontal" ? s === "ltr" ? this._showTools ? "chevrons-left" : "chevrons-right" : this._showTools ? "chevrons-right" : "chevrons-left" : this._showTools ? "chevrons-up" : "chevrons-down";
  }
  _updateTools(s) {
    const e = this._showSelectionTools ? JSON.parse(JSON.stringify(this.selectionTools)) : JSON.parse(JSON.stringify(this.toolOrder));
    this._hasFloorInfo = this.mapView?.map?.floorInfo;
    const t = e.indexOf("floorfilter");
    t > -1 && !this._hasFloorInfo && e.splice(t, 1);
    const o = 40;
    let l = 110;
    this.isMobile && (this.enableMapPicker || this.enableMapLayerPicker) && (l = 160);
    const i = Math.ceil((s - l) / o) - 1;
    i && i !== e.length - 1 ? (this._mapTools = e.slice(0, i), this._ellipsisTools = e.slice(i, e.length)) : (this._mapTools = e, this._ellipsisTools = []);
  }
  _getMapWidgets(s) {
    const e = this._showFullscreen ? "full-screen-exit" : "full-screen", t = this._showFullscreen ? this._translations.exitFullscreen : this._translations.enterFullscreen, o = this._isValidWebMap ? "map" : "exclamation-mark-triangle", l = this.selectedLayer === void 0 ? "exclamation-mark-triangle" : "layers";
    return s.map((i) => {
      switch (i) {
        case "legend":
          return this.enableLegend && this.enableSingleExpand ? this._getActionGroup("legend", !1, this._translations.legend, () => this._showLegend()) : this.enableLegend ? this._getWidget(i, this._legendElement.value?.legendWidget, !0) : void 0;
        case "search":
          return this.enableSearch && this.enableSingleExpand ? this._getActionGroup("magnifying-glass", !1, this._translations.search, () => this._search()) : this.enableSearch ? this._getWidget(i, this._searchElement.value?.searchWidget, !0) : void 0;
        case "fullscreen":
          return this.enableFullscreen && this.enableSingleExpand ? this._getActionGroup(e, !1, t || "", () => this._expand()) : this.enableFullscreen ? this._getWidget(i, this._fullscreenElement.value?.fullscreenWidget, !1) : void 0;
        case "basemap":
          return this.enableBasemap && this.enableSingleExpand ? this._getActionGroup("basemap", !1, this._translations.basemap, () => this._toggleBasemapPicker()) : this.enableBasemap ? this._getWidget(i, this._basemapElement.value?.basemapWidget, !0) : void 0;
        case "layers-list":
          return this.enableLayerList && this.enableSingleExpand ? this._getActionGroup("layers", !1, this._translations.layers || "", () => this._toggleLayerListWidget()) : this.enableLayerList ? this._getWidget(i, this._layerListWidgetElement.value?.layerListWidget, !0) : void 0;
        case "floorfilter":
          return this.enableFloorFilter && this._hasFloorInfo && this.enableSingleExpand ? this._getActionGroup("urban-model", !1, this._translations.floorFilter, () => this._toggleFloorFilter()) : this.enableFloorFilter && this._hasFloorInfo ? this._getWidget(i, this._floorFilterElement.value?.floorFilterWidget, !0) : void 0;
        case "selectiontool":
          return this.enableSelectionTool && this.selectedLayer ? (
            // if selection tool is enabled and a layer is selected only then show selection tool
            this._getActionGroup("cursor-selection", !1, this._translations.selectionTools, this._onSelectionToolClick, !1)
          ) : void 0;
        case "rectangle-select":
          return this.enableSelectionTool && this.selectedLayer ? (
            // if selection tool is enabled and a layer is selected then only show selection tool
            this._getActionGroup("cursor-marquee", !1, this._translations.selectByRectangle, () => this._toggleSelectionTool("rectangle"), !1, this._rectangleSelectionToolActive)
          ) : void 0;
        case "lasso-select":
          return this.enableSelectionTool && this.selectedLayer ? (
            // if selection tool is enabled and a layer is selected then only show selection tool
            this._getActionGroup("lasso-select", !1, this._translations.selectByLasso, () => this._toggleSelectionTool("freehandPolygon"), !1, this._lassoSelectionToolActive)
          ) : void 0;
        case "clear-selection":
          return this.selectedLayer && this.selectedFeaturesIds.length > 0 ? (
            // on mobile show clear selection tool only when a layer is selected
            this._getActionGroup("erase", !1, this._translations.clearSelection, () => this.clearSelection.emit())
          ) : void 0;
        case "home":
          return this.enableHome ? this._getActionGroup("home", !1, this._translations.defaultMapView, () => this._homeWidget?.go()) : void 0;
        case "map-picker":
          return this.enableMapPicker ? this._getActionGroup(o, !1, "", () => {
          }, !this._isValidWebMap) : void 0;
        case "map-layer-picker":
          return this.enableMapLayerPicker && this._isValidWebMap ? this._getActionGroup(l, !1, "", () => {
          }, this.selectedLayer === void 0) : void 0;
        default:
          return "";
      }
    });
  }
  _fullscreenStateChange(s) {
    s === "ready" && this._showFullscreen ? this._showFullscreen = !1 : s === "active" && !this._showFullscreen && (this._showFullscreen = !0);
  }
  _getActionGroup(s, e, t, o, l, i) {
    const c = this.mapWidgetsSize === "s" ? "square-32" : this.mapWidgetsSize === "m" ? "square-40" : "square-48", d = this.stackTools ? "" : "margin-bottom-1-2", p = this.stackTools ? "" : "box-shadow";
    return r`<div><calcite-action alignment=center appearance=solid .indicator=${i} class=${`map-actions ${c} ${d} border-bottom ${p} ${l ? "danger-icon-color" : ""}`} .disabled=${e} .icon=${s} id=${s ?? f} @click=${o} scale=s text ${h(this._mapTool)}><calcite-icon icon=chevron-up scale=s slot=icon></calcite-icon></calcite-action>${!this.isMobile && t && r`<calcite-tooltip .placement=${this.layout === "vertical" ? "auto" : "bottom"} reference-element=${s ?? f}><span>${t}</span></calcite-tooltip>` || ""}</div>`;
  }
  _getWidget(s, e, t) {
    if (this._widgets.indexOf(s) < 0 && this.mapView && e) {
      const o = this.toolOrder.indexOf(s), l = new E({
        view: this.mapView,
        content: e
      }), i = this.enableHome ? 2 : 1;
      this.mapView.ui.add(t ? l : e, {
        position: this.position,
        index: o > -1 ? o + i : 1
      }), this._widgets.push(s);
    }
  }
  _showLegend() {
    this._showLegendWidget = !this._showLegendWidget, this._showTools = !1, this._toggleEllipsisTools();
  }
  _search() {
    this._showSearchWidget = !this._showSearchWidget, this._showTools = !1, this._toggleEllipsisTools();
  }
  _toggleBasemapPicker() {
    this._showBasemapWidget = !this._showBasemapWidget, this._showTools = !1, this._toggleEllipsisTools();
  }
  _toggleLayerListWidget() {
    this._showLayersListWidget = !this._showLayersListWidget, this._showTools = !1, this._toggleEllipsisTools();
  }
  _toggleFloorFilter() {
    this._showFloorFilter = !this._showFloorFilter, this._showTools = !1, this._toggleEllipsisTools();
  }
  _onSelectionToolClick() {
    this._showSelectionTools = !this._showSelectionTools, this._showTools = !1, this._toggleEllipsisTools();
  }
  _toggleSelectionTool(s) {
    s === "rectangle" ? (this._lassoSelectionToolActive = !1, this._rectangleSelectionToolActive = !this._rectangleSelectionToolActive) : s === "freehandPolygon" && (this._rectangleSelectionToolActive = !1, this._lassoSelectionToolActive = !this._lassoSelectionToolActive), this._selectionToolElement.value?.toggleSelectionTool(s, this._rectangleSelectionToolActive || this._lassoSelectionToolActive);
  }
  _expand() {
    this._showFullscreen = !this._showFullscreen;
  }
  _toggleTools() {
    this._showTools || (this._showSelectionTools = !1, this._showBasemapWidget = !1, this._showLayersListWidget = !1, this._showSearchWidget = !1, this._showLegendWidget = !1, this._showFloorFilter = !1), this._rectangleSelectionToolActive ? this._toggleSelectionTool("rectangle") : this._lassoSelectionToolActive && this._toggleSelectionTool("freehandPolygon"), this._showTools = !this._showTools, this._toggleEllipsisTools(), this._updateTools(this.parentComponentWidth);
  }
  _toggleEllipsisTools() {
    this._showEllipsisTools = !this._showEllipsisTools;
  }
}
W("map-tools", M);
export {
  M as MapTools
};
