"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodexMCPClient = exports.CodexMCPConfig = void 0;
const zod_1 = require("zod");
const node_child_process_1 = require("node:child_process");
// `codex` ships as `codex.cmd` when installed via npm on Windows; the
// stock `spawnSync` does not consult PATHEXT and ENOENTs on the bare
// name. `execSync` routes through `cmd.exe` and is fine.
const cross_platform_spawn_js_1 = require("../../../utils/cross-platform-spawn.js");
const MCPClient_1 = require("../MCPClient");
const defaults_1 = require("../defaults");
const analytics_1 = require("../../../utils/analytics");
exports.CodexMCPConfig = defaults_1.DefaultMCPClientConfig;
class CodexMCPClient extends MCPClient_1.DefaultMCPClient {
    name = 'Codex';
    constructor() {
        super();
    }
    isClientSupported() {
        try {
            (0, node_child_process_1.execSync)('codex --version', { stdio: 'ignore' });
            return Promise.resolve(true);
        }
        catch {
            return Promise.resolve(false);
        }
    }
    getConfigPath() {
        throw new Error('Not implemented');
    }
    isServerInstalled(local) {
        const serverName = local ? 'amplitude-local' : 'amplitude';
        try {
            const result = (0, cross_platform_spawn_js_1.spawnSync)('codex', ['mcp', 'list', '--json'], {
                encoding: 'utf-8',
            });
            if (result.error || result.status !== 0) {
                return Promise.resolve(false);
            }
            const stdout = result.stdout?.trim();
            if (!stdout) {
                return Promise.resolve(false);
            }
            const CodexServerListSchema = zod_1.z.array(zod_1.z.object({ name: zod_1.z.string() }).passthrough());
            const servers = CodexServerListSchema.parse(JSON.parse(stdout));
            return Promise.resolve(servers.some((server) => server.name === serverName));
        }
        catch {
            return Promise.resolve(false);
        }
    }
    addServer(apiKey, selectedFeatures, local, zone = 'us') {
        const serverName = local ? 'amplitude-local' : 'amplitude';
        const url = (0, defaults_1.buildMCPUrl)('streamable-http', selectedFeatures, local, zone);
        const args = ['mcp', 'add', serverName, '--url', url];
        const env = { ...process.env };
        if (apiKey) {
            env.AMPLITUDE_API_KEY = apiKey;
            args.push('--bearer-token-env-var', 'AMPLITUDE_API_KEY');
        }
        const result = (0, cross_platform_spawn_js_1.spawnSync)('codex', args, { stdio: 'ignore', env });
        if (result.error || result.status !== 0) {
            analytics_1.analytics.captureException(new Error('Failed to add server to Codex CLI.'));
            return Promise.resolve({ success: false });
        }
        return Promise.resolve({ success: true });
    }
    removeServer(local) {
        const serverName = local ? 'amplitude-local' : 'amplitude';
        const result = (0, cross_platform_spawn_js_1.spawnSync)('codex', ['mcp', 'remove', serverName], {
            stdio: 'ignore',
        });
        if (result.error || result.status !== 0) {
            analytics_1.analytics.captureException(new Error('Failed to remove server from Codex CLI.'));
            return Promise.resolve({ success: false });
        }
        return Promise.resolve({ success: true });
    }
}
exports.CodexMCPClient = CodexMCPClient;
exports.default = CodexMCPClient;
