'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-df541290.js');
require('./model-88c24d18.js');
const index$1 = require('./index-0b6f1dd9.js');
require('./index-c12b8888.js');
require('./_commonjsHelpers-10109b76.js');
require('./index-fe328b96.js');
require('./index-b7493032.js');

const styleCss = ".sc-ig-autocomplete-input-h{display:block;font-family:var(--smoothly-font-family)}.sc-ig-autocomplete-input-h>div.sc-ig-autocomplete-input:nth-child(1){position:absolute;display:grid;grid-template-columns:auto auto;grid-template-rows:auto auto;padding:1em 0.2em 0.2em 0.2em;white-space:nowrap}.sc-ig-autocomplete-input-h>div.sc-ig-autocomplete-input:nth-child(1)>div.sc-ig-autocomplete-input:nth-child(1){color:transparent;pointer-events:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;font-size:1rem;font-family:var(--smoothly-font-family)}.sc-ig-autocomplete-input-h>div.sc-ig-autocomplete-input:nth-child(1) smoothly-menu-options.sc-ig-autocomplete-input{grid-column-start:2;grid-row-start:2;z-index:99}.sc-ig-autocomplete-input-h:not(:focus-within) smoothly-menu-options.sc-ig-autocomplete-input{display:none}.sc-ig-autocomplete-input-h:not([has-suggestion]) smoothly-menu-options.sc-ig-autocomplete-input,.sc-ig-autocomplete-input-h:not([show-suggestion]) smoothly-menu-options.sc-ig-autocomplete-input{display:none}.sc-ig-autocomplete-input-h smoothly-input.sc-ig-autocomplete-input input.sc-ig-autocomplete-input{color:rgb(var(--smoothly-default-contrast))}";

const IntergiroAutocompleteInput = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.lastValue = "";
    this.options = [];
    this.showSuggestion = false;
    this.value = "";
  }
  componentDidRender() {
    var _a;
    (_a = this.menuElement) === null || _a === void 0 ? void 0 : _a.filterOptions("");
  }
  inputValueChangeHandler(event) {
    if (event.detail) {
      this.value = event.detail.value;
      this.setInvisibleDiv(event.detail.value);
    }
  }
  optionSelectHandler(event) {
    if (event.detail) {
      this.inputElement.setKeepFocusOnReRender(true);
      this.select(event.detail.value);
    }
    event.stopPropagation();
  }
  onKeyDown(event) {
    var _a;
    if (event.key == "ArrowUp" || event.key == "ArrowDown")
      this.showSuggestion
        ? this.menuElement.moveHighlight(event.key == "ArrowUp" ? -1 : 1)
        : (this.showSuggestion = true);
    else if (event.key == "Escape")
      this.showSuggestion = false;
    else if (event.key == "Enter" && this.showSuggestion) {
      (_a = this.menuElement) === null || _a === void 0 ? void 0 : _a.getHighlighted().then((result) => {
        result && this.select(result.value);
      });
      this.options && this.options.length > 0 && event.preventDefault();
    }
  }
  onKeyUp(event) {
    if (this.inputElement.value != this.lastValue) {
      this.lastValue = this.inputElement.value;
      this.suggest();
    }
  }
  suggest() {
    if (this.inputElement && this.inputElement.value != undefined && this.menuElement && this.template) {
      const completion = index$1.dist.Type.complete(this.template, this.inputElement.value);
      const oldOptions = this.options;
      this.options = completion.map(c => {
        var _a;
        return { value: c, name: (_a = c.addon) !== null && _a !== void 0 ? _a : "", description: c.description };
      });
      const optionsChanged = oldOptions.map(o => o.value.full).join(",") != this.options.map(o => o.value.full).join(",");
      if (optionsChanged)
        this.showSuggestion = true;
      this.menuElement.setHighlight(0);
    }
  }
  select(newSelection) {
    const newString = newSelection.full;
    this.inputElement.value = newString;
    this.setInvisibleDiv(newString);
    const cursorPosition = newSelection.cursor && typeof newSelection.cursor == "number" ? newSelection.cursor : newString.length;
    this.inputElement.setSelectionRange(cursorPosition, cursorPosition);
  }
  setInvisibleDiv(newValue) {
    if (newValue)
      this.invisibleDiv.innerHTML = newValue.replace(/ /g, "&nbsp;");
    if (newValue == "")
      this.invisibleDiv.innerHTML = "&nbsp;";
  }
  render() {
    return (index.h(index.Host, { "has-suggestion": this.options && this.options.length > 0, "show-suggestion": this.showSuggestion, onClick: () => {
        this.showSuggestion = true;
        this.suggest();
      }, onKeyDown: (e) => this.onKeyDown(e), onKeyUp: (e) => this.onKeyUp(e) }, index.h("div", null, index.h("div", { ref: (el) => (this.invisibleDiv = el) }, "\u00A0"), index.h("smoothly-menu-options", { ref: (el) => (this.menuElement = el), style: { width: "300px" }, "empty-menu-label": "", "max-menu-height": "200px", options: this.options })), index.h("smoothly-input", { ref: (el) => (this.inputElement = el !== null && el !== void 0 ? el : this.inputElement), name: this.name, autocomplete: false, value: this.value }, index.h("slot", null))));
  }
};
IntergiroAutocompleteInput.style = styleCss;

exports.ig_autocomplete_input = IntergiroAutocompleteInput;
