import { useContext, useMemo } from 'react';
import { MONTHS_IN_YEAR } from '../../../../constants';
import { buildIsoDate, checkIsDateInRange, DateCompareDeep, isDateOutOfRange, } from '../../../../utils/date';
import { MinMaxDateContext } from '../../../MinMaxDateContext';
export const useMonthsGrid = ({ baseDate, selectedDate, selectedRanges, }) => {
    const { maxDate, minDate } = useContext(MinMaxDateContext);
    return useMemo(() => {
        const grid = [];
        const startDate = buildIsoDate({ year: baseDate.getUTCFullYear() });
        const year = startDate.getUTCFullYear();
        const currentDate = new Date();
        const currentMonth = currentDate.getMonth();
        const currentYear = currentDate.getFullYear();
        const selectedMonth = selectedDate?.getUTCMonth();
        const selectedYear = selectedDate?.getUTCFullYear();
        for (let i = 0; i < MONTHS_IN_YEAR; i++) {
            const date = buildIsoDate({ year, month: i + 1 });
            grid.push({
                date,
                isSelected: selectedMonth === i && selectedYear === year,
                month: i + 1,
                isCurrentInUserLocalTime: i === currentMonth && year === currentYear,
                isInSelectedRange: selectedRanges?.some(({ dateA, dateB }) => checkIsDateInRange({
                    date,
                    dateA,
                    dateB,
                    deep: DateCompareDeep.Month,
                })) ?? false,
                isDisabled: isDateOutOfRange({
                    date,
                    dateA: minDate,
                    dateB: maxDate,
                    deep: DateCompareDeep.Month,
                }),
            });
        }
        return grid;
    }, [baseDate, selectedDate, maxDate, minDate, selectedRanges]);
};
