"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseInput = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var ui_internal_1 = require("../../utils/ui-internal");
var constants_1 = require("../../constants/constants");
var BaseInput = exports.BaseInput = (function () {
    function BaseInput(element) {
        this.element = element;
        this.maxlength = 0;
        this.allowClear = false;
        this.readonly = false;
        this.disabled = false;
        this.blurFn = undefined;
        this.onFocusFn = undefined;
        this.onKeyPressedFn = undefined;
        this.isDisabled = false;
        this.showCounter = false;
        this.showCounter = element.hasAttribute("counter");
    }
    BaseInput.prototype.focus = function () {
        this.inputEl.focus();
    };
    BaseInput.prototype.blur = function () {
        if (typeof this.blurFn == "function" && this.blurFn != null) {
            this.blurFn();
        }
    };
    BaseInput.prototype.onFocus = function () {
        if (typeof this.onFocusFn == "function" && this.onFocusFn != null) {
            this.onFocusFn();
        }
    };
    BaseInput.prototype.disable = function (b) {
        this.isDisabled = b;
    };
    Object.defineProperty(BaseInput.prototype, "classes", {
        get: function () {
            var classes = [];
            if (this.errors && this.errors.length > 0) {
                classes.push("ui-input--invalid");
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-invalid"));
            }
            else {
                classes.push("ui-input--valid");
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-valid"));
            }
            if (this.isTrue("readonly")) {
                classes.push("ui-input--readonly");
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-readonly"));
            }
            else {
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-not-readonly"));
            }
            if (this.isTrue("disabled") || this.isDisabled) {
                classes.push("ui-input--disabled");
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-disabled"));
            }
            else {
                this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("input-not-disabled"));
            }
            return classes.join(" ");
        },
        enumerable: false,
        configurable: true
    });
    BaseInput.prototype.bind = function (bindingContext, overrideContext) {
        this.allowClear = this.isTrue("allowClear") || this.element.hasAttribute("allow-clear.trigger");
        this.readonly = this.isTrue("readonly");
        this.disabled = this.isTrue("disabled");
    };
    BaseInput.prototype.clear = function () {
        this.value = "";
        this.inputEl.focus();
        this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("clear"));
        this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("change"));
    };
    BaseInput.prototype.keyPressed = function ($event) {
        if (typeof this.onKeyPressedFn == "function" && this.onKeyPressedFn != null) {
            this.onKeyPressedFn($event);
        }
        if ($event.key === constants_1.KeyboardShortcut.Enter) {
            $event.stopEvent();
            this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("enterpressed", this.value));
        }
        return true;
    };
    BaseInput.prototype.canToggleDrop = function (evt) {
        if (evt.relatedTarget && evt.relatedTarget !== this.inputEl) {
            this.toggleDrop(false);
        }
    };
    BaseInput.prototype.toggleDrop = function (open) {
        var _this = this;
        if (open === true && this.dropEl.isOpen) {
            ui_internal_1.UIInternal.queueMicroTask(function () { return _this.dropEl.updatePosition(); });
            return;
        }
        var beforeEvent = this.dropEl.isOpen && !open ? "beforeclose" : "beforeopen";
        var afterEvent = this.dropEl.isOpen && !open ? "close" : "open";
        if (this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent(beforeEvent)) !== false) {
            this.dropEl.toggleDrop(open);
            this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent(afterEvent));
            if (this.dropEl.isOpen) {
                this.inputEl.select();
                return true;
            }
            else {
                return false;
            }
        }
    };
    BaseInput.prototype.isTrue = function (prop) {
        return this[prop] === "" || this[prop] === prop || isTrue(this[prop]);
    };
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Object)
    ], BaseInput.prototype, "errors", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Number)
    ], BaseInput.prototype, "maxlength", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Boolean)
    ], BaseInput.prototype, "allowClear", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Object)
    ], BaseInput.prototype, "readonly", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Object)
    ], BaseInput.prototype, "disabled", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Function)
    ], BaseInput.prototype, "blurFn", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Function)
    ], BaseInput.prototype, "onFocusFn", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Function)
    ], BaseInput.prototype, "onKeyPressedFn", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("isDisabled", "disabled", "readonly", "errors", "errors.length"),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], BaseInput.prototype, "classes", null);
    return BaseInput;
}());

//# sourceMappingURL=base-input.js.map
