/**
 * Nexus Prime — Verifier Priors
 *
 * Tracks common verifier failure patterns for early detection.
 */
export function computeVerifierPriors(history) {
    const grouped = new Map();
    for (let i = 0; i < history.length; i++) {
        const entry = history[i];
        let g = grouped.get(entry.checkName);
        if (!g) {
            g = { failures: 0, total: 0, recentFailures: 0, causes: new Map() };
            grouped.set(entry.checkName, g);
        }
        g.total++;
        if (!entry.passed) {
            g.failures++;
            if (i >= history.length - 10)
                g.recentFailures++;
            g.causes.set(entry.detail, (g.causes.get(entry.detail) ?? 0) + 1);
        }
    }
    const priors = [];
    for (const [checkName, g] of grouped) {
        const topCause = [...g.causes.entries()].sort((a, b) => b[1] - a[1])[0];
        priors.push({
            checkName,
            failureRate: g.total > 0 ? g.failures / g.total : 0,
            recentFailures: g.recentFailures,
            commonCause: topCause?.[0] ?? 'unknown',
        });
    }
    return priors.sort((a, b) => b.failureRate - a.failureRate);
}
