import { r as v, c as p, h as d, H as M } from "./reactcomponent-44b70bf6.mjs";
import { c as f, g as I } from "./_commonjsHelpers-ba3f0406-b96e6d6a.mjs";
import "react";
import "react-dom";
var g = f(function(e, t) {
  Object.defineProperty(t, "__esModule", {
    value: !0
  }), t.default = r, t.debounce = a;
  var s = 500;
  t.DEFAULT_DEBOUNCE_DURATION = s;
  function r(n) {
    return function(o, u, l) {
      return {
        configurable: !0,
        enumerable: l.enumerable,
        get: function() {
          return Object.defineProperty(this, u, {
            configurable: !0,
            enumerable: l.enumerable,
            value: a(l.value, n)
          }), this[u];
        }
      };
    };
  }
  function a(n) {
    var i = arguments.length <= 1 || arguments[1] === void 0 ? s : arguments[1], o = void 0;
    function u() {
      for (var l = this, c = arguments.length, h = Array(c), m = 0; m < c; m++)
        h[m] = arguments[m];
      u.clear(), o = setTimeout(function() {
        o = null, n.apply(l, h);
      }, i);
    }
    return u.clear = function() {
      o && (clearTimeout(o), o = null);
    }, u;
  }
}), x = f(function(e, t) {
  Object.defineProperty(t, "__esModule", {
    value: !0
  });
  function s(i, o) {
    var u = o({}, i);
    return delete u.default, u;
  }
  function r(i, o) {
    for (var u = Object.getOwnPropertyNames(o), l = 0; l < u.length; l++) {
      var c = u[l], h = Object.getOwnPropertyDescriptor(o, c);
      h && h.configurable && i[c] === void 0 && Object.defineProperty(i, c, h);
    }
    return i;
  }
  function a(i) {
    return i && i.__esModule ? i : { default: i };
  }
  var n = a(g);
  t.default = n.default, r(t, s(g, r));
});
const D = /* @__PURE__ */ I(x), E = 'mi-search{display:block;position:relative;}mi-search input[type=search]{border-radius:8px;background-color:#ffffff;padding-left:40px;height:48px;font-size:1rem;padding-right:40px;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";-webkit-appearance:none;-moz-appearance:none;appearance:none;width:100%;position:relative;background-image:url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTgiIGhlaWdodD0iMTgiIHZpZXdCb3g9IjAgMCAxOCAxOCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0xMS43MSAxMUgxMi41TDE3LjQ5IDE2TDE2IDE3LjQ5TDExIDEyLjVWMTEuNzFMMTAuNzMgMTEuNDNDOS41OSAxMi40MSA4LjExIDEzIDYuNSAxM0MyLjkxIDEzIDAgMTAuMDkgMCA2LjVDMCAyLjkxIDIuOTEgMCA2LjUgMEMxMC4wOSAwIDEzIDIuOTEgMTMgNi41QzEzIDguMTEgMTIuNDEgOS41OSAxMS40MyAxMC43M0wxMS43MSAxMVpNMiA2LjVDMiA4Ljk5IDQuMDEgMTEgNi41IDExQzguOTkgMTEgMTEgOC45OSAxMSA2LjVDMTEgNC4wMSA4Ljk5IDIgNi41IDJDNC4wMSAyIDIgNC4wMSAyIDYuNVoiIGZpbGw9IiM4RDk4QUEiLz4KPC9zdmc+Cg==");background-repeat:no-repeat;background-position:16px center;border-color:#E6E6E6;color:var(--tailwind-colors-neutral-500);border:1px solid var(--tailwind-colors-gray-200)}mi-search input[type=search]:focus{border-color:#3b82f6;box-shadow:0 0 0 2px #cfe0fc;}mi-search input[type=search][disabled]{pointer-events:none}mi-search input::-webkit-search-cancel-button{-webkit-appearance:none}mi-search input::-ms-clear{display:none}mi-search input::-moz-search-clear-button{-moz-appearance:none}mi-search button{position:absolute;right:14px;top:50%;transform:translateY(-50%);width:20px;height:20px;background-color:#6b7280;border-radius:50%;padding:0;border-width:0;display:flex;align-items:center;justify-content:center}mi-search button:focus{outline:0}mi-search button svg{fill:white;width:10px;height:10px}';
var A = function(e, t, s, r) {
  var a = arguments.length, n = a < 3 ? t : r === null ? r = Object.getOwnPropertyDescriptor(t, s) : r, i;
  if (typeof Reflect == "object" && typeof Reflect.decorate == "function")
    n = Reflect.decorate(e, t, s, r);
  else
    for (var o = e.length - 1; o >= 0; o--)
      (i = e[o]) && (n = (a < 3 ? i(n) : a > 3 ? i(t, s, n) : i(t, s)) || n);
  return a > 3 && n && Object.defineProperty(t, s, n), n;
};
const b = class {
  constructor(e) {
    v(this, e), this.results = p(this, "results", 7), this.cleared = p(this, "cleared", 7), this.componentRendered = p(this, "componentRendered", 7), this.shortInput = p(this, "shortInput", 7), this.changed = p(this, "changed", 7), this.preventSearch = !1, this.placeholder = "", this.idAttribute = "", this.dataAttributes = {}, this.mapsindoors = !1, this.google = !1, this.mapbox = !1, this.language = "en", this.miFields = "name,description,aliases,categories,externalId", this.miTake = void 0, this.miSkip = void 0, this.miOrder = void 0, this.miCategories = void 0, this.miNear = void 0, this.miVenue = void 0, this.gmCountryCode = void 0, this.value = void 0, this.disabled = !1, this.sessionToken = void 0, this.userPosition = void 0;
  }
  valueChange(e) {
    !e || !this.inputElement || e !== this.inputElement.value && (this.inputElement.value = e, this.preventSearch || this.inputChanged());
  }
  /**
   * Clear the input field.
   */
  async clear() {
    this.inputElement.value = "", this.value = "", this.lastRequested = null, this.cleared.emit();
  }
  /**
   * Programmatically trigger the search.
   */
  triggerSearch() {
    const e = this.inputElement.value;
    this.search(e);
  }
  /**
   * Sets text to be shown in the search field.
   * Setting it will not perform a search.
   */
  setDisplayText(e) {
    this.preventSearch = !0, this.inputElement.value = e, this.value = e, this.preventSearch = !1;
  }
  /**
   * Set focus on the input field.
   * The preventScroll boolean is passed as true to prevent the browser
   * from scrolling the document to bring the newly-focused element into view.
   */
  focusInput() {
    this.inputElement.focus({ preventScroll: !0 });
  }
  /**
   * Get hold of the search input field.
   */
  async getInputField() {
    return this.inputElement;
  }
  /**
   * Perform the search.
   */
  search(e) {
    Promise.all([
      this.makeMapsIndoorsQuery(e),
      this.makeGooglePlacesQuery(e),
      this.getMapboxSearchResults(e)
    ]).then((t) => {
      this.lastRequested = e, this.google ? this.pushResults(t[0].concat(t[1])) : this.mapbox && mapboxgl.accessToken ? this.pushResults(t[0].concat(t[2])) : this.pushResults(t[0]);
    });
  }
  /**
   * Handles incoming input change event, eg. input field value has changed.
   * The function is debounced 500ms to avoid firing too many requests while typing.
   */
  inputChanged() {
    const e = this.inputElement.value;
    if (this.value = e, !this.preventSearch) {
      if (e.length < 2) {
        this.lastRequested = null, e.length === 1 ? this.shortInput.emit() : this.clear();
        return;
      }
      e.length > 1 && e !== this.lastRequested && this.search(e), this.changed.emit();
    }
  }
  /**
   * Push the results via the results event.
   * @param object[] Locations
   */
  pushResults(e) {
    this.results.emit(e);
  }
  /**
   * Make MapsIndoors locations request based on given search query.
   * @param {string} query
   * @return {Promise<any[]>}
   */
  makeMapsIndoorsQuery(e) {
    if (!this.mapsindoors)
      return Promise.resolve([]);
    let t;
    if (this.miNear && (t = this.miNear, /^(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)$/.test(this.miNear))) {
      const s = this.miNear.split(",");
      t = { lat: parseFloat(s[0]), lng: parseFloat(s[1]) };
    }
    return mapsindoors.services.LocationsService.getLocations({
      q: e.trim(),
      fields: this.miFields,
      take: this.miTake,
      skip: this.miSkip,
      orderBy: this.miOrder,
      near: t,
      venue: this.miVenue,
      categories: this.miCategories
    });
  }
  /**
   * Make Google Places autocomplete suggestion request.
   *
   * @param {string} query
   * @return {Promise<any>}
   */
  makeGooglePlacesQuery(e) {
    return this.google ? this.googleAutocompleteService || (this.googleAutocompleteService = new google.maps.places.AutocompleteService()) : this.googleAutocompleteService = null, this.googleAutocompleteService ? new Promise((t) => {
      const s = {
        input: e,
        language: this.language
      };
      this.gmCountryCode && (s.componentRestrictions = { country: this.gmCountryCode }), this.googleAutocompleteService.getPlacePredictions(s, (r) => {
        const a = (r || []).map((n) => ({
          id: n.place_id,
          type: "Feature",
          properties: {
            type: "google_places",
            placeId: n.place_id,
            name: n.structured_formatting.main_text,
            subtitle: n.structured_formatting.secondary_text || "",
            floor: 0
          }
        }));
        t(a);
      });
    }) : Promise.resolve([]);
  }
  /**
   * Get Mapbox Places results.
   *
   * @param {string} query
   * @return {Promise<any>}
   */
  getMapboxSearchResults(e) {
    if (this.mapbox && mapboxgl.accessToken) {
      if (e)
        return new Promise((t) => {
          const s = this.language.split("-")[0];
          let r = `https://api.mapbox.com/search/searchbox/v1/suggest?q=${e}&session_token=${this.sessionToken}&access_token=${mapboxgl.accessToken}&language=${s}`;
          this.userPosition && (r = r.concat(`&proximity=${this.userPosition}`)), fetch(r).then((a) => a.json()).then((a) => {
            const n = a.suggestions.map((i) => ({
              id: i.mapbox_id,
              type: "Feature",
              properties: {
                type: "mapbox_places",
                placeId: i.mapbox_id,
                name: i.name,
                subtitle: i.place_formatted || "",
                floor: 0
              }
            }));
            t(n);
          }).catch(() => {
            t([]);
          });
        });
    } else
      return Promise.resolve([]);
  }
  componentDidRender() {
    if (this.dataAttributes)
      for (const e in this.dataAttributes)
        this.inputElement.setAttribute(e, this.dataAttributes[e]);
    this.componentRendered.emit();
  }
  render() {
    var e;
    return d(M, null, d("input", { disabled: this.disabled, id: this.idAttribute ? this.idAttribute : null, type: "search", ref: (t) => this.inputElement = t, onInput: () => this.inputChanged(), placeholder: this.placeholder, autocomplete: "off" }), ((e = this.inputElement) === null || e === void 0 ? void 0 : e.value) && d("button", { type: "button", onClick: () => this.clear(), "aria-label": "Clear" }, d("svg", { width: "14", height: "14", viewBox: "0 0 14 14", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, d("path", { d: "M14 1.41L12.59 0L7 5.59L1.41 0L0 1.41L5.59 7L0 12.59L1.41 14L7 8.41L12.59 14L14 12.59L8.41 7L14 1.41Z" }))));
  }
  static get watchers() {
    return {
      value: ["valueChange"]
    };
  }
};
A([
  D(500)
], b.prototype, "inputChanged", null);
b.style = E;
export {
  b as mi_search
};
//# sourceMappingURL=mi-search.entry-78c390f2.mjs.map
