// engine/living-specs/file-scanner.ts — File existence, line count, export extraction (SPEC-330)
import { readFile, access } from 'node:fs/promises';
const EXPORT_REGEX = /export\s+(?:async\s+)?(?:function|class|const)\s+(\w+)/g;
const FILE_PATH_REGEX = /\b(?:src|tests)\/[\w/.-]+\.ts\b/g;
/**
 * Scan a single file: check existence, count lines, extract exports.
 */
export async function scanFile(filePath) {
    try {
        await access(filePath);
    }
    catch {
        return { path: filePath, exists: false, lineCount: 0, exportsFound: [] };
    }
    try {
        const content = await readFile(filePath, 'utf-8');
        const lineCount = content.split('\n').length;
        const exportsFound = extractExports(content);
        return { path: filePath, exists: true, lineCount, exportsFound };
    }
    catch {
        return { path: filePath, exists: true, lineCount: 0, exportsFound: [] };
    }
}
/**
 * Extract all exported symbol names from file content.
 */
function extractExports(content) {
    const found = [];
    let match;
    const regex = new RegExp(EXPORT_REGEX.source, 'g');
    while ((match = regex.exec(content)) !== null) {
        const name = match[1];
        if (name !== undefined && !found.includes(name)) {
            found.push(name);
        }
    }
    return found;
}
/**
 * Extract all file paths referenced in technical.md content (src/ and tests/ paths).
 * Deduplicates results.
 */
export function extractFilePaths(technicalMdContent) {
    const found = [];
    const regex = new RegExp(FILE_PATH_REGEX.source, 'g');
    let match;
    while ((match = regex.exec(technicalMdContent)) !== null) {
        const p = match[0];
        if (!found.includes(p)) {
            found.push(p);
        }
    }
    return found;
}
//# sourceMappingURL=file-scanner.js.map