import * as tslib_1 from "tslib";
import { WidgetBase } from '@dojo/framework/core/WidgetBase';
import { ThemedMixin, theme } from '@dojo/framework/core/mixins/Themed';
import { FocusMixin } from '@dojo/framework/core/mixins/Focus';
import { v, w } from '@dojo/framework/core/vdom';
import Focus from '../meta/Focus';
import Label from '../label/index';
import { formatAriaProperties } from '../common/util';
import { uuid } from '@dojo/framework/core/util';
import * as css from '../theme/text-area.m.css';
import HelperText from '../helper-text/index';
import { InputValidity } from '@dojo/framework/core/meta/InputValidity';
let TextArea = class TextArea extends ThemedMixin(FocusMixin(WidgetBase)) {
    constructor() {
        super(...arguments);
        this._dirty = false;
        this._uuid = uuid();
    }
    _onBlur(event) {
        this.properties.onBlur && this.properties.onBlur(event.target.value);
    }
    _onChange(event) {
        event.stopPropagation();
        this.properties.onChange &&
            this.properties.onChange(event.target.value);
    }
    _onClick(event) {
        event.stopPropagation();
        this.properties.onClick &&
            this.properties.onClick(event.target.value);
    }
    _onFocus(event) {
        this.properties.onFocus &&
            this.properties.onFocus(event.target.value);
    }
    _onInput(event) {
        event.stopPropagation();
        this.properties.onInput &&
            this.properties.onInput(event.target.value);
    }
    _onKeyDown(event) {
        event.stopPropagation();
        this.properties.onKeyDown &&
            this.properties.onKeyDown(event.which, () => {
                event.preventDefault();
            });
    }
    _onKeyPress(event) {
        event.stopPropagation();
        this.properties.onKeyPress &&
            this.properties.onKeyPress(event.which, () => {
                event.preventDefault();
            });
    }
    _onKeyUp(event) {
        event.stopPropagation();
        this.properties.onKeyUp &&
            this.properties.onKeyUp(event.which, () => {
                event.preventDefault();
            });
    }
    _onMouseDown(event) {
        event.stopPropagation();
        this.properties.onMouseDown && this.properties.onMouseDown();
    }
    _onMouseUp(event) {
        event.stopPropagation();
        this.properties.onMouseUp && this.properties.onMouseUp();
    }
    _onTouchStart(event) {
        event.stopPropagation();
        this.properties.onTouchStart && this.properties.onTouchStart();
    }
    _onTouchEnd(event) {
        event.stopPropagation();
        this.properties.onTouchEnd && this.properties.onTouchEnd();
    }
    _onTouchCancel(event) {
        event.stopPropagation();
        this.properties.onTouchCancel && this.properties.onTouchCancel();
    }
    _callOnValidate(valid, message) {
        let { valid: previousValid } = this.properties;
        let previousMessage;
        if (typeof previousValid === 'object') {
            previousMessage = previousValid.message;
            previousValid = previousValid.valid;
        }
        if (valid !== previousValid || message !== previousMessage) {
            this.properties.onValidate && this.properties.onValidate(valid, message);
        }
    }
    _validate() {
        const { customValidator, value = '' } = this.properties;
        if (value === '' && !this._dirty) {
            this._callOnValidate(undefined, '');
            return;
        }
        this._dirty = true;
        let { valid, message = '' } = this.meta(InputValidity).get('input', value);
        if (valid && customValidator) {
            const customValid = customValidator(value);
            if (customValid) {
                valid = customValid.valid;
                message = customValid.message || '';
            }
        }
        this._callOnValidate(valid, message);
    }
    get validity() {
        const { valid = { valid: undefined, message: undefined } } = this.properties;
        if (typeof valid === 'boolean') {
            return { valid, message: undefined };
        }
        return {
            valid: valid.valid,
            message: valid.message
        };
    }
    getRootClasses() {
        const { disabled, readOnly, required } = this.properties;
        const focus = this.meta(Focus).get('root');
        const { valid } = this.validity;
        return [
            css.root,
            disabled ? css.disabled : null,
            focus.containsFocus ? css.focused : null,
            valid === false ? css.invalid : null,
            valid === true ? css.valid : null,
            readOnly ? css.readonly : null,
            required ? css.required : null
        ];
    }
    render() {
        const { aria = {}, columns = 20, disabled, widgetId = this._uuid, label, maxLength, minLength, name, placeholder, readOnly, required, rows = 2, value, wrapText, theme, classes, labelHidden, helperText, onValidate } = this.properties;
        const focus = this.meta(Focus).get('root');
        onValidate && this._validate();
        const { valid, message } = this.validity;
        const computedHelperText = (valid === false && message) || helperText;
        return v('div', {
            key: 'root',
            classes: this.theme(this.getRootClasses())
        }, [
            label
                ? w(Label, {
                    theme,
                    classes,
                    disabled,
                    focused: focus.containsFocus,
                    invalid: valid === false || undefined,
                    readOnly,
                    required,
                    hidden: labelHidden,
                    forId: widgetId
                }, [label])
                : null,
            v('div', { classes: this.theme(css.inputWrapper) }, [
                v('textarea', Object.assign({ id: widgetId, key: 'input' }, formatAriaProperties(aria), { classes: this.theme(css.input), cols: `${columns}`, disabled, focus: this.shouldFocus, 'aria-invalid': valid === false ? 'true' : null, maxlength: maxLength ? `${maxLength}` : null, minlength: minLength ? `${minLength}` : null, name,
                    placeholder,
                    readOnly, 'aria-readonly': readOnly ? 'true' : null, required, rows: `${rows}`, value, wrap: wrapText, onblur: this._onBlur, onchange: this._onChange, onclick: this._onClick, onfocus: this._onFocus, oninput: this._onInput, onkeydown: this._onKeyDown, onkeypress: this._onKeyPress, onkeyup: this._onKeyUp, onmousedown: this._onMouseDown, onmouseup: this._onMouseUp, ontouchstart: this._onTouchStart, ontouchend: this._onTouchEnd, ontouchcancel: this._onTouchCancel }))
            ]),
            w(HelperText, { text: computedHelperText, valid })
        ]);
    }
};
TextArea = tslib_1.__decorate([
    theme(css)
], TextArea);
export { TextArea };
export default TextArea;

/*# sourceMappingURL=index.mjs.map*/