"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var vdom_1 = require("@dojo/framework/core/vdom");
var index_1 = require("../label/index");
var util_1 = require("../common/util");
var css = require("../theme/default/text-area.m.css");
var labelCss = require("../theme/default/label.m.css");
var index_2 = require("../helper-text/index");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var theme_1 = require("../middleware/theme");
var focus_1 = require("@dojo/framework/core/middleware/focus");
var validity_1 = require("@dojo/framework/core/middleware/validity");
var factory = vdom_1.create({
    icache: icache_1.createICacheMiddleware(),
    theme: theme_1.default,
    focus: focus_1.default,
    validity: validity_1.default
})
    .properties()
    .children();
exports.TextArea = factory(function TextArea(_a) {
    var id = _a.id, _b = _a.middleware, icache = _b.icache, theme = _b.theme, focus = _b.focus, validity = _b.validity, properties = _a.properties, children = _a.children;
    var themeCss = theme.classes(css);
    function callOnValidate(valid, message) {
        var _a = properties(), previousValid = _a.valid, onValidate = _a.onValidate;
        var previousMessage;
        if (typeof previousValid === 'object') {
            previousMessage = previousValid.message;
            previousValid = previousValid.valid;
        }
        if (valid !== previousValid || message !== previousMessage) {
            onValidate && onValidate(valid, message);
        }
    }
    function validate() {
        var _a = properties(), customValidator = _a.customValidator, _b = _a.value, value = _b === void 0 ? icache.get('value') || '' : _b;
        var dirty = icache.getOrSet('dirty', false);
        if (value === '' && !dirty) {
            callOnValidate(undefined, '');
            return;
        }
        icache.set('dirty', true);
        var _c = validity.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 || '';
            }
        }
        callOnValidate(valid, message);
    }
    function getValidity() {
        var _a = 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
        };
    }
    var _c = properties(), _d = _c.aria, aria = _d === void 0 ? {} : _d, _e = _c.columns, columns = _e === void 0 ? 20 : _e, disabled = _c.disabled, _f = _c.widgetId, widgetId = _f === void 0 ? "textarea-" + id : _f, maxLength = _c.maxLength, minLength = _c.minLength, name = _c.name, placeholder = _c.placeholder, readOnly = _c.readOnly, required = _c.required, _g = _c.rows, rows = _g === void 0 ? 2 : _g, initialValue = _c.initialValue, wrapText = _c.wrapText, themeProp = _c.theme, classes = _c.classes, labelHidden = _c.labelHidden, helperText = _c.helperText, onValidate = _c.onValidate, variant = _c.variant;
    var value = properties().value;
    if (value === undefined) {
        value = icache.get('value');
        var existingInitialValue = icache.get('initialValue');
        if (initialValue !== existingInitialValue) {
            icache.set('value', initialValue);
            icache.set('initialValue', initialValue);
            value = initialValue;
        }
    }
    onValidate && validate();
    var _h = getValidity(), valid = _h.valid, message = _h.message;
    var computedHelperText = (valid === false && message) || helperText;
    var inputFocused = focus.isFocused('input');
    var _j = tslib_1.__read(children(), 1), labelChild = _j[0];
    var label = util_1.isRenderResult(labelChild) ? labelChild : labelChild.label;
    return (vdom_1.tsx("div", { key: "root", classes: [theme.variant(), themeCss.root] },
        vdom_1.tsx("div", { key: "wrapper", classes: [
                themeCss.wrapper,
                disabled ? themeCss.disabled : null,
                valid === false ? themeCss.invalid : null,
                valid === true ? themeCss.valid : null,
                readOnly ? themeCss.readonly : null,
                required ? themeCss.required : null,
                inputFocused ? themeCss.focused : null
            ] },
            label ? (vdom_1.tsx(index_1.default, { theme: theme.compose(labelCss, css, 'label'), classes: classes, variant: variant, disabled: disabled, valid: valid, readOnly: readOnly, required: required, hidden: labelHidden, forId: widgetId, focused: inputFocused, active: !!value || inputFocused }, label)) : null,
            vdom_1.tsx("div", { classes: themeCss.inputWrapper },
                vdom_1.tsx("textarea", tslib_1.__assign({ id: widgetId, key: "input" }, util_1.formatAriaProperties(aria), { classes: themeCss.input, cols: columns, disabled: disabled, focus: focus.shouldFocus, "aria-invalid": valid === false ? 'true' : undefined, maxlength: maxLength ? "" + maxLength : null, minlength: minLength ? "" + minLength : null, name: name, placeholder: placeholder, readOnly: readOnly, "aria-readonly": readOnly ? 'true' : undefined, required: required, rows: rows, value: value, wrap: wrapText, onblur: function () {
                        var onBlur = properties().onBlur;
                        onBlur && onBlur();
                    }, onfocus: function () {
                        var onFocus = properties().onFocus;
                        onFocus && onFocus();
                    }, oninput: function (event) {
                        var onValue = properties().onValue;
                        event.stopPropagation();
                        var value = event.target.value;
                        icache.set('value', value);
                        onValue && onValue(value);
                    }, onkeydown: function (event) {
                        var onKeyDown = properties().onKeyDown;
                        event.stopPropagation();
                        onKeyDown &&
                            onKeyDown(event.which, function () {
                                event.preventDefault();
                            });
                    }, onkeyup: function (event) {
                        var onKeyUp = properties().onKeyUp;
                        event.stopPropagation();
                        onKeyUp &&
                            onKeyUp(event.which, function () {
                                event.preventDefault();
                            });
                    }, onclick: function () {
                        var onClick = properties().onClick;
                        onClick && onClick();
                    }, onpointerenter: function () {
                        var onOver = properties().onOver;
                        onOver && onOver();
                    }, onpointerleave: function () {
                        var onOut = properties().onOut;
                        onOut && onOut();
                    } })))),
        vdom_1.tsx(index_2.default, { text: computedHelperText, valid: valid, classes: classes, theme: themeProp, variant: variant })));
});
exports.default = exports.TextArea;

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