import { execFileSync } from "node:child_process";
export function parseGoMod(source) {
    const nameMatch = source.match(/^module\s+(\S+)/m);
    const deps = [];
    const requireBlock = source.match(/require\s*\(([^)]*)\)/);
    if (requireBlock?.[1]) {
        for (const line of requireBlock[1].split("\n")) {
            const m = line.trim().match(/^([^\s]+)\s+/);
            if (m?.[1])
                deps.push(m[1]);
        }
    }
    for (const m of source.matchAll(/^require\s+([^\s]+)\s+/gm)) {
        if (m[1])
            deps.push(m[1]);
    }
    return { name: nameMatch?.[1] ?? null, deps };
}
/** Narrow regex-based parse of pyproject.toml `[project]` section. */
export function parsePyprojectToml(source) {
    const section = extractTomlSection(source, "project");
    const name = matchTomlKey(section, "name");
    const version = matchTomlKey(section, "version");
    const description = matchTomlKey(section, "description");
    const deps = [];
    const depsArray = section.match(/dependencies\s*=\s*\[([\s\S]*?)\]/);
    if (depsArray?.[1]) {
        for (const m of depsArray[1].matchAll(/"([^"]+)"/g)) {
            // e.g. "fastapi>=0.100" -> fastapi
            const base = m[1].split(/[<>=\s!~]/)[0];
            if (base)
                deps.push(base);
        }
    }
    return { name, version, description, deps };
}
export function parseCargoToml(source) {
    const section = extractTomlSection(source, "package");
    const depsSection = extractTomlSection(source, "dependencies");
    const deps = [];
    for (const m of depsSection.matchAll(/^([A-Za-z0-9_-]+)\s*=/gm)) {
        if (m[1])
            deps.push(m[1]);
    }
    return {
        name: matchTomlKey(section, "name"),
        version: matchTomlKey(section, "version"),
        description: matchTomlKey(section, "description"),
        deps,
    };
}
function extractTomlSection(source, name) {
    // Find the header line, then capture everything until the next section header
    // or end of input.
    const lines = source.split("\n");
    const headerRe = new RegExp(`^\\[${name}\\]\\s*$`);
    let startIdx = -1;
    for (let i = 0; i < lines.length; i++) {
        if (headerRe.test(lines[i])) {
            startIdx = i + 1;
            break;
        }
    }
    if (startIdx < 0)
        return "";
    let endIdx = lines.length;
    for (let i = startIdx; i < lines.length; i++) {
        if (/^\[[^\]]+\]\s*$/.test(lines[i])) {
            endIdx = i;
            break;
        }
    }
    return lines.slice(startIdx, endIdx).join("\n");
}
function matchTomlKey(section, key) {
    const m = section.match(new RegExp(`^\\s*${key}\\s*=\\s*"([^"]*)"`, "m"));
    return m?.[1] ?? null;
}
/** Detect shallow clone (git rev-parse --is-shallow-repository). Safe on
 *  missing git binary / non-repo / permission errors. */
export function isShallowClone(root) {
    try {
        const out = execFileSync("git", ["rev-parse", "--is-shallow-repository"], {
            cwd: root,
            stdio: ["ignore", "pipe", "ignore"],
        });
        return out.toString().trim() === "true";
    }
    catch {
        return false;
    }
}
/** Architectural relevance table for selectKeyDependencies. */
const ARCH_RELEVANT = new Set([
    // Frameworks
    "react", "next", "vue", "angular", "svelte", "solid-js", "nuxt", "gatsby", "remix",
    "express", "fastify", "hono", "@nestjs/core", "koa", "hapi",
    "fastapi", "flask", "django", "pyramid", "starlette",
    // DB / ORM
    "prisma", "@prisma/client", "pg", "mysql2", "mongodb", "mongoose", "drizzle-orm",
    "typeorm", "sequelize", "redis", "ioredis", "sqlalchemy", "peewee",
    // Testing
    "vitest", "jest", "mocha", "cypress", "playwright", "pytest",
    // Build / bundler
    "vite", "webpack", "rollup", "esbuild", "parcel", "tsup", "turbopack",
    // Language / type
    "typescript", "ts-node", "tsx",
    // Validation / schema
    "zod", "yup", "joi", "pydantic",
    // State / data
    "zustand", "redux", "@tanstack/react-query", "swr", "graphql", "apollo-server",
    "@trpc/server",
]);
export function selectKeyDependencies(projectResult) {
    const dh = projectResult.dependency_health;
    const prodTotal = dh?.prod ?? 0;
    const devTotal = dh?.dev ?? 0;
    const key = [];
    const kv = dh?.key_versions ?? {};
    for (const [name, version] of Object.entries(kv)) {
        if (key.length >= 15)
            break;
        const kind = ARCH_RELEVANT.has(name) ? "prod" : "prod";
        key.push({ name, version: String(version), kind });
    }
    return { prod_total: prodTotal, dev_total: devTotal, key };
}
//# sourceMappingURL=wiki-overview-sources.js.map