// tools/code-graph-handler.ts — Handler for Code Graph integration tools (SPEC-376)
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { actionToProviderKey, buildProviderEntry, buildStatusResult, mergeProviderIntoConfig, } from '../engine/code-graph-configurator.js';
const CLAUDE_JSON = '.claude.json';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function readClaudeJson(projectPath) {
    try {
        const raw = await readFile(join(projectPath, CLAUDE_JSON), 'utf8');
        return JSON.parse(raw);
    }
    catch {
        return {};
    }
}
async function writeClaudeJson(projectPath, config) {
    await writeFile(join(projectPath, CLAUDE_JSON), JSON.stringify(config, null, 2) + '\n', 'utf8');
}
// ---------------------------------------------------------------------------
// configure_code_graph
// ---------------------------------------------------------------------------
export async function handleConfigureCodeGraph(input) {
    const { projectPath, action } = input;
    if (action === 'status') {
        return handleCodeGraphStatus({ projectPath });
    }
    const providerKey = actionToProviderKey(action);
    const entry = buildProviderEntry(providerKey);
    /* v8 ignore next 3 */
    if (!entry) {
        return { content: [{ type: 'text', text: `Unknown action: ${action}` }] };
    }
    const existing = await readClaudeJson(projectPath);
    const updated = mergeProviderIntoConfig(existing, providerKey, entry);
    await writeClaudeJson(projectPath, updated);
    const lines = [
        `Code Graph provider configured: ${providerKey}`,
        '',
        `Added to ${join(projectPath, CLAUDE_JSON)}:`,
        '```json',
        JSON.stringify({ [providerKey]: entry }, null, 2),
        '```',
        '',
        providerKey === 'codegraph'
            ? 'Initialize the project index with `npx -y @colbymchenry/codegraph init -i` from the project root before relying on graph queries.'
            : 'Initialize the provider index using the provider documentation before relying on graph queries.',
        '',
        'Restart Claude Code (or your MCP client) to activate the new server.',
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// code_graph_status
// ---------------------------------------------------------------------------
export async function handleCodeGraphStatus(input) {
    const config = await readClaudeJson(input.projectPath);
    const result = buildStatusResult(config);
    const lines = [
        'Code Graph Integration Status',
        '==============================',
        '',
        '| Provider | Configured |',
        '|----------|------------|',
        ...result.providers.map((p) => `| ${p.name} | ${p.configured ? 'yes' : 'no'} |`),
        '',
        `Active provider: ${result.activeProvider ?? 'none'}`,
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=code-graph-handler.js.map