// engine/detectors/library-detector-multilang-a.ts — Go + Rust library detection
// SPEC-004 tech-debt: Go and Rust library detection
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
// === Go library catalog ===
const GO_HTTP_LIBS = [
    'github.com/valyala/fasthttp',
    'github.com/gofiber/fiber',
    'github.com/gin-gonic/gin',
    'github.com/labstack/echo',
    'github.com/go-chi/chi',
    'github.com/go-resty/resty',
];
const GO_TESTING_LIBS = [
    'github.com/stretchr/testify',
    'github.com/golang/mock',
    'gopkg.in/check.v1',
];
const GO_DB_LIBS = [
    'github.com/jackc/pgx',
    'github.com/sqlc-dev/sqlc',
    'entgo.io/ent',
    'gorm.io/gorm',
    'github.com/jmoiron/sqlx',
];
// === Rust crate catalog ===
const RUST_ASYNC_CRATES = ['tokio', 'async-std', 'smol'];
const RUST_SERDE_CRATES = ['serde', 'serde_json', 'serde_yaml'];
const RUST_CLI_CRATES = ['clap', 'structopt', 'argh'];
const RUST_ERROR_CRATES = ['thiserror', 'anyhow', 'eyre'];
const RUST_DB_CRATES = ['sqlx', 'diesel', 'sea-orm'];
// === Detection helpers ===
function filterPresent(catalog, deps) {
    return catalog.filter((lib) => deps.has(lib));
}
// === Go dependency collection ===
function parseGoMod(content) {
    const deps = new Set();
    // Match individual require lines: require github.com/foo/bar v1.2.3
    const singleMatches = content.matchAll(/^require\s+([\w./-]+)/gm);
    for (const match of singleMatches) {
        /* v8 ignore next 2 */
        if (match[1]) {
            deps.add(match[1]);
        }
    }
    // Match require blocks: require ( ... )
    const blockMatch = /require\s*\(([^)]+)\)/s.exec(content);
    if (blockMatch?.[1]) {
        const blockLines = blockMatch[1].split('\n');
        for (const line of blockLines) {
            const trimmed = line.trim();
            if (!trimmed || trimmed.startsWith('//')) {
                continue;
            }
            const modulePath = trimmed.split(/\s+/)[0];
            if (modulePath) {
                deps.add(modulePath);
            }
        }
    }
    return deps;
}
async function collectGoMod(projectPath) {
    try {
        const content = await readFile(join(projectPath, 'go.mod'), 'utf-8');
        return parseGoMod(content);
    }
    catch {
        return new Set();
    }
}
// === Rust dependency collection ===
function parseCargoToml(content) {
    const deps = new Set();
    // Match lines in [dependencies] / [dev-dependencies]: key = "version" or key = { version = ... }
    const matches = content.matchAll(/^([a-zA-Z0-9_-]+)\s*=/gm);
    for (const match of matches) {
        /* v8 ignore next 2 */
        if (match[1] && !match[1].startsWith('[')) {
            deps.add(match[1]);
        }
    }
    return deps;
}
async function collectCargoToml(projectPath) {
    try {
        const content = await readFile(join(projectPath, 'Cargo.toml'), 'utf-8');
        return parseCargoToml(content);
    }
    catch {
        return new Set();
    }
}
// === Public API ===
/**
 * Detect popular Go libraries in a project.
 * Reads go.mod for require blocks.
 */
export async function detectGoLibraries(projectPath) {
    const deps = await collectGoMod(projectPath);
    return {
        http: filterPresent(GO_HTTP_LIBS, deps),
        testing: filterPresent(GO_TESTING_LIBS, deps),
        db: filterPresent(GO_DB_LIBS, deps),
    };
}
/**
 * Detect popular Rust crates in a project.
 * Reads Cargo.toml for [dependencies] and [dev-dependencies].
 */
export async function detectRustCrates(projectPath) {
    const deps = await collectCargoToml(projectPath);
    return {
        async: filterPresent(RUST_ASYNC_CRATES, deps),
        serde: filterPresent(RUST_SERDE_CRATES, deps),
        cli: filterPresent(RUST_CLI_CRATES, deps),
        error: filterPresent(RUST_ERROR_CRATES, deps),
        db: filterPresent(RUST_DB_CRATES, deps),
    };
}
//# sourceMappingURL=library-detector-multilang-a.js.map