'use strict';

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

const index = require('./index-a7b0c73d.js');
const core = require('@sankhyalabs/core');
const searchFormatters = require('./searchFormatters-b7e1ed1e.js');

const ezPopoverPlusCss = ":host{display:none}";

const EzPopoverPlus = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ezVisibilityChange = index.createEvent(this, "ezVisibilityChange", 7);
    this.autoClose = true;
    this.boxWidth = "fit-content";
    this.opened = false;
    this.overlayType = "light";
    this.anchorElement = undefined;
    this.options = { horizontalGap: 0, verticalGap: 0, fromRight: false };
    this.useAnchorSize = false;
    this.minWidth = 150;
  }
  async observeOpened(newValue) {
    if (newValue && !this._popOverCore) {
      this.renderPopoverCore();
    }
    this._popOverCore.opened = newValue;
  }
  /**
   * Atualiza a posição do popover.
   */
  async updatePosition(top, left) {
    var _a;
    await ((_a = this._popOverCore) === null || _a === void 0 ? void 0 : _a.updatePosition(top, left));
  }
  /**
   * Exibe o ez-popover.
   */
  async show(top, left) {
    if (!this._popOverCore) {
      this.renderPopoverCore();
    }
    await this._popOverCore.show(top, left);
  }
  /**
   * Ancora a exibição do popOver a um elemento HTML.
   */
  async showUnder(element, options) {
    if (!this._popOverCore) {
      this.renderPopoverCore();
    }
    await this._popOverCore.showUnder(element, options);
  }
  /**
   * Oculta o ez-popover.
   */
  async hide() {
    var _a;
    await ((_a = this._popOverCore) === null || _a === void 0 ? void 0 : _a.hide());
  }
  /**
   * Altera as opções.
  */
  async setOptions(options) {
    var _a;
    await ((_a = this._popOverCore) === null || _a === void 0 ? void 0 : _a.setOptions(options));
  }
  /**
   * Altera o elemento de ancoragem.
  */
  async setAnchorElement(anchor) {
    var _a;
    await ((_a = this._popOverCore) === null || _a === void 0 ? void 0 : _a.setAnchorElement(anchor));
    this.anchorElement = anchor;
  }
  getFirstElement(collection) {
    if (Array.isArray(collection) && collection.length) {
      return collection[0];
    }
    if (!Array.isArray(collection) && collection.item) {
      return collection.item(0);
    }
    return null;
  }
  buildElementId(keys) {
    const filteredKeys = keys.filter((key) => !!key);
    return core.ElementIDUtils.getInternalIDInfo(filteredKeys.join("_"));
  }
  renderPopoverCore() {
    var _a;
    this._popOverCore = document.createElement('ez-popover-core');
    this._popOverCore.opened = this.opened;
    this._popOverCore.anchorElement = this.anchorElement;
    this._popOverCore.autoClose = this.autoClose;
    this._popOverCore.boxWidth = this.boxWidth;
    this._popOverCore.overlayType = this.overlayType;
    this._popOverCore.options = this.options;
    this._popOverCore.useAnchorSize = this.useAnchorSize;
    this._popOverCore.minWidth = this.minWidth;
    this._popOverCore.setAttribute("data-element-id", this.buildElementId(["ez-popover-core", this._host.getAttribute("data-element-id")]));
    this._popOverCore.addEventListener('ezVisibilityChange', ({ detail }) => {
      this.ezVisibilityChange.emit(detail);
      this.opened = detail;
    });
    const firstChild = this.getFirstElement(this._host.children);
    if (firstChild) {
      this._popOverCore.appendChild(firstChild);
    }
    else {
      console.warn('O slot do popover está vazio. Adicione conteúdo ao slot para exibição correta.');
    }
    const root = document.body.querySelector("#root");
    const firstDiv = this.getFirstElement(document.body.getElementsByTagName("div"));
    const parentElement = (_a = root !== null && root !== void 0 ? root : firstDiv) !== null && _a !== void 0 ? _a : document.body;
    parentElement.appendChild(this._popOverCore);
  }
  render() {
    return (index.h(index.Host, null, index.h("slot", null)));
  }
  get _host() { return index.getElement(this); }
  static get watchers() { return {
    "opened": ["observeOpened"]
  }; }
};
EzPopoverPlus.style = ezPopoverPlusCss;

const multiSelectionBoxMessageCss = ".multi-selection__message{display:flex;flex-direction:row;align-items:center;justify-content:center;width:100%;min-height:320px;text-align:center}.multi-selection__text-message{margin-top:-30px;font-family:var(--font-pattern);font-size:var(--text--meduim);padding:var(--space-xs);color:var(--text--primary)}";

const MultiSelectionBoxMessage = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.message = undefined;
  }
  render() {
    return (index.h("div", { class: "multi-selection__message" }, index.h("span", { class: "multi-selection__text-message" }, this.message)));
  }
};
MultiSelectionBoxMessage.style = multiSelectionBoxMessageCss;

const searchListCss = ":host{--ez-search__list-title--primary:var(--title--primary, #2B3A54);--ez-search__list-text--primary:var(--text--primary, #626e82);--ez-search__list-height:calc(var(--ez-search--font-size) + var(--ez-search--space--medium) + 4px);--ez-search__list-min-width:64px;--ez-search__scrollbar--color-default:var(--scrollbar--default, #626e82);--ez-search__scrollbar--color-background:var(--scrollbar--background, #E5EAF0);--ez-search__scrollbar--color-hover:var(--scrollbar--hover, #2B3A54);--ez-search__scrollbar--color-clicked:var(--scrollbar--clicked, #a2abb9);--ez-search__scrollbar--border-radius:var(--border--radius-small, 6px);--ez-search__scrollbar--width:var(--space--small, 6px);--ez-search--border-radius-small:var(--border--radius-small, 6px);--ez-search--font-size:var(--text--medium, 14px);--ez-search--font-family:var(--font-pattern, Arial);--ez-search--font-weight--large:var(--text-weight--large, 500);--ez-search--font-weight--medium:var(--text-weight--medium, 400);--ez-search--background-medium:var(--background--medium, #f0f3f7);--ez-search--line-height:calc(var(--text--medium, 14px) + 4px)}.list-options{position:relative;z-index:var(--more-visible, 2);margin:var(--space--small) 0px;padding:0px;padding-inline-start:0px;box-sizing:border-box;width:100%;height:100%;max-height:350px;min-width:150px;display:flex;flex-direction:column;scroll-behavior:smooth;overflow-y:auto;overflow-x:hidden;scrollbar-width:thin;scrollbar-color:var(--ez-search__scrollbar--color-clicked) var(--ez-search__scrollbar--color-background)}.list-options::-webkit-scrollbar{background-color:var(--scrollbar--background);width:var(--space--small);max-width:var(--space--small);min-width:var(--space--small);height:var(--space--small);max-height:var(--space--small);min-height:var(--space--small)}.list-options::-webkit-scrollbar-track{background-color:var(--ez-search__scrollbar--color-background);border-radius:var(--ez-search__scrollbar--border-radius)}.list-options::-webkit-scrollbar-thumb{background-color:var(--ez-search__scrollbar--color-default);border-radius:var(--ez-search__scrollbar--border-radius)}.list-options::-webkit-scrollbar-thumb:vertical:hover,.list-options::-webkit-scrollbar-thumb:horizontal:hover{background-color:var(--ez-search__scrollbar--color-hover)}.list-options::-webkit-scrollbar-thumb:vertical:active,.list-options::-webkit-scrollbar-thumb:horizontal:active{background-color:var(--ez-search__scrollbar--color-clicked)}.message{text-align:center;display:flex;justify-content:center;align-items:center;list-style-type:none;margin:var(--space--medium) 0px;min-height:var(--ez-search__list-height)}.message__no-result{color:var(--ez-search__list-title--primary);font-family:var(--ez-search--font-family);font-size:var(--ez-search--font-size)}.message__loading{border-radius:50%;width:14px;height:14px;-webkit-animation:spin 1s linear infinite;animation:spin 1s linear infinite;border:3px solid var(--ez-search__list-title--primary);border-top:3px solid transparent}.item__value,.item__label{flex-basis:auto;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color:var(--ez-search__list-title--primary);font-family:var(--ez-search--font-family);font-size:var(--ez-search--font-size);line-height:var(--ez-search--line-height)}.item__label{font-weight:var(--ez-search--font-weight--medium)}.item__label--bold{font-weight:var(--ez-search--font-weight--large)}.item__value{text-align:center;color:var(--ez-search__list-text--primary);font-weight:var(--ez-search--font-weight--large)}.item__value--hidden{visibility:hidden;position:absolute;white-space:nowrap;z-index:-1;top:0;left:0}.item__label{text-align:left}.item__list>li:hover{background-color:var(--ez-search--background-medium)}.item{display:flex;align-items:center;width:100%;box-sizing:border-box;list-style-type:none;cursor:pointer;border-radius:var(--ez-search--border-radius-small);gap:var(--space--small, 6px)}.preselected{background-color:var(--background--medium)}@-webkit-keyframes spin{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}@supports not (scrollbar-width: thin){.item{padding-right:8px}}";

const SearchList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.changeValue = index.createEvent(this, "changeValue", 7);
    this.changePreSelection = index.createEvent(this, "changePreSelection", 7);
    this._startHighlightTag = "<span class='card-item__highlight'>";
    this._endHighlightTag = "</span>";
    this.MAX_HEIGHT = 350;
    this.HEADER_MENU_HEIGHT = 60;
    this.showLoading = undefined;
    this.visibleOptions = [];
    this.textEmptyList = undefined;
    this.canShowListOptions = undefined;
    this.value = undefined;
    this.showOptionValue = true;
    this.preSelection = undefined;
  }
  async nextOption() {
    const newPreSelection = this.preSelection === undefined ? 0 : Math.min(this.preSelection + 1, this.visibleOptions.length - 1);
    this.changePreSelection.emit(newPreSelection);
    this.scrollToOption(this.visibleOptions[newPreSelection]);
  }
  async previousOption() {
    const newPreSelection = this.preSelection === undefined ? 0 : Math.max(this.preSelection - 1, 0);
    this.changePreSelection.emit(newPreSelection);
    this.scrollToOption(this.visibleOptions[newPreSelection]);
  }
  selectOption(newOption) {
    const newOptionsReplaced = Object.assign(Object.assign({}, newOption), { value: searchFormatters.replaceHighlight(newOption === null || newOption === void 0 ? void 0 : newOption.value), label: searchFormatters.replaceHighlight(newOption === null || newOption === void 0 ? void 0 : newOption.label) });
    const newOptionsFormatted = Object.assign(Object.assign({}, newOptionsReplaced), { value: searchFormatters.replaceQuotes(newOptionsReplaced === null || newOptionsReplaced === void 0 ? void 0 : newOptionsReplaced.value), label: searchFormatters.replaceQuotes(newOptionsReplaced === null || newOptionsReplaced === void 0 ? void 0 : newOptionsReplaced.label) });
    this.changeValue.emit(newOptionsFormatted);
  }
  createOption(detail) {
    let { key, title } = detail;
    const startHighlight = new RegExp(this._startHighlightTag, 'g');
    const endHighlight = new RegExp(this._endHighlightTag, 'g');
    title = core.StringUtils.decodeHtmlEntities(title);
    const option = {
      value: key === null || key === void 0 ? void 0 : key.replace(startHighlight, '').replace(endHighlight, ''),
      label: title === null || title === void 0 ? void 0 : title.replace(startHighlight, '').replace(endHighlight, '')
    };
    this.selectOption(option);
  }
  scrollToOption(opt) {
    window.requestAnimationFrame(() => {
      const liElem = (opt === null || opt === void 0 ? void 0 : opt.value)
        ? this._optionsList.querySelector(`div#item_${opt.value.replace(/([ #;&,.+*~':"!^$[\]()=<>|/\\])/g, '\\$1')}`)
        : undefined;
      if (liElem) {
        liElem.scrollIntoView({ behavior: "smooth", block: "nearest" });
      }
    });
  }
  buildItem(opt, index$1) {
    opt.label = opt.label || opt.value;
    const card = {
      key: opt.value,
      title: opt.label,
      details: opt.details
    };
    return index.h("div", { style: { "height": "100%" }, class: index$1 === this.preSelection ? "item preselected" : "item", id: `item_${opt.value}`, onMouseDown: () => this.createOption(card), onMouseOver: () => this.changePreSelection.emit(index$1) }, index.h("ez-card-item", { item: card, compacted: true, enableKey: this.showOptionValue }));
  }
  componentDidLoad() {
    if (this._optionsList) {
      this._optionsList.style.maxHeight = `${this.MAX_HEIGHT}px`;
    }
  }
  componentDidUpdate() {
    if (this._optionsList) {
      const rect = this._optionsList.getBoundingClientRect();
      const spaceBelow = window.innerHeight - rect.bottom;
      if (spaceBelow < 0) {
        this.adjustMaxHeight(rect);
      }
    }
  }
  adjustMaxHeight(rect) {
    if (this._optionsList) {
      const spaceAbove = rect.top - this.HEADER_MENU_HEIGHT;
      const maxHeight = this.MAX_HEIGHT;
      const height = Math.min(spaceAbove, maxHeight);
      this._optionsList.style.maxHeight = `${height}px`;
    }
  }
  cleanEmptyObjectArray(arr) {
    return arr.filter(item => {
      return Object.values(item).some(valor => valor !== undefined);
    });
  }
  render() {
    return (index.h(index.Host, null, index.h("ul", { class: "list-options", ref: elem => this._optionsList = elem }, !this.showLoading
      && this.cleanEmptyObjectArray(this.visibleOptions).length === 0
      && index.h("div", { class: "message" }, index.h("span", { class: "message__no-result" }, this.textEmptyList)), this.showLoading
      && index.h("div", { class: "message" }, index.h("div", { class: "message__loading" })), this.canShowListOptions && this.visibleOptions.map((opt, index) => this.buildItem(opt, index)))));
  }
};
SearchList.style = searchListCss;

exports.ez_popover_plus = EzPopoverPlus;
exports.multi_selection_box_message = MultiSelectionBoxMessage;
exports.search_list = SearchList;
