import fs from 'node:fs';
import process from 'node:process';
import readline from 'node:readline';
import { usageError } from "../infrastructure.js";
import { openGraph } from "../shared.js";
import { handleMcpMessage, mcpParseError, } from "./mcp/McpProtocol.js";
function readPackageVersion() {
    const packageUrl = new URL('../../../package.json', import.meta.url);
    const packageText = fs.readFileSync(packageUrl, 'utf8');
    const packageJson = JSON.parse(packageText);
    return typeof packageJson.version === 'string' && packageJson.version.length > 0
        ? packageJson.version
        : '0.0.0';
}
function writeResponse(response) {
    process.stdout.write(`${JSON.stringify(response)}\n`);
}
/** Handles `git warp mcp`: starts a local stdio MCP server. */
export default async function handleMcp({ options, args, }) {
    if (args.length > 0) {
        throw usageError('mcp does not accept positional args; use --repo, --graph, and --writer');
    }
    const { graph } = await openGraph(options);
    const serverVersion = readPackageVersion();
    const lines = readline.createInterface({
        input: process.stdin,
        terminal: false,
    });
    lines.on('line', (line) => {
        void dispatchLine(graph, serverVersion, line);
    });
    return {
        payload: undefined,
        close: () => {
            lines.close();
            return Promise.resolve();
        },
    };
}
async function dispatchLine(graph, serverVersion, line) {
    if (line.trim().length === 0) {
        return;
    }
    try {
        const parsed = JSON.parse(line);
        const response = await handleMcpMessage(graph, parsed, { serverVersion });
        if (response !== null) {
            writeResponse(response);
        }
    }
    catch {
        writeResponse(mcpParseError());
    }
}
