"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateComputation = void 0;
var common_model_1 = require("../../../model/common-model");
var moment_1 = __importDefault(require("moment"));
var DateComputation = (function () {
    function DateComputation() {
    }
    DateComputation.prototype.GetDateIntervalFromFilter = function (filter) {
        var isDateFromLocked = false, isDateToLocked = false;
        var dateFrom, dateTo;
        var today = (0, moment_1.default)();
        if ((filter === null || filter === void 0 ? void 0 : filter.datePreFilter) != null) {
            switch (filter.datePreFilter) {
                case common_model_1.DatePreFilter.Today:
                    dateFrom = (0, moment_1.default)(today).startOf("day");
                    dateTo = (0, moment_1.default)(today).endOf("day");
                    break;
                case common_model_1.DatePreFilter.Past:
                    if (filter.includeToday) {
                        dateTo = (0, moment_1.default)(today).endOf("day");
                    }
                    else {
                        dateTo = (0, moment_1.default)(today).subtract(1, "day").endOf("day");
                    }
                    isDateFromLocked = true;
                    break;
                case common_model_1.DatePreFilter.Futur:
                    if (filter.includeToday) {
                        dateFrom = (0, moment_1.default)(today).startOf("day");
                    }
                    else {
                        dateFrom = (0, moment_1.default)(today).add(1, "day").startOf("day");
                    }
                    isDateToLocked = true;
                    break;
            }
        }
        else if ((filter === null || filter === void 0 ? void 0 : filter.dateFrom) != null || (filter === null || filter === void 0 ? void 0 : filter.dateTo) != null) {
            if (filter.dateFrom != null) {
                dateFrom = (0, moment_1.default)(filter.dateFrom).startOf("day");
            }
            if (filter.dateTo != null) {
                dateTo = (0, moment_1.default)(filter.dateTo).endOf("day");
            }
        }
        if ((filter === null || filter === void 0 ? void 0 : filter.isThisMonth) != null || (filter === null || filter === void 0 ? void 0 : filter.isThisWeek) != null || (filter === null || filter === void 0 ? void 0 : filter.isLastMonth) != null ||
            (filter === null || filter === void 0 ? void 0 : filter.isLastWeek) != null || (filter === null || filter === void 0 ? void 0 : filter.isNextMonth) != null || (filter === null || filter === void 0 ? void 0 : filter.isNextWeek) != null) {
            if (filter.isThisMonth) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isThisWeek) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).endOf("week"), dateTo, false, isDateToLocked);
            }
            if (filter.isLastMonth) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).subtract(1, "month").startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).subtract(1, "month").endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isLastWeek) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).subtract(1, "week").startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).subtract(1, "week").endOf("week"), dateTo, false, isDateToLocked);
            }
            if (filter.isNextMonth) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).add(1, "month").startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).add(1, "month").endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isNextWeek) {
                dateFrom = this.addMomentToMoment((0, moment_1.default)(today).add(1, "week").startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment((0, moment_1.default)(today).add(1, "week").endOf("week"), dateTo, false, isDateToLocked);
            }
        }
        return { dateFrom: dateFrom != null ? dateFrom.add(dateFrom.utcOffset(), "minutes") : dateFrom, dateTo: dateTo };
    };
    DateComputation.prototype.addMomentToMoment = function (dateToAdd, date, isBefore, isDateLocked) {
        if (isDateLocked === void 0) { isDateLocked = false; }
        if (!isDateLocked) {
            if (date == null) {
                date = dateToAdd;
            }
            else {
                if (isBefore) {
                    if (dateToAdd < date) {
                        date = dateToAdd;
                    }
                }
                else {
                    if (dateToAdd > date) {
                        date = dateToAdd;
                    }
                }
            }
        }
        return date;
    };
    return DateComputation;
}());
exports.DateComputation = DateComputation;

//# sourceMappingURL=date-computation.js.map
