// engine/detectors/library-detector-multilang-b.ts — Java + PHP library detection
// SPEC-004 tech-debt: Java and PHP library detection
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
// === Java library catalog ===
const JAVA_UTIL_LIBS = ['lombok', 'mapstruct', 'guava'];
const JAVA_MIGRATION_LIBS = ['flyway-core', 'liquibase-core'];
const JAVA_RESILIENCE_LIBS = ['resilience4j-core', 'hystrix-core'];
const JAVA_TESTING_LIBS = ['testcontainers', 'archunit', 'jmockit'];
// === PHP library catalog ===
const PHP_HTTP_LIBS = ['guzzlehttp/guzzle', 'symfony/http-client'];
const PHP_QA_LIBS = ['phpstan/phpstan', 'vimeo/psalm', 'squizlabs/php_codesniffer'];
const PHP_UTIL_LIBS = [
    'spatie/laravel-permission',
    'spatie/laravel-data',
    'spatie/laravel-query-builder',
];
// === Detection helpers ===
function filterPresent(catalog, deps) {
    return catalog.filter((lib) => deps.has(lib));
}
// === Java dependency collection ===
function parsePomXml(content) {
    const deps = new Set();
    // Extract artifactId values inside <dependency> blocks
    const depBlocks = content.matchAll(/<dependency>[\s\S]*?<\/dependency>/g);
    for (const block of depBlocks) {
        const artifactMatch = /<artifactId>([^<]+)<\/artifactId>/.exec(block[0]);
        /* v8 ignore next 2 */
        if (artifactMatch?.[1]) {
            deps.add(artifactMatch[1].trim());
        }
    }
    return deps;
}
function parseBuildGradle(content) {
    const deps = new Set();
    // Match both Groovy style: implementation 'group:artifact:version'
    // and Kotlin DSL style: implementation("group:artifact:version")
    const matches = content.matchAll(/(?:implementation|testImplementation|api|runtimeOnly)\s*\(?\s*["']([^"']+)["']/g);
    for (const match of matches) {
        /* v8 ignore start */
        if (match[1]) {
            /* v8 ignore stop */
            const parts = match[1].split(':');
            const artifact = parts[1];
            if (artifact) {
                deps.add(artifact.trim());
            }
        }
    }
    return deps;
}
async function collectJavaDeps(projectPath) {
    const [pomContent, gradleContent, gradleKtsContent] = await Promise.all([
        readFile(join(projectPath, 'pom.xml'), 'utf-8').catch(() => ''),
        readFile(join(projectPath, 'build.gradle'), 'utf-8').catch(() => ''),
        readFile(join(projectPath, 'build.gradle.kts'), 'utf-8').catch(() => ''),
    ]);
    const pomDeps = pomContent ? parsePomXml(pomContent) : new Set();
    const gradleDeps = gradleContent ? parseBuildGradle(gradleContent) : new Set();
    const gradleKtsDeps = gradleKtsContent ? parseBuildGradle(gradleKtsContent) : new Set();
    return new Set([...pomDeps, ...gradleDeps, ...gradleKtsDeps]);
}
// === PHP dependency collection ===
async function collectComposerJson(projectPath) {
    try {
        const content = await readFile(join(projectPath, 'composer.json'), 'utf-8');
        const composer = JSON.parse(content);
        return new Set([
            ...Object.keys(composer.require ?? {}),
            ...Object.keys(composer['require-dev'] ?? {}),
        ]);
    }
    catch {
        return new Set();
    }
}
// === Public API ===
/**
 * Detect popular Java libraries in a project.
 * Reads pom.xml, build.gradle, or build.gradle.kts.
 */
export async function detectJavaLibraries(projectPath) {
    const deps = await collectJavaDeps(projectPath);
    return {
        util: filterPresent(JAVA_UTIL_LIBS, deps),
        migration: filterPresent(JAVA_MIGRATION_LIBS, deps),
        resilience: filterPresent(JAVA_RESILIENCE_LIBS, deps),
        testing: filterPresent(JAVA_TESTING_LIBS, deps),
    };
}
/**
 * Detect popular PHP packages in a project.
 * Reads composer.json (require + require-dev).
 */
export async function detectPhpLibraries(projectPath) {
    const deps = await collectComposerJson(projectPath);
    return {
        http: filterPresent(PHP_HTTP_LIBS, deps),
        qa: filterPresent(PHP_QA_LIBS, deps),
        util: filterPresent(PHP_UTIL_LIBS, deps),
    };
}
//# sourceMappingURL=library-detector-multilang-b.js.map