import { r as registerInstance, e as createEvent, h, g as getElement } from './index-e9c7ceaa.js';
import { ElementIDUtils, ApplicationContext } from '@sankhyalabs/core';
import { F as FilterItemType } from './filter-item-type.enum-d45e026f.js';

const SnkFilterSearch = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.valueChanged = createEvent(this, "valueChanged", 7);
    this._isSearchPlus = false;
    this.config = undefined;
    this.value = undefined;
    this.errorMessage = undefined;
  }
  /**
   * Emitido quando acontece a alteração de valor do componente snk-filter-search
   */
  async ezChangeListener(_evt) {
    this.errorMessage = undefined;
    if (this._isSearchPlus) {
      this.value = await this._searchInput.getValueAsync();
    }
    else {
      this.value = this._searchInput.value;
    }
    this.valueChanged.emit(this.value);
  }
  componentDidLoad() {
    if (this._element) {
      ElementIDUtils.addIDInfo(this._element, 'filterContentEditor');
    }
  }
  doSearch(mode, argument, isSearchPlus = false) {
    const application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
    const loader = isSearchPlus ? application.executePreparedSearchPlus : application.executePreparedSearch;
    return new Promise((resolve, reject) => {
      loader.bind(application)(mode, argument, this.config.props.searchContext)
        .then(result => {
        resolve(result);
      }).catch(reason => {
        reject(reason);
      });
    });
  }
  /**
   * Define o foco no componente snk-filter-search.
   */
  async setFocus() {
    this._searchInput.setFocus();
  }
  /**
   * Limpa o valor do componente restaurando o valor original da configuração.
   */
  async clearValue() {
    this.value = this.config.value;
    if (this._searchInput) {
      this._searchInput.value = this.config.value;
    }
  }
  getInputItem() {
    var _a;
    this._isSearchPlus = ((_a = this.config.props) === null || _a === void 0 ? void 0 : _a["useSearchPlus"]) == true;
    const TagName = this._isSearchPlus ? "ez-search-plus" : "ez-search";
    return (h("ez-tooltip", { active: this.config.enabled === false, message: this.config.disabledMessage, type: 'warning' }, h(TagName, { id: this.config.id, suppressEmptyOption: true, value: this.config.value, label: this.config.label, enabled: this.config.enabled, ref: ref => this._searchInput = ref, optionLoader: ({ mode, argument }) => this.doSearch(mode, argument, this._isSearchPlus), errorMessage: this.errorMessage })));
  }
  render() {
    if (!this.config || this.config.type !== FilterItemType.SEARCH) {
      return undefined;
    }
    return this.getInputItem();
  }
  get _element() { return getElement(this); }
};

export { SnkFilterSearch as snk_filter_search };
