/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { c as n } from "../../chunks/runtime.js";
import { css as d, html as p } from "lit";
import { createRef as u, ref as m } from "lit-html/directives/ref.js";
import { LitElement as f } from "@arcgis/lumina";
import g from "@arcgis/core/widgets/Search";
import S from "@arcgis/core/layers/FeatureLayer";
const w = d`:host{display:block}.search-widget{width:100%!important;border:1px solid var(--calcite-color-border-input)}`;
class y extends f {
  constructor() {
    super(...arguments), this.popupEnabled = !1, this.resultGraphicEnabled = !1, this.searchTerm = "", this._searchElement = u();
  }
  static {
    this.properties = { mapView: 0, popupEnabled: 5, resultGraphicEnabled: 5, searchConfiguration: 0, searchTerm: 1, searchWidget: 0 };
  }
  static {
    this.styles = w;
  }
  willUpdate(t) {
    t.has("searchConfiguration") && this.watchSearchConfigurationHandler(), t.has("mapView") && this.mapViewWatchHandler();
  }
  async watchSearchConfigurationHandler() {
    this._initSearchWidget();
  }
  async mapViewWatchHandler() {
    await this.mapView?.when(() => {
      this._initSearchWidget();
    });
  }
  loaded() {
    return this._initSearchWidget();
  }
  render() {
    return p`<div class="search-widget" ${m(this._searchElement)}></div>`;
  }
  _initSearchWidget() {
    if (this.mapView && this._searchElement.value && !this.searchWidget) {
      let t = {
        view: this.mapView,
        container: this._searchElement.value,
        searchTerm: this.searchTerm
      };
      this.searchConfiguration && (t = {
        ...this._getSearchConfig(this.searchConfiguration, this.mapView)
      }), this.searchWidget = new g(t), this.searchWidget && (this.searchWidget.popupEnabled = this.popupEnabled, this.searchWidget.resultGraphicEnabled = this.resultGraphicEnabled);
    } else
      this.searchWidget && (this.searchWidget.view = this.mapView);
  }
  _getSearchConfig(t, s) {
    const o = "includeDefaultSources", r = t.sources;
    return r && r?.length > 0 ? (t[o] = !1, r.forEach((i) => {
      if (i.hasOwnProperty("layer")) {
        const e = i, a = e.layer?.id, c = a ? s.map?.findLayerById(a) : null, h = e?.layer?.url;
        c ? e.layer = c : h && (e.layer = new S(h));
      }
    }), r?.forEach((i) => {
      if (i.hasOwnProperty("locator")) {
        const e = i;
        if (e?.name === "ArcGIS World Geocoding Service") {
          const a = e.outFields || ["Addr_type", "Match_addr", "StAddr", "City"];
          e.outFields = a, e.singleLineFieldName = "SingleLine";
        }
        e.url = e.url, delete e.url;
      }
    })) : t = {
      ...t,
      includeDefaultSources: !0
    }, t;
  }
}
n("map-search", y);
export {
  y as MapSearch
};
