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 { bindable, bindingMode, computedFrom, customElement, TaskQueue } from "aurelia-framework";
import { UIInternal } from './../../utils/ui-internal';
export var TimePage = (function () {
    function TimePage(element, taskQueue) {
        this.element = element;
        this.taskQueue = taskQueue;
        this.time = new Date();
        this.startHour = 0;
        this.endHour = 23;
        this.hourInterval = 1;
        this.startMinute = 0;
        this.endMinute = 59;
        this.minuteInterval = 5;
        this.hours = [];
        this.currHour = 0;
        this.minutes = [];
        this.currMinute = 0;
        this.timeIsChanging = false;
    }
    TimePage.prototype.bind = function () {
        this.setHours();
        this.setMinutes();
        if (this.time != null) {
            this.setCurrHour();
            this.setCurrMinute();
        }
    };
    TimePage.prototype.attached = function () {
        var _this = this;
        var hour = this.element.children[0].children[1];
        hour === null || hour === void 0 ? void 0 : hour.addEventListener('wheel', function (ev) { return _this.wheelHour(ev); });
        var minute = this.element.children[2].children[1];
        minute === null || minute === void 0 ? void 0 : minute.addEventListener('wheel', function (ev) { return _this.wheelMinute(ev); });
        UIInternal.subscribe("openTimePage", function () {
            _this.taskQueue.queueTask(function () { return _this.scrollMinuteAndHour(); });
        });
    };
    TimePage.prototype.timeChanged = function (newVal, oldVal) {
        var _this = this;
        if (newVal != null && newVal != oldVal && !this.timeIsChanging) {
            this.timeIsChanging = true;
            if (this.setCurrHour()) {
                this.time.setHours(this.currHour);
                this.time = new Date(this.time);
            }
            if (this.setCurrMinute()) {
                this.time.setMinutes(this.currMinute);
                this.time = new Date(this.time);
            }
            this.scrollMinuteAndHour();
            this.taskQueue.queueTask(function () {
                _this.timeIsChanging = false;
            });
        }
    };
    TimePage.prototype.setCurrHour = function () {
        var _this = this;
        this.currHour = this.time.getHours();
        if (!this.hours.some(function (x) { return x == _this.currHour.toString(); })) {
            var closest = this.hours.reduce(function (a, b) {
                return Math.abs(parseInt(b) - _this.currHour) < Math.abs(parseInt(a) - _this.currHour) ? b : a;
            });
            this.currHour = parseInt(closest);
            return true;
        }
        return false;
    };
    TimePage.prototype.setCurrMinute = function () {
        var _this = this;
        this.currMinute = this.time.getMinutes();
        if (!this.minutes.some(function (x) { return x == _this.currMinute.toString(); })) {
            var closest = this.minutes.reduce(function (a, b) {
                return Math.abs(parseInt(b) - _this.currMinute) < Math.abs(parseInt(a) - _this.currMinute) ? b : a;
            });
            this.currMinute = parseInt(closest);
            return true;
        }
        return false;
    };
    TimePage.prototype.startHourChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setHours();
        }
    };
    TimePage.prototype.endHourChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setHours();
        }
    };
    TimePage.prototype.hourIntervalChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setHours();
        }
    };
    TimePage.prototype.startMinuteChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setMinutes();
        }
    };
    TimePage.prototype.endMinuteChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setMinutes();
        }
    };
    TimePage.prototype.minuteIntervalChanged = function (newVal, oldVal) {
        if (newVal != null && newVal != oldVal) {
            this.setMinutes();
        }
    };
    TimePage.prototype.setHours = function () {
        this.hours.splice(0);
        for (var i = this.startHour; i <= this.endHour; i = i + this.hourInterval) {
            this.hours.push(i.toLocaleString('fr-BE', { minimumIntegerDigits: 2, useGrouping: false }));
        }
    };
    TimePage.prototype.setMinutes = function () {
        this.minutes.splice(0);
        for (var i = this.startMinute; i <= this.endMinute; i = i + this.minuteInterval) {
            this.minutes.push(i.toLocaleString('fr-BE', { minimumIntegerDigits: 2, useGrouping: false }));
        }
    };
    TimePage.prototype.wheelHour = function (event) {
        var delta = 0;
        if (event.wheelDelta) {
            (delta = event.wheelDelta / 120);
        }
        else if (event.detail) {
            (delta = -event.detail / 3);
        }
        var divHour = this.element.children[0].children[1];
        var iCurrent = Array.from(divHour.children).findIndex(function (x) { return x.classList.contains("currentHour"); });
        var toShow;
        if (delta < 0) {
            toShow = divHour.children.item(iCurrent + 1);
        }
        else {
            toShow = divHour.children.item(iCurrent - 1);
        }
        this.wheel(toShow, true);
    };
    TimePage.prototype.wheelMinute = function (event) {
        var delta = 0;
        if (event.wheelDelta) {
            (delta = event.wheelDelta / 120);
        }
        else if (event.detail) {
            (delta = -event.detail / 3);
        }
        var divMinute = this.element.children[2].children[1];
        var iCurrentMinute = Array.from(divMinute.children).findIndex(function (x) { return x.classList.contains("currentMinute"); });
        var toShow;
        if (delta < 0) {
            toShow = divMinute.children.item(iCurrentMinute + 1);
        }
        else {
            toShow = divMinute.children.item(iCurrentMinute - 1);
        }
        this.wheel(toShow, false);
    };
    TimePage.prototype.wheel = function (toShow, isHours) {
        if (toShow == null)
            return;
        var toShowInt = parseInt(toShow.innerHTML);
        if (!isNaN(toShowInt)) {
            var newTime = null;
            if (this.time == null) {
                newTime = new Date();
            }
            else {
                newTime = new Date(this.time);
            }
            if (isHours) {
                this.currHour = toShowInt;
                newTime.setHours(toShowInt);
            }
            else {
                this.currMinute = toShowInt;
                newTime.setMinutes(toShowInt);
            }
            this.time = newTime;
        }
    };
    TimePage.prototype.selectHour = function (hour) {
        var divHour = this.element.children[0].children[1];
        var iCurrent = Array.from(divHour.children).findIndex(function (x) { return x.classList.contains("currentHour"); });
        var toShow;
        if (hour < 0) {
            toShow = divHour.children.item(iCurrent - 1);
        }
        else {
            toShow = divHour.children.item(iCurrent + 1);
        }
        this.wheel(toShow, true);
    };
    TimePage.prototype.selectMinute = function (minute) {
        var divMinute = this.element.children[2].children[1];
        var iCurrentMinute = Array.from(divMinute.children).findIndex(function (x) { return x.classList.contains("currentMinute"); });
        var toShow;
        if (minute < 0) {
            toShow = divMinute.children.item(iCurrentMinute - 1);
        }
        else {
            toShow = divMinute.children.item(iCurrentMinute + 1);
        }
        this.wheel(toShow, false);
    };
    TimePage.prototype.scrollMinuteAndHour = function () {
        var divHour = this.element.children[0].children[1];
        var iCurrentHour = Array.from(divHour.children).findIndex(function (x) { return x.classList.contains("currentHour"); });
        if (iCurrentHour != -1) {
            var topHour = divHour.children.item(iCurrentHour).offsetTop - divHour.children.item(iCurrentHour).getBoundingClientRect().height -
                (divHour.getBoundingClientRect().height - divHour.children.item(iCurrentHour).getBoundingClientRect().height) / 2;
            divHour.scrollTo({ behavior: 'smooth', top: topHour });
        }
        var divMinute = this.element.children[2].children[1];
        var iCurrentMinute = Array.from(divMinute.children).findIndex(function (x) { return x.classList.contains("currentMinute"); });
        if (iCurrentMinute != -1) {
            var topMinute = divMinute.children.item(iCurrentMinute).offsetTop - divMinute.children.item(iCurrentMinute).getBoundingClientRect().height -
                (divMinute.getBoundingClientRect().height - divMinute.children.item(iCurrentMinute).getBoundingClientRect().height) / 2;
            divMinute.scrollTo({ behavior: 'smooth', top: topMinute });
        }
    };
    Object.defineProperty(TimePage.prototype, "currHourPrevDisabled", {
        get: function () {
            return this.currHour == this.startHour;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimePage.prototype, "currHourNextDisabled", {
        get: function () {
            return this.currHour == parseInt(this.hours[this.hours.length - 1]);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimePage.prototype, "currMinutePrevDisabled", {
        get: function () {
            return this.currMinute == this.startMinute;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimePage.prototype, "currMinuteNextDisabled", {
        get: function () {
            return this.currMinute == parseInt(this.minutes[this.minutes.length - 1]);
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        bindable({ defaultBindingMode: bindingMode.twoWay }),
        __metadata("design:type", Date)
    ], TimePage.prototype, "time", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "startHour", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "endHour", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "hourInterval", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "startMinute", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "endMinute", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Number)
    ], TimePage.prototype, "minuteInterval", void 0);
    __decorate([
        computedFrom("currHour", "startHour"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], TimePage.prototype, "currHourPrevDisabled", null);
    __decorate([
        computedFrom("currHour", "hours.length"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], TimePage.prototype, "currHourNextDisabled", null);
    __decorate([
        computedFrom("currMinute", "startMinute"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], TimePage.prototype, "currMinutePrevDisabled", null);
    __decorate([
        computedFrom("currMinute", "minutes.length"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], TimePage.prototype, "currMinuteNextDisabled", null);
    TimePage = __decorate([
        customElement("time-page"),
        __metadata("design:paramtypes", [Element, TaskQueue])
    ], TimePage);
    return TimePage;
}());

//# sourceMappingURL=time-page.js.map
