"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDisabled = exports.isAfterMax = exports.isBeforeMin = exports.buildHeaderConfig = exports.changeMonth = exports.getTitle = exports.parseRange = exports.parseDate = exports.CALENDAR_GRAIN = exports.CALENDAR_NAVIGATION = exports.CALENDAR_VIEWS = void 0;
var constants_1 = require("./../../constants/constants");
var moment_1 = __importDefault(require("moment"));
var CALENDAR_VIEWS;
(function (CALENDAR_VIEWS) {
    CALENDAR_VIEWS[CALENDAR_VIEWS["DAYS"] = 0] = "DAYS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["MONTHS"] = 1] = "MONTHS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["YEARS"] = 2] = "YEARS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["DECADES"] = 3] = "DECADES";
})(CALENDAR_VIEWS = exports.CALENDAR_VIEWS || (exports.CALENDAR_VIEWS = {}));
var CALENDAR_NAVIGATION;
(function (CALENDAR_NAVIGATION) {
    CALENDAR_NAVIGATION["FIRST"] = "first";
    CALENDAR_NAVIGATION["LAST"] = "last";
    CALENDAR_NAVIGATION["PREV"] = "prev";
    CALENDAR_NAVIGATION["NEXT"] = "next";
})(CALENDAR_NAVIGATION = exports.CALENDAR_NAVIGATION || (exports.CALENDAR_NAVIGATION = {}));
var CALENDAR_GRAIN;
(function (CALENDAR_GRAIN) {
    CALENDAR_GRAIN["DAY"] = "$day";
    CALENDAR_GRAIN["WEEK"] = "$week";
    CALENDAR_GRAIN["MONTH"] = "$month";
    CALENDAR_GRAIN["YEAR"] = "$year";
})(CALENDAR_GRAIN = exports.CALENDAR_GRAIN || (exports.CALENDAR_GRAIN = {}));
var parseDate = function (date, dateUtils) {
    if (isString(date)) {
        var dt = dateUtils.getStartOfMinute((0, moment_1.default)());
        if (date.startsWith(CALENDAR_GRAIN.DAY)) {
            return dateUtils.addDays(dt, parseInt(date.replace(CALENDAR_GRAIN.DAY, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.WEEK)) {
            return dateUtils.addWeeks(dt, parseInt(date.replace(CALENDAR_GRAIN.WEEK, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.MONTH)) {
            return dateUtils.addMonths(dt, parseInt(date.replace(CALENDAR_GRAIN.MONTH, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.YEAR)) {
            return dateUtils.addYears(dt, parseInt(date.replace(CALENDAR_GRAIN.YEAR, "") || "0", 10));
        }
        else {
            return dateUtils.toDate(date);
        }
    }
    else if (date) {
        return date;
    }
    return null;
};
exports.parseDate = parseDate;
var parseRange = function (date, dateUtils) {
    if (isString(date)) {
        if (date.startsWith(CALENDAR_GRAIN.DAY)) {
            var today = dateUtils.getStartOfDay((0, moment_1.default)());
            var diff = dateUtils.addDays((0, moment_1.default)(), parseInt(date.replace(CALENDAR_GRAIN.DAY, "") || "0", 10));
            return date.includes("-") ? [diff, today] : [today, diff];
        }
        if (date.startsWith(CALENDAR_GRAIN.WEEK)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.WEEK, "") || "0", 10);
            return [dateUtils.addWeeks(dateUtils.getStartOfWeek((0, moment_1.default)()), diff), dateUtils.addWeeks(dateUtils.getEndOfWeek((0, moment_1.default)()), diff)];
        }
        if (date.startsWith(CALENDAR_GRAIN.MONTH)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.MONTH, "") || "0", 10);
            return [dateUtils.addMonths(dateUtils.getStartOfMonth((0, moment_1.default)()), diff), dateUtils.addMonths(dateUtils.getEndOfMonth((0, moment_1.default)()), diff)];
        }
        if (date.startsWith(CALENDAR_GRAIN.YEAR)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.YEAR, "") || "0", 10);
            return [dateUtils.addYears(dateUtils.getStartOfYear((0, moment_1.default)()), diff), dateUtils.addYears(dateUtils.getEndOfYear((0, moment_1.default)()), diff)];
        }
    }
    else if (isArray(date)) {
        return [(0, exports.parseDate)(date[0], dateUtils), (0, exports.parseDate)(date[1], dateUtils)];
    }
    return null;
};
exports.parseRange = parseRange;
var getTitle = function (date, view, translations, dateUtils) {
    if (view === CALENDAR_VIEWS.DAYS) {
        return translations.months[date.getMonth()] + " " + dateUtils.format(date, "yyyy");
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        return dateUtils.format(date, "yyyy");
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        return dateUtils.format(dateUtils.getStartOfDecade(date), "yyyy") + "-" + dateUtils.format(dateUtils.getEndOfDecade(date), "yyyy");
    }
    return "-";
};
exports.getTitle = getTitle;
var changeMonth = function (date, view, grain, dateUtils) {
    if (view === CALENDAR_VIEWS.DAYS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.FIRST:
                return dateUtils.subtractMonths(date, 12);
            case CALENDAR_NAVIGATION.LAST:
                return dateUtils.addMonths(date, 12);
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractMonths(date, 1);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addMonths(date, 1);
        }
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractYears(date, 1);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addYears(date, 1);
        }
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractYears(date, 10);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addYears(date, 10);
        }
    }
    return date;
};
exports.changeMonth = changeMonth;
var buildHeaderConfig = function (date, view, config, dateUtils, translations) {
    if (translations === void 0) { translations = constants_1.Internationalization.datePickerTranslationsEN; }
    if (view === CALENDAR_VIEWS.DAYS) {
        return {
            firstDisabled: config.minDate ? (0, exports.isBeforeMin)(date, dateUtils.getStartOfYear(config.minDate), -12, dateUtils) : false,
            lastDisabled: config.maxDate ? (0, exports.isAfterMax)(date, dateUtils.getEndOfYear(config.maxDate), 12, dateUtils) : false,
            prevDisabled: config.minDate ? (0, exports.isBeforeMin)(date, dateUtils.getStartOfMonth(config.minDate), -1, dateUtils) : false,
            nextDisabled: config.maxDate ? (0, exports.isAfterMax)(date, dateUtils.getEndOfMonth(config.maxDate), 1, dateUtils) : false,
            firstTooltip: (0, exports.getTitle)(dateUtils.subtractMonths(date, 12), view, translations, dateUtils),
            lastTooltip: (0, exports.getTitle)(dateUtils.addMonths(date, 12), view, translations, dateUtils),
            prevTooltip: (0, exports.getTitle)(dateUtils.subtractMonths(date, 1), view, translations, dateUtils),
            nextTooltip: (0, exports.getTitle)(dateUtils.addMonths(date, 1), view, translations, dateUtils)
        };
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        return {
            prevDisabled: config.minDate ? dateUtils.isBeforeYear(dateUtils.subtractMonths(date, 12), config.minDate) : false,
            nextDisabled: config.maxDate ? dateUtils.isAfterYear(dateUtils.addMonths(date, 12), config.maxDate) : false,
            prevTooltip: (0, exports.getTitle)(dateUtils.subtractYears(date, 1), view, translations, dateUtils),
            nextTooltip: (0, exports.getTitle)(dateUtils.addYears(date, 1), view, translations, dateUtils)
        };
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        var start = dateUtils.getStartOfDecade(date);
        var end = dateUtils.getEndOfDecade(date);
        return {
            prevDisabled: config.minDate ? dateUtils.isBeforeYear(dateUtils.subtractMonths(date, 120), dateUtils.getStartOfDecade(config.minDate)) : false,
            nextDisabled: config.maxDate ? dateUtils.isAfterYear(dateUtils.addMonths(date, 120), dateUtils.getEndOfDecade(config.maxDate)) : false,
            prevTooltip: dateUtils.format(dateUtils.addYears(start, -10), "yyyy") + "-" + dateUtils.format(dateUtils.addYears(start, -1), "yyyy"),
            nextTooltip: dateUtils.format(dateUtils.addYears(end, 1), "yyyy") + "-" + dateUtils.format(dateUtils.addYears(end, 10), "yyyy")
        };
    }
};
exports.buildHeaderConfig = buildHeaderConfig;
var isBeforeMin = function (date, minDate, n, dateUtils) {
    if (n === void 0) { n = 0; }
    return dateUtils.isValid(minDate) ? dateUtils.isBefore(dateUtils.addMonths(dateUtils.getStartOfDay(date), n), minDate) : false;
};
exports.isBeforeMin = isBeforeMin;
var isAfterMax = function (date, maxDate, n, dateUtils) {
    if (n === void 0) { n = 0; }
    return dateUtils.isValid(maxDate) ? dateUtils.isAfter(dateUtils.addMonths(dateUtils.getStartOfDay(date), n), maxDate) : false;
};
exports.isAfterMax = isAfterMax;
var isDisabled = function (config, date, dateUtils) {
    var min = config.minDate;
    var max = config.maxDate;
    if (config.page === CALENDAR_VIEWS.DAYS) {
        min = dateUtils.getStartOfDay(min);
        max = dateUtils.getStartOfDay(max);
    }
    if (config.page === CALENDAR_VIEWS.MONTHS) {
        min = dateUtils.getStartOfDay(dateUtils.getStartOfMonth(min));
        max = dateUtils.getStartOfDay(dateUtils.getStartOfMonth(max));
    }
    if (config.page === CALENDAR_VIEWS.YEARS) {
        min = dateUtils.getStartOfDay(dateUtils.getStartOfYear(min));
        max = dateUtils.getStartOfDay(dateUtils.getStartOfYear(max));
    }
    if (dateUtils.isBefore(date, min)) {
        return true;
    }
    if (dateUtils.isAfter(date, max)) {
        return true;
    }
    if (config.page === CALENDAR_VIEWS.DAYS && config.disabled) {
        var disabled = config.disabled;
        if (isArray(disabled)) {
            return disabled.includes(dateUtils.getStartOfDay(date).toISOString());
        }
        else if (isFunction(disabled)) {
            return disabled({ date: date });
        }
    }
    return false;
};
exports.isDisabled = isDisabled;

//# sourceMappingURL=calendar-utils.js.map
