"use strict";
/**
 * `vyuh-dxkit explore communities` — natural-module summary.
 *
 * Pure pipeline: parse args → call communitiesQuery → format output.
 * Per CLAUDE.md Rule 12: graph traversal flows through queries.ts.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCommunities = runCommunities;
const queries_1 = require("../queries");
const format_1 = require("../format");
const DEFAULT_LIMIT = 8;
function runCommunities(graph, _positionals, values) {
    const limit = parseLimit(values.limit, DEFAULT_LIMIT);
    const results = (0, queries_1.communitiesQuery)(graph, limit);
    if (values.json) {
        (0, format_1.printJson)((0, format_1.envelope)('explore.communities', { limit }, graph, results));
        return;
    }
    if (results.length === 0) {
        (0, format_1.printMarkdown)((0, format_1.markdownHeader)('Communities', 'natural modules in this repo', graph), 'No communities found in the graph. Either the repo is too small for Louvain clustering to find structure, or the graph artifact is empty.');
        return;
    }
    const rows = results.map((r) => ({
        ID: r.id,
        Dir: r.dominantSourceDir || '—',
        Pack: r.dominantPack || '—',
        Nodes: r.nodeCount,
        Cohesion: r.cohesion.toFixed(2),
        'Top hot files': r.topHotFiles.map((f) => basename(f)).join(', ') || '—',
    }));
    const headers = ['ID', 'Dir', 'Pack', 'Nodes', 'Cohesion', 'Top hot files'];
    (0, format_1.printMarkdown)((0, format_1.markdownHeader)('Communities', 'natural modules in this repo', graph), (0, format_1.markdownTable)(headers, rows), (0, format_1.markdownFooter)("Drill into a community's central file: `vyuh-dxkit explore file <path>`."));
}
function basename(p) {
    const i = p.lastIndexOf('/');
    return i >= 0 ? p.slice(i + 1) : p;
}
function parseLimit(raw, defaultValue) {
    if (!raw)
        return defaultValue;
    const n = parseInt(raw, 10);
    if (isNaN(n) || n < 1)
        return defaultValue;
    return Math.min(n, 1000);
}
//# sourceMappingURL=communities.js.map