import { ApplicationContext, ElementIDUtils } from '@sankhyalabs/core';
import { h } from '@stencil/core';
import FilterItemType from '../filter-item-type.enum';
export class SnkFilterSearch {
  constructor() {
    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();
  }
  static get is() { return "snk-filter-search"; }
  static get properties() {
    return {
      "config": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "SnkFilterItemConfig",
          "resolved": "SnkFilterItemConfig",
          "references": {
            "SnkFilterItemConfig": {
              "location": "import",
              "path": "../snk-filter-item"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define as configura\u00E7\u00F5es do componente snk-filter-search"
        }
      },
      "value": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "IOption",
          "resolved": "IOption",
          "references": {
            "IOption": {
              "location": "import",
              "path": "@sankhyalabs/ezui/dist/types/components/ez-combo-box/ez-combo-box"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define o valor do componente snk-filter-search"
        }
      },
      "errorMessage": {
        "type": "string",
        "mutable": true,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Usado para exibir mensagens de erro."
        },
        "attribute": "error-message",
        "reflect": true
      }
    };
  }
  static get events() {
    return [{
        "method": "valueChanged",
        "name": "valueChanged",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Emitido quando o valor do filtro \u00E9 alterado."
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }];
  }
  static get methods() {
    return {
      "setFocus": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Define o foco no componente snk-filter-search.",
          "tags": []
        }
      },
      "clearValue": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Limpa o valor do componente restaurando o valor original da configura\u00E7\u00E3o.",
          "tags": []
        }
      }
    };
  }
  static get elementRef() { return "_element"; }
  static get listeners() {
    return [{
        "name": "ezChange",
        "method": "ezChangeListener",
        "target": undefined,
        "capture": false,
        "passive": false
      }];
  }
}
