import { useMemo } from 'react';
import { computeGroupSummary } from '../GanttChart.utils.js';

function useGanttLayout(groups, tasks, collapsedGroups, hideCompleted) {
    return useMemo(() => {
        const sorted = [...groups].sort((a, b) => { var _a, _b; return ((_a = a.order) !== null && _a !== void 0 ? _a : 0) - ((_b = b.order) !== null && _b !== void 0 ? _b : 0); });
        const rows = [];
        for (const group of sorted) {
            const groupTasks = tasks.filter((t) => t.groupId === group.id);
            const visibleTasks = hideCompleted
                ? groupTasks.filter((t) => !t.completed)
                : groupTasks;
            const summary = computeGroupSummary(group, groupTasks);
            rows.push({
                type: "group",
                group,
                taskCount: visibleTasks.length,
                completedCount: summary.completedCount,
                earliestStart: summary.earliestStart,
                latestEnd: summary.latestEnd,
            });
            const isCollapsed = collapsedGroups.includes(group.id);
            if (!isCollapsed) {
                for (const task of visibleTasks) {
                    rows.push({ type: "task", task, group });
                }
            }
        }
        return rows;
    }, [groups, tasks, collapsedGroups, hideCompleted]);
}

export { useGanttLayout };
