/**
 * Task analysis and prioritization core module.
 * @task T4538
 * @epic T4454
 */
import { getAccessor } from '../store/data-accessor.js';
/** Analyze task priority with leverage scoring. */
export async function analyzeTaskPriority(opts, accessor) {
    const acc = accessor ?? (await getAccessor(opts.cwd));
    const { tasks } = await acc.queryTasks({});
    // Build dependency graph
    const blocksMap = {};
    for (const task of tasks) {
        if (task.depends) {
            for (const dep of task.depends) {
                if (!blocksMap[dep])
                    blocksMap[dep] = [];
                blocksMap[dep].push(task.id);
            }
        }
    }
    // Calculate leverage for each task
    const leverageMap = {};
    for (const task of tasks) {
        leverageMap[task.id] = (blocksMap[task.id] ?? []).length;
    }
    // Find actionable tasks (pending/active, not blocked)
    const actionable = tasks.filter((t) => t.status === 'pending' || t.status === 'active');
    const blocked = tasks.filter((t) => t.status === 'blocked');
    // Bottlenecks (tasks that block the most others)
    const bottlenecks = tasks
        .filter((t) => (blocksMap[t.id]?.length ?? 0) > 0 && t.status !== 'done')
        .map((t) => ({ id: t.id, title: t.title, blocksCount: blocksMap[t.id].length }))
        .sort((a, b) => b.blocksCount - a.blocksCount)
        .slice(0, 5);
    // Tier tasks
    const scored = actionable.map((t) => ({
        id: t.id,
        title: t.title,
        leverage: leverageMap[t.id] ?? 0,
        priority: t.priority,
    }));
    scored.sort((a, b) => {
        const priorityWeight = { critical: 100, high: 50, medium: 20, low: 5 };
        const aScore = (priorityWeight[a.priority ?? 'medium'] ?? 20) + a.leverage * 10;
        const bScore = (priorityWeight[b.priority ?? 'medium'] ?? 20) + b.leverage * 10;
        return bScore - aScore;
    });
    const critical = scored.filter((t) => t.priority === 'critical');
    const high = scored.filter((t) => t.priority === 'high');
    const normal = scored.filter((t) => t.priority !== 'critical' && t.priority !== 'high');
    const recommended = scored.length > 0
        ? {
            id: scored[0].id,
            title: scored[0].title,
            leverage: scored[0].leverage,
            reason: 'Highest combined priority and leverage score',
        }
        : null;
    const totalLeverage = Object.values(leverageMap).reduce((s, v) => s + v, 0);
    const avgLeverage = tasks.length > 0 ? Math.round((totalLeverage / tasks.length) * 100) / 100 : 0;
    let autoStarted = false;
    if (opts.autoStart && recommended) {
        const currentFocus = await acc.getMetaValue('focus_state');
        await acc.setMetaValue('focus_state', { ...(currentFocus ?? {}), currentTask: recommended.id });
        autoStarted = true;
    }
    return {
        recommended,
        bottlenecks,
        tiers: {
            critical: critical.map(({ id, title, leverage }) => ({ id, title, leverage })),
            high: high.map(({ id, title, leverage }) => ({ id, title, leverage })),
            normal: normal.slice(0, 10).map(({ id, title, leverage }) => ({ id, title, leverage })),
        },
        metrics: {
            totalTasks: tasks.length,
            actionable: actionable.length,
            blocked: blocked.length,
            avgLeverage,
        },
        ...(autoStarted && { autoStarted }),
    };
}
//# sourceMappingURL=analyze.js.map