"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isProductActive = exports.getShiftedMonth = exports.getDays = exports.getStartOfMonthsBetweenDates = exports.convertSelectedMonthToDate = exports.excelDateToJSDate = exports.calculateTimeZone = exports.convertDateToFormat = exports.convertDayjsToFormat = exports.dateLessThan = exports.convertAnyToDate = exports.normalizeDate = exports.defaultDateFormat = void 0;
const dayjs_1 = __importDefault(require("dayjs"));
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
const isSameOrAfter_1 = __importDefault(require("dayjs/plugin/isSameOrAfter"));
const objectSupport_1 = __importDefault(require("dayjs/plugin/objectSupport"));
dayjs_1.default.extend(objectSupport_1.default);
dayjs_1.default.extend(utc_1.default);
dayjs_1.default.extend(timezone_1.default);
dayjs_1.default.extend(isSameOrAfter_1.default);
exports.defaultDateFormat = 'MM-DD-YYYY';
const normalizeDate = (date, type) => {
    return type === 'start'
        ? (0, dayjs_1.default)(date).utc().startOf('day').toDate()
        : (0, dayjs_1.default)(date).utc().endOf('day').toDate();
};
exports.normalizeDate = normalizeDate;
const convertAnyToDate = (date) => {
    let result = null;
    if (!date) {
        result = null;
    }
    if (date instanceof Date) {
        result = date;
    }
    if (date.toString().match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/)) {
        result = new Date(date.slice(0, -1));
    }
    return result;
};
exports.convertAnyToDate = convertAnyToDate;
const dateLessThan = (date, compare, unit) => {
    const currentDate = (0, dayjs_1.default)().utc();
    const nextDate = (0, dayjs_1.default)(date).utc();
    const difference = currentDate.diff(nextDate, unit);
    return difference < compare;
};
exports.dateLessThan = dateLessThan;
const convertDayjsToFormat = (date, format = exports.defaultDateFormat) => {
    return date.format(format);
};
exports.convertDayjsToFormat = convertDayjsToFormat;
const convertDateToFormat = (date, format = exports.defaultDateFormat) => {
    return (0, exports.convertDayjsToFormat)((0, dayjs_1.default)(date).utc(), format);
};
exports.convertDateToFormat = convertDateToFormat;
const calculateTimeZone = (date) => {
    if (date instanceof Date) {
        return (0, dayjs_1.default)(date).utc().subtract(date.getTimezoneOffset(), 'minute').toDate();
    }
    return date;
};
exports.calculateTimeZone = calculateTimeZone;
const excelDateToJSDate = (serial) => {
    const utcDays = Math.floor(serial - 25569);
    const utcValue = utcDays * 86400;
    const dateInfo = new Date(utcValue * 1000);
    const fractionalDay = serial - Math.floor(serial) + 0.0000001;
    let totalSeconds = Math.floor(86400 * fractionalDay);
    const seconds = totalSeconds % 60;
    totalSeconds -= seconds;
    const hours = Math.floor(totalSeconds / (60 * 60));
    const minutes = Math.floor(totalSeconds / 60) % 60;
    return new Date(dateInfo.getFullYear(), dateInfo.getMonth(), dateInfo.getDate(), hours, minutes, seconds);
};
exports.excelDateToJSDate = excelDateToJSDate;
const convertSelectedMonthToDate = (selectedMonth) => {
    return dayjs_1.default.utc({ year: selectedMonth.year, month: selectedMonth.month - 1, day: 1 }).toDate();
};
exports.convertSelectedMonthToDate = convertSelectedMonthToDate;
const getStartOfMonthsBetweenDates = (startDate, endDate) => {
    let start = dayjs_1.default.utc(startDate).startOf('month');
    const end = dayjs_1.default.utc(endDate).startOf('month');
    const dates = {};
    while (start.isBefore(end) || start.isSame(end)) {
        dates[start.format('YYYY-MM')] = { year: start.year(), month: start.month() + 1 };
        start = start.add(1, 'month');
    }
    return dates;
};
exports.getStartOfMonthsBetweenDates = getStartOfMonthsBetweenDates;
const getDays = (period) => {
    const days = [];
    for (let day = (0, dayjs_1.default)(period.start); day.isBefore(period.end); day = day.add(1, 'day')) {
        days.push(day.format('YYYY-MM-DD'));
    }
    return days;
};
exports.getDays = getDays;
const getShiftedMonth = (month, shift) => {
    const nextMonth = (0, dayjs_1.default)().utc().year(month.year).month(month.month - 1).add(shift, 'month');
    return { year: nextMonth.year(), month: nextMonth.month() + 1 };
};
exports.getShiftedMonth = getShiftedMonth;
const isProductActive = (startDate, deactivationDate, checkDate) => {
    const check = (0, dayjs_1.default)(checkDate);
    // If startDate exists and is in the future → not active yet
    if (startDate && (0, dayjs_1.default)(startDate).isAfter(check)) {
        return false;
    }
    // If no deactivation date → active (and already started)
    if (!deactivationDate) {
        return true;
    }
    const deactivation = (0, dayjs_1.default)(deactivationDate);
    // Active if checkDate is before deactivationDate
    return check.isBefore(deactivation);
};
exports.isProductActive = isProductActive;
