var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { TaskQueue, bindable, computedFrom, customElement, inlineView, observable } from "aurelia-framework";
export var UIFieldWrapper = (function () {
    function UIFieldWrapper(element, taskQueue) {
        this.element = element;
        this.taskQueue = taskQueue;
        this.plain = false;
        this.isEmpty = false;
        this.hasError = false;
        this.hasDisabled = false;
        this.hasReadonly = false;
    }
    UIFieldWrapper.prototype.attached = function () {
        var _this = this;
        this.taskQueue.queueMicroTask(function () {
            var _a, _b, _c, _d;
            var el = _this.element.querySelector("input, textarea, select, ui-input, ui-select, ui-date-picker, ui-ml-input, ui-ml-textarea");
            if (el != null) {
                if (el.tagName.toLowerCase() == 'select') {
                    if (((_b = (_a = el.textContent) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.length) == 0 && ((_c = el.parentElement) === null || _c === void 0 ? void 0 : _c.classList.contains('ui-input--readonly'))) {
                        _this.isEmpty = true;
                    }
                }
                else {
                    var val = el.value;
                    if ((val === null || val === void 0 ? void 0 : val.length) == 0 && ((_d = el.parentElement) === null || _d === void 0 ? void 0 : _d.classList.contains('ui-input--readonly'))) {
                        _this.isEmpty = true;
                    }
                }
                _this.hasReadonly = el.hasAttribute('readonly');
                _this.hasDisabled = el.hasAttribute('disabled');
                el.addEventListener("input-invalid", function () {
                    _this.hasError = true;
                });
                el.addEventListener("input-valid", function () {
                    _this.hasError = false;
                });
                el.addEventListener("input-readonly", function () {
                    _this.hasReadonly = true;
                });
                el.addEventListener("input-not-readonly", function () {
                    _this.hasReadonly = false;
                });
                el.addEventListener("input-disabled", function () {
                    _this.hasDisabled = true;
                });
                el.addEventListener("input-not-disabled", function () {
                    _this.hasDisabled = false;
                });
            }
        });
    };
    Object.defineProperty(UIFieldWrapper.prototype, "classes", {
        get: function () {
            var classes = [];
            if (this.plain === "" || this.plain === "plain" || isTrue(this.plain)) {
                classes.push("ui-field__wrapper--plain");
            }
            if (this.isEmpty === true) {
                classes.push("ui-field__wrapper--empty");
            }
            if (this.hasDisabled) {
                classes.push("ui-field__wrapper--disabled");
            }
            if (this.hasError) {
                classes.push("ui-field__wrapper--invalid");
            }
            if (this.hasReadonly) {
                classes.push("ui-field__wrapper--readonly");
            }
            return classes.join(" ");
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], UIFieldWrapper.prototype, "plain", void 0);
    __decorate([
        observable,
        __metadata("design:type", Object)
    ], UIFieldWrapper.prototype, "isEmpty", void 0);
    __decorate([
        observable,
        __metadata("design:type", Object)
    ], UIFieldWrapper.prototype, "hasError", void 0);
    __decorate([
        observable,
        __metadata("design:type", Object)
    ], UIFieldWrapper.prototype, "hasDisabled", void 0);
    __decorate([
        observable,
        __metadata("design:type", Object)
    ], UIFieldWrapper.prototype, "hasReadonly", void 0);
    __decorate([
        computedFrom("plain", "isEmpty", 'hasError', 'hasDisabled', 'hasReadonly'),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], UIFieldWrapper.prototype, "classes", null);
    UIFieldWrapper = __decorate([
        customElement("ui-field-wrapper"),
        inlineView("<template class=\"ui-field__wrapper ${classes}\">\n  <slot></slot>\n  </template>"),
        __metadata("design:paramtypes", [Element, TaskQueue])
    ], UIFieldWrapper);
    return UIFieldWrapper;
}());

//# sourceMappingURL=ui-field-wrapper.js.map
