// impact-analyzer.ts — Scan project for code affected by breaking changes (SPEC-041)
import { readFile } from 'node:fs/promises';
import { glob } from 'glob';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const DEFAULT_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs'];
const EXCLUDE_DIRS = ['**/node_modules/**', '**/dist/**', '**/build/**', '**/.git/**'];
// ---------------------------------------------------------------------------
// File scanner
// ---------------------------------------------------------------------------
async function findSourceFiles(projectPath) {
    const patterns = DEFAULT_EXTENSIONS.map((ext) => `${projectPath}/**/*.${ext}`);
    const results = [];
    for (const pattern of patterns) {
        const files = await glob(pattern, { ignore: EXCLUDE_DIRS });
        results.push(...files);
    }
    return [...new Set(results)];
}
async function fileMatchesPattern(filePath, pattern) {
    /* v8 ignore next */
    if (!pattern) {
        return false;
    }
    try {
        const content = await readFile(filePath, 'utf-8');
        const regex = new RegExp(pattern, 'i');
        return regex.test(content);
    }
    catch {
        return false;
    }
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * For each breaking change, scan the project source files to determine
 * whether the project is actually impacted (sets impact field).
 */
export async function analyzeImpact(projectPath, breakingChanges) {
    if (breakingChanges.length === 0) {
        return [];
    }
    const sourceFiles = await findSourceFiles(projectPath);
    const results = await Promise.all(breakingChanges.map(async (bc) => {
        if (!bc.affectedPattern) {
            return { ...bc, impact: 'unknown' };
        }
        let impacted = false;
        for (const file of sourceFiles) {
            if (await fileMatchesPattern(file, bc.affectedPattern)) {
                impacted = true;
                break;
            }
        }
        return {
            ...bc,
            impact: (impacted ? 'impacted' : 'not_impacted'),
        };
    }));
    return results;
}
//# sourceMappingURL=impact-analyzer.js.map