/* 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 Search from "@arcgis/core/widgets/Search";
import FeatureLayer from "@arcgis/core/layers/FeatureLayer";
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;
}

.search-widget {
  width: 100% !important;
  border: 1px solid var(--calcite-color-border-input);
}`;
class MapSearch extends LitElement {
  constructor() {
    super(...arguments);
    this.popupEnabled = false;
    this.resultGraphicEnabled = false;
    this.searchTerm = "";
    this._searchElement = createRef();
  }
  static {
    this.properties = { mapView: [0, {}, { attribute: false }], popupEnabled: [5, {}, { type: Boolean }], resultGraphicEnabled: [5, {}, { type: Boolean }], searchConfiguration: [0, {}, { attribute: false }], searchTerm: 1, searchWidget: [0, {}, { attribute: false }] };
  }
  static {
    this.styles = styles;
  }
  willUpdate(changes) {
    if (changes.has("searchConfiguration")) {
      void this.watchSearchConfigurationHandler();
    }
    if (changes.has("mapView")) {
      void this.mapViewWatchHandler();
    }
  }
  async watchSearchConfigurationHandler() {
    this._initSearchWidget();
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this._initSearchWidget();
    });
  }
  loaded() {
    return this._initSearchWidget();
  }
  render() {
    return html`<div class="search-widget" ${ref(this._searchElement)}></div>`;
  }
  _initSearchWidget() {
    if (this.mapView && this._searchElement.value && !this.searchWidget) {
      let searchOptions = {
        view: this.mapView,
        container: this._searchElement.value,
        searchTerm: this.searchTerm
      };
      if (this.searchConfiguration) {
        const searchConfiguration = this._getSearchConfig(this.searchConfiguration, this.mapView);
        searchOptions = {
          ...searchConfiguration
        };
      }
      this.searchWidget = new Search(searchOptions);
      if (this.searchWidget) {
        this.searchWidget.popupEnabled = this.popupEnabled;
        this.searchWidget.resultGraphicEnabled = this.resultGraphicEnabled;
      }
    } else {
      if (this.searchWidget) {
        this.searchWidget.view = this.mapView;
      }
    }
  }
  _getSearchConfig(searchConfiguration, view) {
    const INCLUDE_DEFAULT_SOURCES = "includeDefaultSources";
    const sources = searchConfiguration.sources;
    if (sources && sources?.length > 0) {
      searchConfiguration[INCLUDE_DEFAULT_SOURCES] = false;
      sources.forEach((source) => {
        const isLayerSource = source.hasOwnProperty("layer");
        if (isLayerSource) {
          const layerSource = source;
          const layerId = layerSource.layer?.id;
          const layerFromMap = layerId ? view.map?.findLayerById(layerId) : null;
          const layerUrl = layerSource?.layer?.url;
          if (layerFromMap) {
            layerSource.layer = layerFromMap;
          } else if (layerUrl) {
            layerSource.layer = new FeatureLayer(layerUrl);
          }
        }
      });
      sources?.forEach((source) => {
        const isLocatorSource = source.hasOwnProperty("locator");
        if (isLocatorSource) {
          const locatorSource = source;
          if (locatorSource?.name === "ArcGIS World Geocoding Service") {
            const outFields = locatorSource.outFields || ["Addr_type", "Match_addr", "StAddr", "City"];
            locatorSource.outFields = outFields;
            locatorSource.singleLineFieldName = "SingleLine";
          }
          locatorSource.url = locatorSource.url;
          delete locatorSource.url;
        }
      });
    } else {
      searchConfiguration = {
        ...searchConfiguration,
        includeDefaultSources: true
      };
    }
    return searchConfiguration;
  }
}
customElement("map-search", MapSearch);
export {
  MapSearch
};
