import Component from '../../component.js';
import OptionComponent from '../../option.js';

export default class CoUxFormControlSelect extends Component {
    constructor(parent) {
        let selector = 'select[@data-e2e = "ux-form-control-sel-rw"]';
        super(parent, selector);
        //https://citnet.tech.ec.europa.eu/CITnet/stash/projects/CSDR/repos/eui/browse/packages/components/src/lib/ux-components/ux-form-control/ux-form-control.component.html
    }
    getSelectOptionByIndex(index) {
        return '(' + this.currentPath + '//option' + ')[' + index + ']';
    }
    getOptionByValue(value) {
        let path = '//option[@value="' + value + '"]';
        return this.currentPath + path;
    }
    getOptionByText(text) {
        let path = '//option[contains(string(), "' + text + '")]';
        return this.currentPath + path;
    }
    async optionListHasGroups() {
        let cl = await (await $(this.currentPath + '//optgroup')).isExisting();
        if (cl) {
            return true;
        } else {
            return false;
        }
    }
    getGroupByIndex(index) {
        return '(' + this.currentPath + '//optgroup' + ')[' + index + ']';
    }
    getGroupOptionByIndex(gIndex, oIndex) {
        return '(' + this.getGroupByIndex(gIndex) + '//option'+ ')[' + oIndex + ']';
    }
    getGroupByLabel(label) {
        return this.currentPath + '//optgroup[@label = "' + label + '"]';
    }
    getGroupOptionByValue(label, value) {
        return this.getGroupByLabel(label) + '//option[@value = "' + value + '"]';
    }

    async isDisabled() {
        let at = await (await $(this.currentPath)).getAttribute('Disabled');
        if ( at === 'true' ) {
            return true;
        } else if (at === null) {
            return false;
        } else { throw new Error(
            '<CoUxFormControlSelect> <isDisabled> method needs to be revised!');
        }
    }

    get optionCo(){
        return new OptionComponent(this.currentPath);
    }
}


