'use strict';

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

const index = require('./index-df541290.js');

const styleCss = ":host{max-height:300px;width:85%;max-height:var(--max-menu-height);border:1px solid black;-webkit-box-sizing:border-box;box-sizing:border-box;background-color:rgb(var(--smoothly-default-shade));overflow-y:auto;cursor:pointer}div:last-child:not(:empty){padding:0.5em 1em;font-style:italic}";

const SmoothlyMenuOptions = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.filteredOptions = [];
    this.highlightIndex = 0;
    this.emptyMenuLabel = "No Options";
    this.order = false;
    this.options = [];
  }
  optionsChangeHandler(newOptions) {
    this.highlightIndex = 0;
  }
  optionHoverHandler(event) {
    if (event.detail.value)
      this.setHighlight(event.detail.value);
    event.stopPropagation();
  }
  async moveHighlight(step) {
    if (this.filteredOptions.length > 0)
      this.setHighlight((this.highlightIndex + step + this.filteredOptions.length) % this.filteredOptions.length, true);
  }
  async setHighlight(newIndex, scrollToHighlight = false) {
    if (typeof newIndex == "number") {
      this.highlightIndex = newIndex;
      scrollToHighlight && this.scrollTo(this.highlightIndex);
    }
    else {
      const value = newIndex;
      let isHighlightSet = false;
      this.filteredOptions.forEach((option, index) => {
        const highlight = option.value == value;
        this.highlightIndex = highlight ? index : this.highlightIndex;
        highlight && scrollToHighlight && this.scrollTo(index);
        isHighlightSet = highlight ? true : isHighlightSet;
      });
      if (!isHighlightSet)
        this.setHighlight(0, true);
    }
  }
  async getHighlighted() {
    let result = undefined;
    if (this.highlightIndex != undefined && this.filteredOptions.length > 0)
      result = this.filteredOptions[this.highlightIndex];
    return result;
  }
  async filterOptions(keyword, excludeValues = []) {
    const keywordLowercase = keyword.toLowerCase();
    this.filteredOptions = [];
    for (const option of this.options) {
      const names = option.name + (option.aliases ? option.aliases.join(" ") : "");
      const isVisible = names.toLowerCase().includes(keywordLowercase) && !excludeValues.includes(option.value);
      isVisible && this.filteredOptions.push(option);
    }
    this.order && this.sortOptions(keyword);
  }
  sortOptions(keyword) {
    const keywordLowercase = keyword.toLowerCase();
    this.filteredOptions.sort((a, b) => {
      return this.getPriorityScore(a, keywordLowercase) - this.getPriorityScore(b, keywordLowercase);
    });
  }
  getPriorityScore(option, keywordLowercase) {
    let result = Number.MAX_SAFE_INTEGER;
    const name = option.name;
    const aliases = option.aliases ? option.aliases.join(" ") : "";
    const names = `${name ? name : ""} ${aliases ? aliases : ""}`.toLowerCase();
    if (name) {
      result = names.split(" ").reduce((minIndex, word) => {
        const index = word.indexOf(keywordLowercase);
        return index != -1 && index < minIndex ? index : minIndex;
      }, Number.MAX_SAFE_INTEGER);
    }
    return result;
  }
  scrollTo(optionIndex) {
    var _a;
    const optionHeight = (_a = this.firstOptionsElement) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().height;
    if (optionHeight) {
      const menuHeight = this.element.getBoundingClientRect().height;
      const scrollPosition = this.element.scrollTop;
      const optionPosition = optionIndex * optionHeight;
      const numOptionsInMenu = Math.floor(menuHeight / optionHeight);
      if (optionPosition - optionHeight / 2 < scrollPosition)
        this.element.scrollTo({ top: (optionIndex - 1) * optionHeight });
      else if (scrollPosition < optionPosition + optionHeight - menuHeight)
        this.element.scrollTo({ top: (optionIndex + 1 - numOptionsInMenu) * optionHeight });
    }
  }
  render() {
    return (index.h(index.Host, { style: { "--max-menu-height": this.maxMenuHeight } }, this.filteredOptions.length > 0 ? (this.filteredOptions.map((option, index$1) => (index.h("smoothly-option", { style: this.optionStyle, ref: el => index$1 == 0 && (this.firstOptionsElement = el !== null && el !== void 0 ? el : this.firstOptionsElement), value: option.value, name: option.name, "data-highlight": this.highlightIndex == index$1 }, option.description)))) : (index.h("div", null, this.emptyMenuLabel))));
  }
  get element() { return index.getElement(this); }
  static get watchers() { return {
    "options": ["optionsChangeHandler"]
  }; }
};
SmoothlyMenuOptions.style = styleCss;

exports.smoothly_menu_options = SmoothlyMenuOptions;
