import { OjInputSearchBase } from './OjInputSearchBase';
import { mixinClear } from '../../lib/input-utils';

/**
 * The component WebElement for [oj-input-search](../../jsdocs/oj.ojInputSearch.html).
 * Do not instantiate this class directly, instead, use
 * [ojInputSearch](../functions/elements.ojInputSearch-1.html).
 */
export class OjInputSearch extends OjInputSearchBase {
  /**
   * Sets the value of "value" property.
   * The value of the element.
   * @param value The value to set for "value"
   * @override
   */
  async changeValue(value: string) {
    // Call focus() on the root element
    await this.getDriver().executeScript('arguments[0].focus()', this);
    await this.whenBusyContextReady();
    await this.getDriver().executeAsyncScript(
      `
      var element = arguments[0];
      var value = arguments[1];

      // Last argument will the done function
      var doneFunc = arguments[arguments.length - 1];

      element._testChangeValue(value)
        .then(doneFunc, doneFunc)
        .catch(doneFunc);
    `,
      this,
      value
    );
  }

  /**
   * Sets the value of "value" property based on data for the given key from the data provider.
   * The value of the element.
   * @param key The data provider key for which to set the "value"
   */
  async changeValueByKey(key: any) {
    // Call focus() on the root element
    await this.getDriver().executeScript('arguments[0].focus()', this);
    await this.whenBusyContextReady();
    await this.getDriver().executeAsyncScript(
      `
      var element = arguments[0];
      var key = arguments[1];

      // Last argument will the done function
      var doneFunc = arguments[arguments.length - 1];

      element._testChangeValueByKey(key)
        .then(doneFunc, doneFunc)
        .catch(doneFunc);
    `,
      this,
      key
    );
  }
}
mixinClear(OjInputSearch);
