'use strict';

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

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

const ezFilterInputCss = ":host{display:block;width:100%}";

const EzFilterInput = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ezChange = index.createEvent(this, "ezChange", 7);
    this.ezSearching = index.createEvent(this, "ezSearching", 7);
    this.ezFocusIn = index.createEvent(this, "ezFocusIn", 7);
    this._searchingText = "";
    this.handleFocus = () => {
      if (this._searchingText === "") {
        this._textInput.value = "";
      }
      else {
        this._textInput.value = this._searchingText;
      }
      this.ezFocusIn.emit();
    };
    this.label = undefined;
    this.value = undefined;
    this.enabled = true;
    this.errorMessage = undefined;
    this.restrict = undefined;
    this.mode = "regular";
    this.asyncSearch = false;
    this.canShowError = true;
    this.autoFocus = false;
  }
  observeLabel() {
    if (this._textInput) {
      this._textInput.label = this.label;
    }
  }
  observeErrorMessage() {
    if (this._textInput) {
      this._textInput.errorMessage = this.errorMessage;
    }
  }
  observeValue(newValue, oldValue) {
    if (this._textInput && newValue != oldValue) {
      this._textInput.value = newValue;
      this.ezChange.emit(newValue);
    }
  }
  /**
   * Aplica o foco no campo.
   */
  async setFocus(options) {
    this._textInput.setFocus(options);
  }
  /**
   * Remove o foco do campo.
   */
  async setBlur() {
    this._textInput.setBlur();
  }
  /**
   * Retorna se o conteúdo é inválido.
   */
  async isInvalid() {
    return typeof this.errorMessage === "string" && this.errorMessage.trim() !== "";
  }
  /**
   * Método responsável por setar um novo valor ao campo.
   */
  async setValue(newValue) {
    if (!this.asyncSearch || this._textInput == undefined) {
      return;
    }
    if (newValue !== this.value) {
      this.value = newValue;
      this._searchingText = "";
    }
    else {
      this._textInput.value = newValue;
    }
  }
  /**
   * Método responsável por resetar o valor do campo para o ultimo valor inputado.
   */
  async endSearch() {
    if (!this.asyncSearch || this._textInput == undefined) {
      return;
    }
    if (this._textInput.value !== this.value) {
      this._textInput.value = this.value;
    }
  }
  handleChange(evt) {
    const newValue = evt.detail;
    this.errorMessage = "";
    if (this.asyncSearch) {
      this._searchingText = newValue;
      this.ezSearching.emit(newValue);
    }
    else if (newValue !== this.value) {
      this.value = newValue;
    }
  }
  componentDidLoad() {
    CSSVarsUtils.CSSVarsUtils.applyVarsTextInput(this._elem, this._textInput);
    if (this.autoFocus) {
      requestAnimationFrame(() => {
        this.setFocus({ selectText: true });
      });
    }
  }
  render() {
    core.ElementIDUtils.addIDInfoIfNotExists(this._elem, 'input');
    return (index.h("ez-text-input", { "data-element-id": core.ElementIDUtils.getInternalIDInfo("textInput"), ref: elem => this._textInput = elem, label: this.label, onEzChange: evt => this.handleChange(evt), value: this.value, enabled: this.enabled, errorMessage: this.errorMessage, restrict: this.restrict, mode: this.mode, onFocusin: this.handleFocus, canShowError: this.canShowError }, index.h("ez-icon", { slot: "leftIcon", iconName: "search" })));
  }
  get _elem() { return index.getElement(this); }
  static get watchers() { return {
    "label": ["observeLabel"],
    "errorMessage": ["observeErrorMessage"],
    "value": ["observeValue"]
  }; }
};
EzFilterInput.style = ezFilterInputCss;

exports.ez_filter_input = EzFilterInput;
