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 { DateComputation } from './date-computation';
import { DatePreFilter, FilterDatePreset } from './../../../model/common-model';
import moment from 'moment';
import { observable } from 'aurelia-binding';
import { Container, TaskQueue } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
export var DateFilterHelper = (function () {
    function DateFilterHelper(propertyChangedHelper, resetFilterCallback) {
        this.dateFormat = "DD/MM/YYYY";
        this._filterProperties = ['fromDate', 'toDate', 'selectedDatePreFilter', 'isDateNullable', 'isLastMonth', 'isLastWeek',
            'isThisWeek', 'isThisMonth', 'isNextWeek', 'isNextMonth'];
        this.includeToday = false;
        this.ignorePropertyChanges = false;
        this.dateComputation = Container.instance.get(DateComputation);
        this.taskQueue = Container.instance.get(TaskQueue);
        this.i18n = Container.instance.get(I18N);
        this.initPreset();
        this.propertyChangedHelper = propertyChangedHelper;
        this.resetFilterCallback = resetFilterCallback;
    }
    DateFilterHelper.prototype.initPreset = function () {
        var _this = this;
        var today = moment();
        this.preset = [
            {
                label: "common:grid.past",
                preset: [
                    Date.MIN_DATE,
                    moment(today).subtract(1, "day").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = true;
                    _this.selectedDatePreFilter = DatePreFilter.Past;
                    if (_this.includeToday) {
                        _this.toDate = today.toDate();
                    }
                }
            },
            {
                label: "common:grid.today",
                preset: [
                    moment(today).toDate(),
                    moment(today).toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.selectedDatePreFilter = DatePreFilter.Today;
                }
            },
            {
                label: "common:grid.futur",
                preset: [
                    moment(today).add(1, "day").toDate(),
                    Date.MAX_DATE
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = true;
                    _this.selectedDatePreFilter = DatePreFilter.Futur;
                    if (_this.includeToday) {
                        _this.fromDate = today.toDate();
                    }
                }
            },
            {
                label: "common:grid.lastMonth",
                preset: [
                    moment(today).subtract(1, "month").startOf("month").toDate(),
                    moment(today).subtract(1, "month").endOf("month").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isLastMonth = true;
                }
            },
            {
                label: "common:grid.lastWeek",
                preset: [
                    moment(today).subtract(1, "week").startOf("week").toDate(),
                    moment(today).subtract(1, "week").endOf("week").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isLastWeek = true;
                }
            },
            {
                label: "common:grid.thisWeek",
                preset: [
                    moment(today).startOf("week").toDate(),
                    moment(today).endOf("week").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isThisWeek = true;
                },
                includeCurrentDate: true
            },
            {
                label: "common:grid.thisMonth",
                preset: [
                    moment(today).startOf("month").toDate(),
                    moment(today).endOf("month").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isThisMonth = true;
                },
                includeCurrentDate: true
            },
            {
                label: "common:grid.nextWeek",
                preset: [
                    moment(today).add(1, "week").startOf("week").toDate(),
                    moment(today).add(1, "week").endOf("week").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isNextWeek = true;
                }
            },
            {
                label: "common:grid.nextMonth",
                preset: [
                    moment(today).add(1, "month").startOf("month").toDate(),
                    moment(today).add(1, "month").endOf("month").toDate()
                ],
                action: function () {
                    _this.resetSelection();
                    _this.viewToday = false;
                    _this.isNextMonth = true;
                }
            }
        ];
    };
    DateFilterHelper.prototype.getPreset = function () {
        return this.preset;
    };
    DateFilterHelper.prototype.getModelFilter = function () {
        return {
            filterType: 'date', type: 'greaterThan', dateFrom: this.fromDate, dateTo: this.toDate, datePreFilter: this.selectedDatePreFilter, isDateNullable: this.isDateNullable,
            isLastMonth: this.isLastMonth, isLastWeek: this.isLastWeek, isThisWeek: this.isThisWeek, isThisMonth: this.isThisMonth, isNextWeek: this.isNextWeek, isNextMonth: this.isNextMonth
        };
    };
    DateFilterHelper.prototype.getModel = function (type) {
        if (type === void 0) { type = 'inRange'; }
        return this.isFilterActive() ? {
            filterType: 'date', type: type, dateFrom: this.fromDate, dateTo: this.toDate, datePreFilter: this.selectedDatePreFilter, isDateNullable: this.isDateNullable, includeToday: this.includeToday,
            isLastMonth: this.isLastMonth, isLastWeek: this.isLastWeek, isThisWeek: this.isThisWeek, isThisMonth: this.isThisMonth, isNextWeek: this.isNextWeek, isNextMonth: this.isNextMonth
        } : null;
    };
    DateFilterHelper.prototype.isFilterActive = function () {
        return (this.fromDate != null || this.toDate != null || this.selectedDatePreFilter != null || this.isDateNullable ||
            this.isLastMonth || this.isLastWeek || this.isThisWeek || this.isThisMonth || this.isNextWeek || this.isNextMonth);
    };
    DateFilterHelper.prototype.getFilter = function () {
        if (this.selectedDatePreFilter == DatePreFilter.Past) {
            this.selectedPreset = this.preset[FilterDatePreset.Past];
            this.viewToday = true;
            if (this.includeToday) {
                this.toDate = moment().toDate();
            }
        }
        else if (this.selectedDatePreFilter == DatePreFilter.Today) {
            this.selectedPreset = this.preset[FilterDatePreset.Today];
        }
        else if (this.selectedDatePreFilter == DatePreFilter.Futur) {
            this.selectedPreset = this.preset[FilterDatePreset.Futur];
            this.viewToday = true;
            if (this.includeToday) {
                this.fromDate = moment().toDate();
            }
        }
        if (this.isLastMonth) {
            this.selectedPreset = this.preset[FilterDatePreset.IsLastMonth];
        }
        else if (this.isLastWeek) {
            this.selectedPreset = this.preset[FilterDatePreset.IsLastWeek];
        }
        else if (this.isThisWeek) {
            this.selectedPreset = this.preset[FilterDatePreset.IsThisWeek];
        }
        else if (this.isThisMonth) {
            this.selectedPreset = this.preset[FilterDatePreset.IsThisMonth];
        }
        else if (this.isNextWeek) {
            this.selectedPreset = this.preset[FilterDatePreset.IsNextWeek];
        }
        else if (this.isNextMonth) {
            this.selectedPreset = this.preset[FilterDatePreset.IsNextMonth];
        }
        return "\n        <div class=\"col s12 m12 l12 date-filter\">\n            <ui-field label.bind=\"params.colDef.headerName\">\n                <ui-range-picker clear=\"true\" date-from.bind=\"filterHelper.fromDate\" date-to.bind=\"filterHelper.toDate\"\n                presets.bind=\"filterHelper.preset\" selected-preset.bind=\"filterHelper.selectedPreset\" autoset-when-opened.one-time='false'>\n                    <div slot=\"range-footer\" class=\"range-footer\">\n                        <div>\n                            <ui-checkbox show.bind=\"filterHelper.viewToday\" checked.bind=\"filterHelper.includeToday\">\n                                <span t=\"common:grid.includeToday\"></span>\n                            </ui-checkbox>\n                        </div>\n                        <ui-checkbox checked.bind=\"filterHelper.isDateNullable\">\n                            <span t=\"grid.isDateNullable\"></span>\n                        </ui-checkbox>\n                        <ui-button class=\"date-filter-reset\" t=\"[label]common:grid.resetFilter\" click.delegate=\"filterHelper.resetFilter()\" icon=\"digi-cross-circle\"></ui-button>\n                    </div>\n                </ui-range-picker>\n            </ui-field>\n        </div>";
    };
    DateFilterHelper.prototype.getValue = function () {
        var dateInterval = this.dateComputation.GetDateIntervalFromFilter(this.getModel());
        if (dateInterval.dateFrom != null || dateInterval.dateTo != null) {
            if (dateInterval.dateTo == null) {
                return '>= ' + moment(dateInterval.dateFrom).format(this.dateFormat);
            }
            else if (dateInterval.dateFrom == null) {
                return '<= ' + moment(dateInterval.dateTo).format(this.dateFormat);
            }
            else {
                return moment(dateInterval.dateFrom).format(this.dateFormat) + ' - ' + moment(dateInterval.dateTo).format(this.dateFormat);
            }
        }
        return null;
    };
    DateFilterHelper.prototype.getModelAsString = function () {
        var _a;
        return (_a = this.getValue()) !== null && _a !== void 0 ? _a : this.i18n.tr('grid.noFilter', { ns: "common" });
    };
    DateFilterHelper.prototype.resetSelection = function () {
        this.selectedDatePreFilter = null;
        this.isLastMonth = false;
        this.isLastWeek = false;
        this.isThisMonth = false;
        this.isThisWeek = false;
        this.isNextMonth = false;
        this.isNextWeek = false;
    };
    DateFilterHelper.prototype.resetFilter = function () {
        var _this = this;
        var resetModel = {
            fromDate: null,
            toDate: null,
            selectedDatePreFilter: null,
            isDateNullable: false,
            isLastMonth: false,
            isLastWeek: false,
            isThisWeek: false,
            isThisMonth: false,
            isNextWeek: false,
            isNextMonth: false,
        };
        this.selectedPreset = null;
        this.includeToday = null;
        if (this.isAnyDifferent(this, resetModel)) {
            this.ignorePropertyChanges = true;
            this.assignToThis(resetModel);
            if (this.resetFilterCallback) {
                this.resetFilterCallback();
            }
            this.taskQueue.queueMicroTask(function () { return _this.ignorePropertyChanges = false; });
        }
    };
    DateFilterHelper.prototype.isAnyDifferent = function (current, next) {
        return this._filterProperties.some(function (p) { return current[p] !== next[p]; });
    };
    DateFilterHelper.prototype.assignToThis = function (next) {
        var _this = this;
        this._filterProperties.forEach(function (p) { return _this[p] = next[p]; });
    };
    DateFilterHelper.prototype.viewTodayChanged = function (newValue, oldValue) {
        if (newValue != oldValue) {
            if (!newValue) {
                this.includeToday = false;
            }
        }
    };
    DateFilterHelper.prototype.includeTodayChanged = function (newValue, oldValue) {
        if (newValue != oldValue) {
            if (newValue) {
                if (this.selectedDatePreFilter == DatePreFilter.Past) {
                    this.toDate = moment().toDate();
                }
                else if (this.selectedDatePreFilter == DatePreFilter.Futur) {
                    this.fromDate = moment().toDate();
                }
            }
            else {
                if (this.selectedDatePreFilter == DatePreFilter.Past) {
                    this.toDate = this.preset[FilterDatePreset.Past].preset[FilterDatePreset.Today];
                }
                else if (this.selectedDatePreFilter == DatePreFilter.Futur) {
                    this.fromDate = this.preset[FilterDatePreset.Futur].preset[FilterDatePreset.Past];
                }
            }
        }
    };
    DateFilterHelper.prototype.fromDateChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.toDateChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isDateNullableChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isLastMonthChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isLastWeekChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isThisMonthChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isThisWeekChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isNextMonthChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.isNextWeekChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.selectedDatePreFilterChanged = function (newVal, oldVal) {
        this.propertyChangedHelper(newVal, oldVal);
    };
    DateFilterHelper.prototype.selectedPresetChanged = function (newVal, oldVal) {
        if (newVal == null) {
            this.resetSelection();
        }
    };
    __decorate([
        observable,
        __metadata("design:type", Date)
    ], DateFilterHelper.prototype, "fromDate", void 0);
    __decorate([
        observable,
        __metadata("design:type", Date)
    ], DateFilterHelper.prototype, "toDate", void 0);
    __decorate([
        observable,
        __metadata("design:type", Number)
    ], DateFilterHelper.prototype, "selectedDatePreFilter", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isDateNullable", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isLastMonth", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isLastWeek", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isThisWeek", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isThisMonth", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isNextWeek", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "isNextMonth", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "viewToday", void 0);
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], DateFilterHelper.prototype, "includeToday", void 0);
    __decorate([
        observable,
        __metadata("design:type", Object)
    ], DateFilterHelper.prototype, "selectedPreset", void 0);
    return DateFilterHelper;
}());

//# sourceMappingURL=date-filter-helper.js.map
