import Component from './component.js'
import LabelComponent from './label.js'

export default class InputComponent extends Component {
    /**
    * @param {string} parent
    */
    constructor(parent, selector) {
      if (selector) {
          super(parent, selector);
      } else {
          super(parent, 'input');
      }
    }
    async labelCo(){
      return await new LabelComponent(this.parent).byFor(await (await this.element()).getAttribute('id'));
    }
    /**
    * Sets the current component element to the one with the given label
    * The parent remains unchanged
    * @param {string} label
    * @type InputComponent
    */
    byLabel(label){
      let forAttr = this.parent + '//label/descendant-or-self::*[text()[normalize-space()="' + label +
        '"]]/ancestor-or-self::label[1]/@for';
      this.currentPath = '(' + this.parent + '//' + this.selector + ')[@id=(' + forAttr +  ')]';
      return this;
    }
    /**
    * Sets the current component element to an xpath selector where the given attribute equals the given value [@attr="value"] and returns the updated component.
    * The parent remains unchanged
    * @param type string
    * @type Component
    */
    byType(type) {
      this.currentPath = this.parent + '//' + this.selector + '[@type="' + type + '"]';
      return this;
    }

    async isSelected() {
      return await (await this.element()).isSelected();
    }

    async isDisabled() {
      return await (await (await this.element()).getAttribute('disabled') != null);
    }
}
