// engine/cross-repo-searcher.ts — SPEC-296: Search specs across multiple registered projects.
// Pure function — no I/O. Accepts pre-loaded specs per project, merges results.
import { searchSpecs } from './spec-searcher.js';
const DEFAULT_LIMIT = 50;
/**
 * Search specs across multiple projects.
 *
 * For each project bundle, delegates to searchSpecs (from spec-searcher) to
 * apply filtering and scoring, then merges all results, enriches them with
 * project context, sorts by matchScore descending, and applies the limit.
 *
 * Pure function — all I/O must happen before calling this.
 */
export function searchAcrossProjects(projectSpecs, input) {
    const limit = input.limit ?? DEFAULT_LIMIT;
    const allResults = [];
    for (const bundle of projectSpecs) {
        const { items } = searchSpecs(bundle.specs, {
            projectPath: bundle.projectPath,
            query: input.query,
            status: input.status,
            tags: input.tags,
            type: input.type,
            // No per-project limit — apply a global limit after merging
            limit: bundle.specs.length,
        });
        for (const item of items) {
            allResults.push({
                ...item,
                projectPath: bundle.projectPath,
                projectHash: bundle.projectHash,
            });
        }
    }
    // Sort by matchScore descending, then specId ascending for stability
    allResults.sort((a, b) => {
        if (b.matchScore !== a.matchScore) {
            return b.matchScore - a.matchScore;
        }
        return a.specId.localeCompare(b.specId);
    });
    const total = allResults.length;
    const items = allResults.slice(0, limit);
    return {
        total,
        items,
        scannedProjects: projectSpecs.length,
        failedProjects: 0,
    };
}
//# sourceMappingURL=cross-repo-searcher.js.map