import Component  from '../component.js';
import CoUxControlFeedback from './ux-control-feedback.js';

export default class CoUxFormControlInputText extends Component {
    constructor(parent) {
        let selector = 'span[@data-e2e ="ux-form-control-tf-rw"]';
        super(parent, selector);
        //https://citnet.tech.ec.europa.eu/CITnet/stash/projects/CSDR/repos/eui/browse/packages/core/src/lib/commons/ux-components/ux-form-control/ux-form-control.component.html?at=refs%2Fheads%2Fsupport%2F7.x
    }
    get input() {
        let path = '//input[contains(@class, "form-control")]'
        return this.currentPath + path;
    }
    async isInputTextDisabled() {
        let at = await (await $(this.input)).getAttribute('Disabled');
        if ( at === 'true' ) {
            return true;
        } else if (at === null) {
            return false;
        } else { throw new Error(
            'ux-form-control <isInputTextDisabled> method needs to be revised!');
        }
    }
    async hasMaxLength() {
        let cl = await (await $(this.input)).getAttribute('maxlength');
        let cl2 = await(await $(this.currentPath + '/ancestor-or-self::ux-form-control[1]')).getAttribute('maxlength');
        if (cl != null || cl2 != null) {
            return true;
        } else {
            return false;
        }
    }
    async maxLength() {
        let cl = await (await $(this.input)).getAttribute('maxlength');
        let cl2 = await (await $(this.currentPath + '/ancestor-or-self::ux-form-control[1]')).getAttribute('maxlength');
        if (cl != null ) {
            return await (await $(this.input)).getAttribute('maxlength');
        } else if ( cl2 != null ) {
            return await (await $(this.currentPath + '/ancestor-or-self::ux-form-control[1]')).getAttribute('maxlength');
        } else { throw new Error(
            'ux-form-control <getMaxLength> method needs to be revised!');
        }
    }
    get maxLengthIndicator() {
        let path = '//span[contains(@class, "input-maxlength")]';
        return this.currentPath + path;
    }
    get controlFeedbackCO() {
        return new CoUxControlFeedback(this.currentPath + '/ancestor-or-self::ux-form-control[1]');
    }
    async hasError() {
        return await (await $(this.controlFeedbackCO.currentPath)).isExisting();
    }
}


