/**
 * Nexus Prime — Topology Priors
 *
 * Computes reliable topology patterns from run history.
 */
export function computeTopologyPriors(history) {
    const grouped = new Map();
    for (const entry of history) {
        let g = grouped.get(entry.taskType);
        if (!g) {
            g = { successes: 0, total: 0, modes: new Map(), workers: new Map() };
            grouped.set(entry.taskType, g);
        }
        g.total++;
        if (entry.wasSuccessful) {
            g.successes++;
            g.modes.set(entry.mode, (g.modes.get(entry.mode) ?? 0) + 1);
            g.workers.set(entry.workerCount, (g.workers.get(entry.workerCount) ?? 0) + 1);
        }
    }
    const priors = [];
    for (const [taskType, g] of grouped) {
        const bestMode = [...g.modes.entries()].sort((a, b) => b[1] - a[1])[0];
        const bestWorkers = [...g.workers.entries()].sort((a, b) => b[1] - a[1])[0];
        if (bestMode && bestWorkers) {
            priors.push({
                taskType,
                preferredWorkerCount: bestWorkers[0],
                preferredMode: bestMode[0],
                successRate: g.total > 0 ? g.successes / g.total : 0,
                sampleCount: g.total,
            });
        }
    }
    return priors.sort((a, b) => b.successRate - a.successRate);
}
