"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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.UIInput = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var ui_internal_1 = require("../../utils/ui-internal");
var config_1 = require("../../config");
var base_input_1 = require("./base-input");
var input_wrapper_1 = require("./input-wrapper");
var UIInput = exports.UIInput = (function (_super) {
    __extends(UIInput, _super);
    function UIInput(element, config, bindingEngine) {
        var _this = _super.call(this, element) || this;
        _this.element = element;
        _this.config = config;
        _this.bindingEngine = bindingEngine;
        _this.type = "text";
        _this.placeholder = "";
        _this.autocomplete = "";
        _this.autofocus = false;
        _this.inputMode = null;
        _this.step = null;
        _this.ignoreInputValueChange = 0;
        _this.ignoreValueChange = 0;
        return _this;
    }
    UIInput.prototype.bind = function () {
        var _a;
        _super.prototype.bind.call(this);
        this.valueChanged();
        (_a = this.inputMode) !== null && _a !== void 0 ? _a : (this.inputMode = this.config.globalConfig.uiInputInputMode);
    };
    UIInput.prototype.attached = function () {
        this.maxlengthChanged();
        this.valueChanged();
    };
    Object.defineProperty(UIInput.prototype, "counter", {
        get: function () {
            if (this.maxlength) {
                return "".concat(this.maxlength - (this.inputValue ? this.inputValue.length : 0));
            }
            else {
                return "".concat(this.inputValue ? this.inputValue.length : 0);
            }
        },
        enumerable: false,
        configurable: true
    });
    UIInput.prototype.maxlengthChanged = function () {
        if (this.inputEl) {
            this.inputEl.removeAttribute("maxLength");
            if (this.maxlength > 0) {
                this.inputEl.maxLength = this.maxlength;
            }
        }
    };
    UIInput.prototype.inputValueChanged = function () {
        var _this = this;
        if (this.value !== this.inputValue && 0 == this.ignoreInputValueChange) {
            this.ignoreValueChange++;
            if (this.type == 'number') {
                this.value = this.inputValue ? parseFloat(this.inputValue) : null;
            }
            else {
                this.value = this.inputValue;
            }
            ui_internal_1.UIInternal.queueTask(function () { return _this.ignoreValueChange--; });
        }
    };
    UIInput.prototype.valueChanged = function () {
        var _this = this;
        var _a;
        if (this.value != this.inputValue && 0 == this.ignoreValueChange) {
            this.ignoreInputValueChange++;
            this.inputValue = (_a = this.value) === null || _a === void 0 ? void 0 : _a.toString();
            ui_internal_1.UIInternal.queueTask(function () { return _this.ignoreInputValueChange--; });
        }
    };
    UIInput.prototype.blur = function () {
        var _a, _b;
        if (this.type == 'number' && (this.value || '').toString().length > 0) {
            var castedNumber = this.toNumber(this.value);
            if (this.min != null) {
                castedNumber = Math.max(castedNumber || 0, this.toNumber(this.min));
            }
            if (this.max != null) {
                castedNumber = Math.min(castedNumber || 0, this.toNumber(this.max));
            }
            if (this.step != null && this.step != 0) {
                var step = this.toNumber(this.step);
                var min = this.min != null ? this.toNumber(this.min) : 0;
                var normalizedNumber = (castedNumber || 0) - min;
                var nbrDecimalsInInput = ((_a = normalizedNumber.toString().split(".")[1]) === null || _a === void 0 ? void 0 : _a.length) || 0;
                var nbrDecimalsInAny = Math.max(((_b = step.toString().split(".")[1]) === null || _b === void 0 ? void 0 : _b.length) || 0, nbrDecimalsInInput);
                var multiplier = Math.pow(10, nbrDecimalsInAny);
                normalizedNumber *= multiplier;
                step *= multiplier;
                castedNumber = Math.floor(normalizedNumber / step) * step;
                castedNumber = parseFloat((castedNumber / multiplier).toFixed(nbrDecimalsInInput)) + min;
            }
            if (this.value != castedNumber.toString()) {
                this.value = castedNumber.toString();
            }
        }
        _super.prototype.blur.call(this);
    };
    UIInput.prototype.focusOut = function () {
        this.resetPlaceholder();
    };
    UIInput.prototype.mouseOut = function () {
        this.resetPlaceholder();
    };
    UIInput.prototype.toNumber = function (value) {
        return typeof value == 'string' ? parseFloat(value) : value;
    };
    UIInput.prototype.resetPlaceholder = function () {
        var _this = this;
        ui_internal_1.UIInternal.queueTask(function () {
            if (_this.placeholder != _this.inputEl.placeholder) {
                _this.inputEl.placeholder = _this.placeholder;
            }
        });
    };
    UIInput.prototype.inputModeChanged = function () {
        var _a, _b;
        if (((_b = (_a = this.element) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.length) > 0) {
            this.element.children[0].inputMode = this.inputMode;
        }
    };
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Object)
    ], UIInput.prototype, "value", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UIInput.prototype, "type", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UIInput.prototype, "placeholder", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UIInput.prototype, "autocomplete", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Boolean)
    ], UIInput.prototype, "autofocus", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UIInput.prototype, "inputMode", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", Object)
    ], UIInput.prototype, "min", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", Object)
    ], UIInput.prototype, "max", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", Object)
    ], UIInput.prototype, "step", void 0);
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", String)
    ], UIInput.prototype, "inputValue", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("inputValue", "maxlength"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UIInput.prototype, "counter", null);
    UIInput = __decorate([
        (0, aurelia_framework_1.customElement)("ui-input"),
        (0, aurelia_framework_1.viewResources)(input_wrapper_1.InputWrapper),
        __metadata("design:paramtypes", [Element, config_1.Config, aurelia_framework_1.BindingEngine])
    ], UIInput);
    return UIInput;
}(base_input_1.BaseInput));

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