'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { BurndownChart } from './burndown-chart.js';
import { StatusBreakdown } from './status-breakdown.js';
import { ModuleProgressChart } from './module-progress-chart.js';
import { getProjectBreakdown } from '../../../app/actions/get-project-breakdown.js';
import { getCycleBurndown } from '../../../app/actions/get-cycle-burndown.js';
import { getModuleProgress } from '../../../app/actions/get-module-progress.js';
export function AnalyticsDashboard({ projectId, cycles, className }) {
    const [breakdown, setBreakdown] = useState(null);
    const [burndown, setBurndown] = useState(null);
    const [modules, setModules] = useState([]);
    const [loading, setLoading] = useState(true);
    const activeCycle = cycles.find((c) => c.status === 'Active');
    useEffect(() => {
        async function load() {
            setLoading(true);
            const [breakdownResult, moduleResult, burndownResult] = await Promise.all([
                getProjectBreakdown(projectId),
                getModuleProgress(projectId),
                activeCycle ? getCycleBurndown(activeCycle.id) : Promise.resolve(null),
            ]);
            if (breakdownResult?.data) {
                setBreakdown({
                    totalItems: breakdownResult.data.totalItems,
                    byState: breakdownResult.data.byState,
                    byPriority: breakdownResult.data.byPriority,
                });
            }
            if (moduleResult?.modules) {
                setModules(moduleResult.modules);
            }
            if (burndownResult?.data) {
                setBurndown({
                    cycleName: burndownResult.data.cycleName,
                    totalItems: burndownResult.data.totalItems,
                    completedItems: burndownResult.data.completedItems,
                    dataPoints: burndownResult.data.dataPoints,
                });
            }
            setLoading(false);
        }
        load();
    }, [projectId, activeCycle]);
    if (loading) {
        return (_jsx("div", { className: "flex items-center justify-center py-12", children: _jsx("span", { className: "text-muted-foreground text-xs", children: "Loading analytics..." }) }));
    }
    return (_jsx("div", { "data-testid": "analytics-dashboard", className: className, children: _jsxs("div", { className: "space-y-6", children: [breakdown ? (_jsx(StatusBreakdown, { byState: breakdown.byState, byPriority: breakdown.byPriority, totalItems: breakdown.totalItems })) : null, burndown ? (_jsx(BurndownChart, { dataPoints: burndown.dataPoints, cycleName: burndown.cycleName, totalItems: burndown.totalItems, completedItems: burndown.completedItems })) : null, _jsx(ModuleProgressChart, { modules: modules })] }) }));
}
