/**
 * pipeline/reverse-mapping.ts
 *
 * Builds a reverse index from document slugs to feature areas.
 *
 * The canonical_docs in each task YAML define which docs are relevant to
 * each task. This module inverts that mapping: given a document slug,
 * return which feature areas (and specific tasks) would be affected if
 * that document changed.
 *
 * This is the foundation for event-driven evaluation triggers — when a
 * content webhook fires for a document, we look up affected areas and
 * dispatch a scoped evaluation.
 *
 * @see docs/design-docs/report-store/visibility-workflows.md — Event-Driven Triggers
 */
import { resolveMappings } from "./resolve-mappings.js";
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Get all unique canonical doc slugs across all tasks.
 * Useful for checking if a webhook payload matches any tracked document.
 */
export function allTrackedSlugs(reverseMapping) {
    return [...reverseMapping.keys()].sort();
}
/**
 * Given a set of changed document slugs, determine which areas and tasks
 * are affected and should be re-evaluated.
 *
 * @param slugs - Document slugs that changed
 * @param reverseMapping - Pre-built reverse mapping (or will build from rootDir)
 * @returns Impact summary with deduplicated areas and tasks
 */
export function assessImpact(slugs, reverseMapping) {
    const allTargets = [];
    const unmatchedSlugs = [];
    for (const slug of slugs) {
        const targets = reverseMapping.get(slug);
        if (targets && targets.length > 0) {
            allTargets.push(...targets);
        }
        else {
            unmatchedSlugs.push(slug);
        }
    }
    // Deduplicate areas and task IDs
    const areas = [...new Set(allTargets.map((t) => t.area))].sort();
    const taskIds = [...new Set(allTargets.map((t) => t.taskId))].sort();
    return {
        areas,
        targets: allTargets,
        taskIds,
        unmatchedSlugs,
    };
}
/**
 * Build a reverse mapping from document slugs to feature areas.
 *
 * Reads all task YAML files and inverts the canonical_docs mappings.
 * A single slug may map to multiple areas/tasks (e.g., "groq-introduction"
 * is referenced by multiple GROQ tasks).
 *
 * @param rootDir - Path to the eval package root (packages/eval)
 * @returns Map from slug → array of affected targets
 */
export function buildReverseMapping(rootDir) {
    const mappings = resolveMappings(rootDir);
    return buildReverseMappingFromResolved(mappings);
}
/**
 * Build a reverse mapping from already-resolved mappings.
 * Useful for testing without filesystem access.
 */
export function buildReverseMappingFromResolved(mappings) {
    const reverse = new Map();
    for (const [area, config] of Object.entries(mappings.feature_areas)) {
        for (const task of config.tasks) {
            for (const doc of task.canonical_docs) {
                const existing = reverse.get(doc.slug) ?? [];
                existing.push({ area, taskId: task.id });
                reverse.set(doc.slug, existing);
            }
        }
    }
    return reverse;
}
