"use strict";
/**
 * Import-graph test matching.
 *
 * Replaces the filename-based `matchTestsToSource` heuristic with something
 * that actually reflects what a test exercises. A source file is "tested"
 * when at least one active test file imports it, directly or transitively
 * through a small number of hops.
 *
 * The point is to rescue common real-world shapes the filename matcher
 * misses:
 *
 *   test/cli-init.test.ts  imports  src/cli.ts
 *     src/cli.ts           imports  src/generator.ts, src/detect.ts, ...
 *   → all of those count as tested even though none of their filenames
 *     contain "cli-init".
 *
 * Phase 10e.B.4.6: the BFS now walks a pre-computed, union-of-packs
 * edge map produced by the `IMPORTS` capability via the dispatcher.
 * Each pack's provider walks its own source extensions and resolves
 * in-pack edges; the dispatcher concatenates the per-pack graphs
 * before BFS starts. Go dead-ends naturally at package directories
 * (edge target is a dir, and no pack has an edge out of a dir), which
 * preserves the pre-refactor semantics.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildReachable = buildReachable;
const dispatcher_1 = require("../dispatcher");
const descriptors_1 = require("../../languages/capabilities/descriptors");
const languages_1 = require("../../languages");
/**
 * Build the set of source files reachable from the given test-file seeds by
 * following import edges up to maxHops. Paths are project-relative.
 */
async function buildReachable(seeds, cwd, options = {}) {
    // D010 fix: filter to packs active for this cwd. Imports gather is
    // expensive (find subprocess + per-file regex extraction); inactive
    // packs would walk the tree only to emit zero results.
    const providers = [];
    for (const lang of languages_1.LANGUAGES) {
        if (!lang.detect(cwd))
            continue;
        const p = lang.capabilities?.imports;
        if (p)
            providers.push(p);
    }
    const result = await dispatcher_1.defaultDispatcher.gather(cwd, descriptors_1.IMPORTS, providers);
    const edges = result?.edges ?? new Map();
    const maxHops = options.maxHops ?? 3;
    const reached = new Set();
    let frontier = seeds.slice();
    for (let hop = 0; hop <= maxHops && frontier.length > 0; hop++) {
        const next = [];
        for (const file of frontier) {
            const targets = edges.get(file);
            if (!targets)
                continue;
            for (const resolved of targets) {
                if (reached.has(resolved))
                    continue;
                reached.add(resolved);
                next.push(resolved);
            }
        }
        frontier = next;
    }
    return reached;
}
//# sourceMappingURL=import-graph.js.map