"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var WidgetBase_1 = require("@dojo/framework/core/WidgetBase");
var Themed_1 = require("@dojo/framework/core/mixins/Themed");
var Focus_1 = require("@dojo/framework/core/mixins/Focus");
var vdom_1 = require("@dojo/framework/core/vdom");
var Focus_2 = require("../meta/Focus");
var index_1 = require("../label/index");
var util_1 = require("../common/util");
var util_2 = require("@dojo/framework/core/util");
var css = require("../theme/text-area.m.css");
var index_2 = require("../helper-text/index");
var InputValidity_1 = require("@dojo/framework/core/meta/InputValidity");
var TextArea = /** @class */ (function (_super) {
    tslib_1.__extends(TextArea, _super);
    function TextArea() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._dirty = false;
        _this._uuid = util_2.uuid();
        return _this;
    }
    TextArea.prototype._onBlur = function (event) {
        this.properties.onBlur && this.properties.onBlur(event.target.value);
    };
    TextArea.prototype._onChange = function (event) {
        event.stopPropagation();
        this.properties.onChange &&
            this.properties.onChange(event.target.value);
    };
    TextArea.prototype._onClick = function (event) {
        event.stopPropagation();
        this.properties.onClick &&
            this.properties.onClick(event.target.value);
    };
    TextArea.prototype._onFocus = function (event) {
        this.properties.onFocus &&
            this.properties.onFocus(event.target.value);
    };
    TextArea.prototype._onInput = function (event) {
        event.stopPropagation();
        this.properties.onInput &&
            this.properties.onInput(event.target.value);
    };
    TextArea.prototype._onKeyDown = function (event) {
        event.stopPropagation();
        this.properties.onKeyDown &&
            this.properties.onKeyDown(event.which, function () {
                event.preventDefault();
            });
    };
    TextArea.prototype._onKeyPress = function (event) {
        event.stopPropagation();
        this.properties.onKeyPress &&
            this.properties.onKeyPress(event.which, function () {
                event.preventDefault();
            });
    };
    TextArea.prototype._onKeyUp = function (event) {
        event.stopPropagation();
        this.properties.onKeyUp &&
            this.properties.onKeyUp(event.which, function () {
                event.preventDefault();
            });
    };
    TextArea.prototype._onMouseDown = function (event) {
        event.stopPropagation();
        this.properties.onMouseDown && this.properties.onMouseDown();
    };
    TextArea.prototype._onMouseUp = function (event) {
        event.stopPropagation();
        this.properties.onMouseUp && this.properties.onMouseUp();
    };
    TextArea.prototype._onTouchStart = function (event) {
        event.stopPropagation();
        this.properties.onTouchStart && this.properties.onTouchStart();
    };
    TextArea.prototype._onTouchEnd = function (event) {
        event.stopPropagation();
        this.properties.onTouchEnd && this.properties.onTouchEnd();
    };
    TextArea.prototype._onTouchCancel = function (event) {
        event.stopPropagation();
        this.properties.onTouchCancel && this.properties.onTouchCancel();
    };
    TextArea.prototype._callOnValidate = function (valid, message) {
        var previousValid = this.properties.valid;
        var previousMessage;
        if (typeof previousValid === 'object') {
            previousMessage = previousValid.message;
            previousValid = previousValid.valid;
        }
        if (valid !== previousValid || message !== previousMessage) {
            this.properties.onValidate && this.properties.onValidate(valid, message);
        }
    };
    TextArea.prototype._validate = function () {
        var _a = this.properties, customValidator = _a.customValidator, _b = _a.value, value = _b === void 0 ? '' : _b;
        if (value === '' && !this._dirty) {
            this._callOnValidate(undefined, '');
            return;
        }
        this._dirty = true;
        var _c = this.meta(InputValidity_1.InputValidity).get('input', value), valid = _c.valid, _d = _c.message, message = _d === void 0 ? '' : _d;
        if (valid && customValidator) {
            var customValid = customValidator(value);
            if (customValid) {
                valid = customValid.valid;
                message = customValid.message || '';
            }
        }
        this._callOnValidate(valid, message);
    };
    Object.defineProperty(TextArea.prototype, "validity", {
        get: function () {
            var _a = this.properties.valid, valid = _a === void 0 ? { valid: undefined, message: undefined } : _a;
            if (typeof valid === 'boolean') {
                return { valid: valid, message: undefined };
            }
            return {
                valid: valid.valid,
                message: valid.message
            };
        },
        enumerable: true,
        configurable: true
    });
    TextArea.prototype.getRootClasses = function () {
        var _a = this.properties, disabled = _a.disabled, readOnly = _a.readOnly, required = _a.required;
        var focus = this.meta(Focus_2.default).get('root');
        var valid = this.validity.valid;
        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
        ];
    };
    TextArea.prototype.render = function () {
        var _a = this.properties, _b = _a.aria, aria = _b === void 0 ? {} : _b, _c = _a.columns, columns = _c === void 0 ? 20 : _c, disabled = _a.disabled, _d = _a.widgetId, widgetId = _d === void 0 ? this._uuid : _d, label = _a.label, maxLength = _a.maxLength, minLength = _a.minLength, name = _a.name, placeholder = _a.placeholder, readOnly = _a.readOnly, required = _a.required, _e = _a.rows, rows = _e === void 0 ? 2 : _e, value = _a.value, wrapText = _a.wrapText, theme = _a.theme, classes = _a.classes, labelHidden = _a.labelHidden, helperText = _a.helperText, onValidate = _a.onValidate;
        var focus = this.meta(Focus_2.default).get('root');
        onValidate && this._validate();
        var _f = this.validity, valid = _f.valid, message = _f.message;
        var computedHelperText = (valid === false && message) || helperText;
        return vdom_1.v('div', {
            key: 'root',
            classes: this.theme(this.getRootClasses())
        }, [
            label
                ? vdom_1.w(index_1.default, {
                    theme: theme,
                    classes: classes,
                    disabled: disabled,
                    focused: focus.containsFocus,
                    invalid: valid === false || undefined,
                    readOnly: readOnly,
                    required: required,
                    hidden: labelHidden,
                    forId: widgetId
                }, [label])
                : null,
            vdom_1.v('div', { classes: this.theme(css.inputWrapper) }, [
                vdom_1.v('textarea', tslib_1.__assign({ id: widgetId, key: 'input' }, util_1.formatAriaProperties(aria), { classes: this.theme(css.input), cols: "" + columns, disabled: disabled, focus: this.shouldFocus, 'aria-invalid': valid === false ? 'true' : null, maxlength: maxLength ? "" + maxLength : null, minlength: minLength ? "" + minLength : null, name: name,
                    placeholder: placeholder,
                    readOnly: readOnly, 'aria-readonly': readOnly ? 'true' : null, required: required, rows: "" + rows, value: 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 }))
            ]),
            vdom_1.w(index_2.default, { text: computedHelperText, valid: valid })
        ]);
    };
    TextArea = tslib_1.__decorate([
        Themed_1.theme(css)
    ], TextArea);
    return TextArea;
}(Themed_1.ThemedMixin(Focus_1.FocusMixin(WidgetBase_1.WidgetBase))));
exports.TextArea = TextArea;
exports.default = TextArea;

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