"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processViewDate = exports.processSelectYear = exports.processSelectMonth = exports.getNextDate = exports.isYear = exports.isMonth = exports.isDay = exports.isRange = exports.isTo = exports.isFrom = exports.isExact = void 0;
const utils_1 = require("../utils");
const consts_1 = require("./consts");
const isExact = (mode) => mode === 'exact';
exports.isExact = isExact;
const isFrom = (mode) => mode === 'from';
exports.isFrom = isFrom;
const isTo = (mode) => mode === 'to';
exports.isTo = isTo;
const isRange = (mode) => mode === 'range';
exports.isRange = isRange;
const isDay = (timeUnitMode) => timeUnitMode === 'day';
exports.isDay = isDay;
const isMonth = (timeUnitMode) => timeUnitMode === 'month';
exports.isMonth = isMonth;
const isYear = (timeUnitMode) => timeUnitMode === 'year';
exports.isYear = isYear;
/**
 * Helper to get the next date item when iterating between two dates
 * @param date current date in the iteration
 * @param timeUnitMode time unit mode for day, month, year
 */
const getNextDate = (date, timeUnitMode) => {
    const { utcYear, utcMonth, utcDate } = (0, utils_1.getDateUTCValue)(date);
    if ((0, exports.isDay)(timeUnitMode))
        return (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth, utcDate + 1);
    if ((0, exports.isMonth)(timeUnitMode))
        return (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth + 1, utcDate);
    return (0, utils_1.generateDateByUTCValue)(utcYear + 1, utcMonth, utcDate);
};
exports.getNextDate = getNextDate;
const processSelectMonth = (clickedDate, mode, boundaries) => {
    const date = clickedDate;
    const minDateTime = new Date(consts_1.minDateTimestamp);
    const { utcYear, utcMonth } = (0, utils_1.getDateUTCValue)(clickedDate);
    if ((0, exports.isTo)(mode)) {
        const selectDayInMonth = (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth + 1, 0).getUTCDate();
        if ((boundaries === null || boundaries === void 0 ? void 0 : boundaries.endDate) && (0, utils_1.sameYearSameMonth)(clickedDate, boundaries === null || boundaries === void 0 ? void 0 : boundaries.endDate))
            return boundaries === null || boundaries === void 0 ? void 0 : boundaries.endDate;
        return (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth, selectDayInMonth);
    }
    if ((0, exports.isFrom)(mode) && (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) && (0, utils_1.sameYearSameMonth)(clickedDate, boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate))
        return boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate;
    if ((0, exports.isExact)(mode) && (0, utils_1.sameYear)(clickedDate, minDateTime))
        return minDateTime;
    return (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) && date < (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) ? boundaries.startDate : date;
};
exports.processSelectMonth = processSelectMonth;
/**
 * Helper that calcs what new date we should set as viewDate when a year is clicked
 *
 * @param clickedDate date of year clicked
 * @param newDate pre-processed potential new view date
 * @param mode calendar mode
 * @param boundaries boundaries of date range
 */
const processSelectYear = (clickedData, mode, boundaries) => {
    const { utcYear, utcMonth } = (0, utils_1.getDateUTCValue)(clickedData);
    const minDateTime = new Date(consts_1.minDateTimestamp);
    const date = clickedData;
    if ((0, exports.isTo)(mode)) {
        const selectDayInMonth = (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth + 1, 0).getUTCDate();
        const selectMonthInYear = 11;
        if ((boundaries === null || boundaries === void 0 ? void 0 : boundaries.endDate) && (0, utils_1.sameYear)(clickedData, boundaries === null || boundaries === void 0 ? void 0 : boundaries.endDate))
            return boundaries.endDate;
        return (0, utils_1.generateDateByUTCValue)(clickedData.getUTCFullYear(), selectMonthInYear, selectDayInMonth);
    }
    if ((0, exports.isFrom)(mode) && (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) && (0, utils_1.sameYear)(clickedData, boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate))
        return boundaries.startDate;
    if ((0, exports.isExact)(mode) && (0, utils_1.sameYear)(clickedData, minDateTime))
        return minDateTime;
    return (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) && date < (boundaries === null || boundaries === void 0 ? void 0 : boundaries.startDate) ? boundaries.startDate : date;
};
exports.processSelectYear = processSelectYear;
const processViewDate = (viewDate, direction, timeUnitMode, calendarItems) => {
    const { utcYear, utcMonth } = (0, utils_1.getDateUTCValue)(viewDate);
    if ((0, exports.isDay)(timeUnitMode)) {
        const newMonth = (0, utils_1.generateDateByUTCValue)(utcYear, utcMonth + (direction === 'next' ? 1 : -1));
        return newMonth;
    }
    if ((0, exports.isMonth)(timeUnitMode)) {
        const newMonth = (0, utils_1.generateDateByUTCValue)(utcYear + (direction === 'next' ? 1 : -1), 0);
        return newMonth;
    }
    if (calendarItems) {
        const date = calendarItems[direction === 'next' ? calendarItems.length - 1 : 0].date;
        const year = date.getUTCFullYear();
        const newViewDate = (0, utils_1.generateDateByUTCValue)(year + (direction === 'next' ? 1 : -consts_1.viewYears), 0);
        return newViewDate;
    }
    return null;
};
exports.processViewDate = processViewDate;
//# sourceMappingURL=utils.js.map