"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var core_1 = require("@angular/core");
var $ = require("jquery");
require("../../../../libraries/bootstrap-touchspin/index");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __object = typescript_angular_utilities_1.services.object;
var __array = typescript_angular_utilities_1.services.array;
var __guid = typescript_angular_utilities_1.services.guid;
var __number = typescript_angular_utilities_1.services.number;
var __string = typescript_angular_utilities_1.services.string;
var validationInput_1 = require("../validationInput");
var componentValidator_service_1 = require("../../../services/componentValidator/componentValidator.service");
var form_1 = require("../../form/form");
var input_1 = require("../input");
exports.defaultMaxValue = 100000000000000000000;
var SpinnerComponent = (function (_super) {
    __extends(SpinnerComponent, _super);
    function SpinnerComponent(number, string, rlForm, componentValidator, object, array, guid) {
        var _this = _super.call(this, rlForm, componentValidator, object, array, guid) || this;
        _this.rendering = false;
        _this.inputType = 'spinner';
        _this.numberUtility = number;
        _this.stringUtility = string;
        _this.showLabel();
        return _this;
    }
    SpinnerComponent.prototype.focus = function () {
        this.spinner.nativeElement.focus();
    };
    SpinnerComponent.prototype.ngAfterViewInit = function () {
        var _this = this;
        if (this.roundToStep && this.decimals == null) {
            throw new Error('You must specify the number of decimals to show when forcing to the step amount');
        }
        _super.prototype.ngAfterViewInit.call(this);
        this.value = this.value || 0;
        this.setDisabled(this.disabled);
        this.control.valueChanges.subscribe(function (value) {
            var roundedValue = _this.round(value);
            if (value !== roundedValue) {
                _this.control.setValue(roundedValue);
                _this.value = roundedValue;
            }
            if (_this.touchspin) {
                _this.touchspin.val(roundedValue != null ? roundedValue.toString() : '');
            }
        });
    };
    SpinnerComponent.prototype.ngOnChanges = function (changes) {
        _super.prototype.ngOnChanges.call(this, changes);
        if (changes.disabled) {
            this.setDisabled(changes.disabled.currentValue);
        }
    };
    SpinnerComponent.prototype.ngAfterViewChecked = function () {
        var _this = this;
        if (this.rendering && this.spinner) {
            this.touchspin = $(this.spinner.nativeElement).TouchSpin({
                min: (this.min != null ? this.min : 0),
                max: (this.max != null ? this.max : exports.defaultMaxValue),
                step: this.step,
                prefix: this.prefix,
                postfix: this.postfix,
                decimals: this.decimals,
                initval: this.value,
                forcestepdivisibility: this.roundToStep ? 'round' : 'none',
            });
            this.touchspin.on('change', function () {
                var spinValue = _this.touchspin.val();
                var valueAsNumber = _this.stringUtility.toNumber(spinValue);
                _this.setValue(_this.round(valueAsNumber));
            });
            this.rendering = false;
        }
    };
    SpinnerComponent.prototype.round = function (num) {
        if (num != null && this.roundToStep) {
            num = __number.numberUtility.roundToStep(num, this.step);
            num = __number.numberUtility.preciseRound(num, this.decimals);
        }
        return num;
    };
    SpinnerComponent.prototype.setDisabled = function (disabled) {
        if (!disabled) {
            this.rendering = true;
        }
        else {
            this.touchspin = null;
        }
    };
    return SpinnerComponent;
}(validationInput_1.ValidatedInputComponent));
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], SpinnerComponent.prototype, "min", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], SpinnerComponent.prototype, "max", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], SpinnerComponent.prototype, "step", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], SpinnerComponent.prototype, "decimals", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", String)
], SpinnerComponent.prototype, "prefix", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", String)
], SpinnerComponent.prototype, "postfix", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], SpinnerComponent.prototype, "roundToStep", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", String)
], SpinnerComponent.prototype, "spinnerId", void 0);
__decorate([
    core_1.ViewChild('spinner'),
    __metadata("design:type", core_1.ElementRef)
], SpinnerComponent.prototype, "spinner", void 0);
SpinnerComponent = __decorate([
    core_1.Component({
        selector: 'rlSpinner',
        template: require('./spinner.html'),
        inputs: validationInput_1.validationInputs,
        outputs: validationInput_1.baseOutputs,
        providers: [componentValidator_service_1.ComponentValidator],
        animations: input_1.baseAnimations,
    }),
    __param(2, core_1.Optional()),
    __metadata("design:paramtypes", [__number.NumberUtility, __string.StringUtility, form_1.FormComponent,
        componentValidator_service_1.ComponentValidator, __object.ObjectUtility, __array.ArrayUtility, __guid.GuidService])
], SpinnerComponent);
exports.SpinnerComponent = SpinnerComponent;
//# sourceMappingURL=data:application/json;base64, 