"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clampDayToMonth = clampDayToMonth;
exports.generateMonthlyDueDates = generateMonthlyDueDates;
exports.generateQuarterlyDueDates = generateQuarterlyDueDates;
exports.yearForMonthInAY = yearForMonthInAY;
exports.buildQuarterLabels = buildQuarterLabels;
function clampDayToMonth(year, month1, day) {
    const lastDayOfMonth = new Date(Date.UTC(year, month1, 0)).getUTCDate();
    return Math.min(Math.max(1, day), lastDayOfMonth);
}
function generateMonthlyDueDates(start, end, dueDay) {
    const out = [];
    let year = start.getUTCFullYear();
    let month = start.getUTCMonth() + 1;
    const startDay = start.getUTCDate();
    const endYear = end.getUTCFullYear();
    const endMonth = end.getUTCMonth() + 1;
    if (dueDay < startDay) {
        month++;
        if (month > 12) {
            month = 1;
            year++;
        }
    }
    for (let guard = 0; guard < 60; guard++) {
        if (year > endYear || (year === endYear && month > endMonth))
            break;
        const day = clampDayToMonth(year, month, dueDay);
        out.push(new Date(Date.UTC(year, month - 1, day)));
        if (year === endYear && month === endMonth)
            break;
        month++;
        if (month > 12) {
            month = 1;
            year++;
        }
    }
    return out;
}
function generateQuarterlyDueDates(start, _end, dueDay) {
    const out = [];
    const startYear = start.getUTCFullYear();
    const startMonth = start.getUTCMonth() + 1;
    for (const offset of [0, 3, 6, 9]) {
        const rawMonth = startMonth + offset;
        const year = startYear + Math.floor((rawMonth - 1) / 12);
        const month = ((rawMonth - 1) % 12) + 1;
        const day = clampDayToMonth(year, month, dueDay);
        out.push(new Date(Date.UTC(year, month - 1, day)));
    }
    return out;
}
function yearForMonthInAY(start, month1) {
    const startYear = start.getUTCFullYear();
    const startMonth = start.getUTCMonth() + 1;
    return month1 >= startMonth ? startYear : startYear + 1;
}
const MONTH_SHORT = [
    '', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
];
function buildQuarterLabels(startMonth1) {
    const wrap = (m) => ((m - 1) % 12) + 1;
    return [0, 3, 6, 9].map((offset, qi) => {
        const first = wrap(startMonth1 + offset);
        const last = wrap(startMonth1 + offset + 2);
        return `Q${qi + 1} (${MONTH_SHORT[first]}-${MONTH_SHORT[last]})`;
    });
}
//# sourceMappingURL=fees.installment-dates.js.map