// ---------------------------------------------------------------------------
// CLI handlers for wiki-generate and wiki-lint
// ---------------------------------------------------------------------------
import { getFlag, getBoolFlag } from "./args.js";
export async function handleWikiGenerate(args, flags) {
    // Repo id is optional: when omitted, auto-resolve from CWD (same rule the MCP
    // tools use). This lets `codesift wiki-generate` run from inside a repo with no
    // argument — and lets the postindex hook trigger regeneration knowing only the
    // repo root (it spawns this command with cwd set to the repo root).
    let repo = args[0] ?? "";
    if (!repo) {
        const { resolveRepoFromCwd } = await import("../server-helpers.js");
        repo = resolveRepoFromCwd(process.cwd());
    }
    const focus = getFlag(flags, "focus") ?? undefined;
    const outputDir = getFlag(flags, "output") ?? undefined;
    const noLens = getBoolFlag(flags, "no-lens");
    const v1Flag = getBoolFlag(flags, "v1");
    // --v1 flag maps to the CODESIFT_WIKI_V1 env var the orchestrator reads.
    // Preserve any pre-existing env value set by the shell.
    if (v1Flag)
        process.env.CODESIFT_WIKI_V1 = "1";
    const { generateWiki } = await import("../tools/wiki-tools.js");
    const wikiOptions = {};
    if (focus !== undefined)
        wikiOptions.focus = focus;
    if (outputDir !== undefined)
        wikiOptions.output_dir = outputDir;
    const result = await generateWiki(repo, wikiOptions);
    // Lens generation requires pre-assembled LensData from the wiki orchestrator.
    // When --no-lens is set, skip entirely. When lens is requested, it will be
    // wired up once the wiki orchestrator exposes buildLensData.
    if (!noLens) {
        // Lens data plumbing not yet available via generateWiki result;
        // no-op until wiki-tools exposes LensData in WikiResult.
        void (await import("../tools/lens-tools.js"));
    }
    const lines = [
        `Wiki generated: ${result.pages} pages`,
        `  Communities: ${result.communities}`,
        `  Hubs: ${result.hubs}`,
        `  Surprises: ${result.surprises}`,
        result.degraded ? "  \u26a0 Degraded: some analyses timed out" : "",
        `  Output: ${result.wiki_dir}`,
    ].filter(Boolean);
    process.stdout.write(lines.join("\n") + "\n");
}
export async function handleWikiLint(args, flags) {
    const wikiDir = args[0] ?? "";
    const currentHash = getFlag(flags, "current-hash") ?? undefined;
    const { lintWiki } = await import("../tools/wiki-lint.js");
    const result = await lintWiki(wikiDir, currentHash);
    if (result.issues.length === 0 && result.warnings.length === 0) {
        process.stdout.write("Wiki lint: no issues found\n");
    }
    else {
        for (const issue of result.issues) {
            process.stderr.write(`ERROR: ${issue.message}\n`);
        }
        for (const warning of result.warnings) {
            process.stderr.write(`WARN: ${warning.message}\n`);
        }
        process.exitCode = result.issues.length > 0 ? 1 : 0;
    }
}
//# sourceMappingURL=wiki-commands.js.map