// engine/detectors/library-detector-langs5.ts — Legacy language detection
// SPEC-004 tech-debt S4: COBOL, Fortran, Ada, V lang, ColdFusion
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
// === Helpers ===
async function globFiles(pattern, projectPath) {
    try {
        return await glob(pattern, { cwd: projectPath, absolute: false, ignore: ['node_modules/**'] });
    }
    catch {
        return [];
    }
}
// === V lang parser ===
/**
 * Parse v.mod for `deps: [...]` section.
 * Matches quoted dependency names.
 */
export function parseVMod(content) {
    const deps = new Set();
    const sectionMatch = /deps\s*:\s*\[([\s\S]*?)\]/m.exec(content);
    if (sectionMatch?.[1]) {
        const matches = sectionMatch[1].matchAll(/'([^']+)'/g);
        for (const match of matches) {
            /* v8 ignore start */
            if (match[1]) {
                /* v8 ignore stop */
                deps.add(match[1]);
            }
        }
        // Also match double-quoted
        const dqMatches = sectionMatch[1].matchAll(/"([^"]+)"/g);
        for (const match of dqMatches) {
            /* v8 ignore start */
            if (match[1]) {
                /* v8 ignore stop */
                deps.add(match[1]);
            }
        }
    }
    return deps;
}
// === Ada parser ===
/**
 * Parse alire.toml dependencies block.
 * Matches `name = { version = "..." }` entries.
 */
export function parseAlireToml(content) {
    const deps = new Set();
    const sectionMatch = /\[dependencies\]([\s\S]*?)(?:\n\[|$)/s.exec(content);
    if (sectionMatch?.[1]) {
        const matches = sectionMatch[1].matchAll(/^([a-zA-Z0-9_-]+)\s*=/gm);
        for (const match of matches) {
            /* v8 ignore start */
            if (match[1]) {
                /* v8 ignore stop */
                deps.add(match[1]);
            }
        }
    }
    return deps;
}
// === ColdFusion server detection ===
/**
 * Determine ColdFusion server type by checking config filenames.
 */
export function detectColdFusionServer(files) {
    const names = files.map((f) => f.toLowerCase());
    if (names.some((n) => n.includes('lucee'))) {
        return 'lucee';
    }
    if (names.some((n) => n.includes('adobe') || n.includes('cfconfig'))) {
        return 'adobe';
    }
    return 'unknown';
}
// === Public API ===
/**
 * Detect COBOL project by scanning for *.cob / *.cbl files.
 */
export async function detectCobolProject(projectPath) {
    const [cobFiles, cblFiles] = await Promise.all([
        globFiles('**/*.cob', projectPath),
        globFiles('**/*.cbl', projectPath),
    ]);
    const files = [...cobFiles, ...cblFiles];
    return { isCobol: files.length > 0, files };
}
/**
 * Detect Fortran project by scanning for common Fortran source extensions.
 */
export async function detectFortranProject(projectPath) {
    const extensions = ['**/*.f90', '**/*.f95', '**/*.f03', '**/*.for', '**/*.f'];
    const found = await Promise.all(extensions.map((p) => globFiles(p, projectPath)));
    const files = found.flat();
    return { isFortran: files.length > 0, files };
}
/**
 * Detect Ada project by scanning for *.adb / *.ads files
 * and reading alire.toml for package names.
 */
export async function detectAdaProject(projectPath) {
    const [adbFiles, adsFiles] = await Promise.all([
        globFiles('**/*.adb', projectPath),
        globFiles('**/*.ads', projectPath),
    ]);
    const files = [...adbFiles, ...adsFiles];
    let packages = [];
    try {
        const alireContent = await readFile(join(projectPath, 'alire.toml'), 'utf-8');
        packages = [...parseAlireToml(alireContent)];
    }
    catch {
        // no alire.toml — that's fine
    }
    return { isAda: files.length > 0, packages };
}
/**
 * Detect V lang project from v.mod dependencies.
 */
export async function detectVLangLibraries(projectPath) {
    try {
        const content = await readFile(join(projectPath, 'v.mod'), 'utf-8');
        const deps = parseVMod(content);
        const web = ['vweb', 'heroku', 'vex'].filter((lib) => deps.has(lib));
        const db = ['db', 'pg', 'sqlite', 'mysql'].filter((lib) => deps.has(lib));
        return { web, db };
    }
    catch {
        return { web: [], db: [] };
    }
}
/**
 * Detect ColdFusion project by scanning for *.cfm / *.cfc files.
 * Determines server type (adobe / lucee / unknown) from config files present.
 */
export async function detectColdFusionProject(projectPath) {
    const [cfmFiles, cfcFiles] = await Promise.all([
        globFiles('**/*.cfm', projectPath),
        globFiles('**/*.cfc', projectPath),
    ]);
    const files = [...cfmFiles, ...cfcFiles];
    const server = detectColdFusionServer(files);
    return { isColdFusion: files.length > 0, server };
}
//# sourceMappingURL=library-detector-langs5.js.map