import { useMemo } from 'react';
import { DAY_WIDTHS } from '../GanttChart.types.js';
import { autoDateRange, generateDayColumns, generateMonthSpans, getDayOffset } from '../GanttChart.utils.js';
import { toDate, isSameDay } from '../../../../utils/dateUtils.js';

function useGanttDates(tasks, zoom, startDate, endDate) {
    return useMemo(() => {
        const auto = autoDateRange(tasks);
        const rangeStart = startDate ? toDate(startDate) : auto.start;
        const rangeEnd = endDate ? toDate(endDate) : auto.end;
        const days = generateDayColumns(rangeStart, rangeEnd);
        const monthSpans = generateMonthSpans(days);
        const totalDays = days.length;
        const dayWidth = DAY_WIDTHS[zoom];
        const today = new Date();
        today.setHours(0, 0, 0, 0);
        const todayInRange = days.some((d) => isSameDay(d, today));
        const todayOffset = todayInRange
            ? getDayOffset(today, rangeStart)
            : null;
        return { rangeStart, rangeEnd, days, monthSpans, totalDays, dayWidth, todayOffset };
    }, [tasks, zoom, startDate, endDate]);
}

export { useGanttDates };
