"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 lodash_1 = require("lodash");
var moment = require("moment");
var $ = require("jquery");
require("../../../../libraries/bootstrap-datetimepicker/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 __date = typescript_angular_utilities_1.services.date;
var __dateFormats = __date.defaultFormats;
var __timezone = typescript_angular_utilities_1.services.timezone;
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");
var DateTimeComponent = (function (_super) {
    __extends(DateTimeComponent, _super);
    function DateTimeComponent(timezoneService, dateService, rlForm, componentValidator, object, array, guid) {
        var _this = _super.call(this, rlForm, componentValidator, object, array, guid) || this;
        _this.timezoneService = timezoneService;
        _this.dateService = dateService;
        _this.clear = new core_1.EventEmitter();
        _this.rendering = false;
        return _this;
    }
    DateTimeComponent.prototype.focus = function () {
        this.dateinput.nativeElement.focus();
    };
    DateTimeComponent.prototype.ngOnInit = function () {
        var _this = this;
        this.inputType = 'dateTime';
        this.timezone = this.timezoneService.currentTimezone;
        if (this.validators == null) {
            this.validators = [];
        }
        this.validators.push({
            name: 'valiDate',
            validate: function (value$) {
                return value$.map(function () { return _this.validFormat ? null : 'Date is not in a valid format'; });
            },
        });
        if (this.max != null) {
            this.validators.push({
                name: 'maxDate',
                validate: function (value$) {
                    return value$.map(function (date) { return date > _this.max
                        ? 'Date is greater than the greatest allowed date: ' + _this.formatDate(moment(_this.max))
                        : null; });
                },
            });
        }
        _super.prototype.ngOnInit.call(this);
        this.useDate = lodash_1.isUndefined(this.useDate) ? true : this.useDate;
        this.useTime = lodash_1.isUndefined(this.useTime) ? true : this.useTime;
        this.valueAsString = this.formatDate(this.value);
        var defaults = $(this.datepicker).datetimepicker.defaults;
        this.min = this.min != null ? this.min : defaults.minDate;
        this.max = this.max != null ? this.max : defaults.maxDate;
        this.setValidity(this.value);
        this.control.valueChanges.subscribe(function (value) {
            _this.valueAsString = _this.formatDate(value);
        });
    };
    DateTimeComponent.prototype.ngAfterViewInit = function () {
        var _this = this;
        _super.prototype.ngAfterViewInit.call(this);
        $(this.datepicker.nativeElement).datetimepicker({
            stepping: this.minuteStepping || 1,
            format: this.getFormatOrDefault(),
            direction: 'bottom',
            elementHeight: 2,
            pickDate: this.useDate,
            pickTime: this.useTime,
            minDate: this.min,
            maxDate: this.max,
        }).on('change.dp', function () {
            var newValue = $(_this.dateinput.nativeElement).val();
            _this.setValue(_this.parseDate(newValue));
        });
    };
    DateTimeComponent.prototype.onClear = function () {
        this.setValue(null);
        this.clear.emit(null);
    };
    DateTimeComponent.prototype.formatDate = function (value) {
        if (value == null) {
            this.timezone = this.timezoneService.currentTimezone;
            return '';
        }
        var date = moment(value);
        this.setValidity(date);
        this.timezone = __timezone.timezones.get(date.tz());
        return date.format(this.getFormatOrDefault());
    };
    DateTimeComponent.prototype.parseDate = function (value) {
        if (this.object.isNullOrEmpty(value)) {
            return null;
        }
        var newMoment = this.timezoneService.buildMomentWithTimezone(value, this.timezone, this.getFormatOrDefault());
        this.setValidity(newMoment);
        return newMoment;
    };
    DateTimeComponent.prototype.getFormatOrDefault = function () {
        return this.format || this.defaultFormat(this.useDate, this.useTime);
    };
    DateTimeComponent.prototype.defaultFormat = function (hasDate, hasTime) {
        if (hasDate && hasTime) {
            return __dateFormats.dateTimeFormat;
        }
        else if (hasDate) {
            return __dateFormats.dateFormat;
        }
        else if (hasTime) {
            return __dateFormats.timeFormat;
        }
        else {
            // revert to default format
            return false;
        }
    };
    DateTimeComponent.prototype.setValidity = function (date) {
        this.validFormat = __object.objectUtility.isNullOrEmpty(date)
            ? true
            : moment(date).isValid();
    };
    return DateTimeComponent;
}(validationInput_1.ValidatedInputComponent));
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], DateTimeComponent.prototype, "useDate", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], DateTimeComponent.prototype, "useTime", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], DateTimeComponent.prototype, "min", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], DateTimeComponent.prototype, "max", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], DateTimeComponent.prototype, "minuteStepping", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], DateTimeComponent.prototype, "showClear", void 0);
__decorate([
    core_1.Output(),
    __metadata("design:type", core_1.EventEmitter)
], DateTimeComponent.prototype, "clear", void 0);
__decorate([
    core_1.ViewChild('datepicker'),
    __metadata("design:type", core_1.ElementRef)
], DateTimeComponent.prototype, "datepicker", void 0);
__decorate([
    core_1.ViewChild('dateinput'),
    __metadata("design:type", core_1.ElementRef)
], DateTimeComponent.prototype, "dateinput", void 0);
DateTimeComponent = __decorate([
    core_1.Component({
        selector: 'rlDateTime',
        template: require('./dateTime.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", [__timezone.TimezoneService, __date.DateUtility, form_1.FormComponent,
        componentValidator_service_1.ComponentValidator, __object.ObjectUtility, __array.ArrayUtility, __guid.GuidService])
], DateTimeComponent);
exports.DateTimeComponent = DateTimeComponent;
//# sourceMappingURL=data:application/json;base64, 