"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeMCPServer = exports.addMCPServer = exports.getInstalledClients = exports.removeMCPServerFromClientsStep = exports.addMCPServerToClientsStep = exports.getSupportedClients = void 0;
const telemetry_1 = require("../../telemetry");
const analytics_1 = require("../../utils/analytics");
const ui_1 = require("../../ui");
const cursor_1 = require("./clients/cursor");
const claude_1 = require("./clients/claude");
const claude_code_1 = require("./clients/claude-code");
const visual_studio_code_1 = require("./clients/visual-studio-code");
const zed_1 = require("./clients/zed");
const codex_1 = require("./clients/codex");
const cline_1 = require("./clients/cline");
const windsurf_1 = require("./clients/windsurf");
const gemini_cli_1 = require("./clients/gemini-cli");
const amp_1 = require("./clients/amp");
const opencode_1 = require("./clients/opencode");
const claude_code_plugin_1 = require("./clients/claude-code-plugin");
const defaults_1 = require("./defaults");
const debug_1 = require("../../utils/debug");
/**
 * Deprecated clients — uninstall-only. Drop in next release.
 *
 * These clients are no longer offered for install, but the wizard still
 * needs to be able to scrub stale `mcpServers.amplitude` entries that
 * previous wizard versions wrote to their settings files. Their
 * `addServer` is a no-op; only `removeServer` does real work.
 */
const getDeprecatedUninstallOnlyClients = () => [
    new cline_1.ClineMCPClient(),
    new windsurf_1.WindsurfMCPClient(),
    new gemini_cli_1.GeminiCLIMCPClient(),
    new amp_1.AmpMCPClient(),
    new opencode_1.OpenCodeMCPClient(),
    new claude_code_plugin_1.ClaudeCodePluginClient(),
];
const getSupportedClients = async () => {
    const allClients = [
        new cursor_1.CursorMCPClient(),
        new claude_1.ClaudeMCPClient(),
        new claude_code_1.ClaudeCodeMCPClient(),
        new visual_studio_code_1.VisualStudioCodeClient(),
        new zed_1.ZedClient(),
        new codex_1.CodexMCPClient(),
    ];
    (0, debug_1.debug)('Checking for supported MCP clients...');
    // Run support checks in parallel — Claude Code + Codex shell out via
    // spawnSync/execSync, so doing them sequentially doubles wall-clock time.
    const supportFlags = await Promise.all(allClients.map(async (client) => {
        const isSupported = await client.isClientSupported();
        (0, debug_1.debug)(`${client.name}: ${isSupported ? '✓ supported' : '✗ not supported'}`);
        return isSupported;
    }));
    const supportedClients = allClients.filter((_, i) => supportFlags[i]);
    (0, debug_1.debug)(`Found ${supportedClients.length} supported client(s): ${supportedClients
        .map((c) => c.name)
        .join(', ')}`);
    return supportedClients;
};
exports.getSupportedClients = getSupportedClients;
/**
 * Add MCP server to clients. No prompts — pure orchestration.
 * Prompts are handled by McpScreen (TUI) or auto-accepted (CI).
 */
const addMCPServerToClientsStep = async ({ integration, local = false, ci = false, zone = 'us', }) => {
    const ui = (0, ui_1.getUI)();
    // CI mode: skip MCP installation entirely
    if (ci) {
        ui.log.info('Skipping MCP installation (CI mode)');
        return [];
    }
    const supportedClients = await (0, exports.getSupportedClients)();
    if (supportedClients.length === 0) {
        ui.log.info('No supported MCP clients detected. Skipping MCP installation.');
        return [];
    }
    // Auto-install to all supported clients
    await (0, telemetry_1.traceStep)('adding mcp servers', async () => {
        await (0, exports.addMCPServer)(supportedClients, undefined, [...defaults_1.ALL_FEATURE_VALUES], local, zone);
    });
    ui.log.success(`Added the MCP server to:
  ${supportedClients.map((c) => `- ${c.name}`).join('\n  ')} `);
    analytics_1.analytics.wizardCapture('MCP Servers Added', {
        clients: supportedClients.map((c) => c.name),
        integration,
    });
    return supportedClients.map((c) => c.name);
};
exports.addMCPServerToClientsStep = addMCPServerToClientsStep;
const removeMCPServerFromClientsStep = async ({ integration, local = false, }) => {
    const installedClients = await (0, exports.getInstalledClients)(local);
    if (installedClients.length === 0) {
        analytics_1.analytics.wizardCapture('MCP No Servers To Remove', {
            integration,
        });
        return [];
    }
    // Auto-remove from all installed clients
    const results = await (0, telemetry_1.traceStep)('removing mcp servers', async () => {
        await (0, exports.removeMCPServer)(installedClients, local);
        return installedClients.map((c) => c.name);
    });
    analytics_1.analytics.wizardCapture('MCP Servers Removed', {
        clients: results,
        integration,
    });
    return results;
};
exports.removeMCPServerFromClientsStep = removeMCPServerFromClientsStep;
const getInstalledClients = async (local) => {
    // Include deprecated uninstall-only clients here so the uninstall flow
    // can scrub stale config entries left over from previous wizard versions.
    // These clients aren't returned by getSupportedClients() (no install path),
    // but their isServerInstalled() probe still detects existing entries.
    const clients = [
        ...(await (0, exports.getSupportedClients)()),
        ...getDeprecatedUninstallOnlyClients(),
    ];
    const installedClients = [];
    for (const client of clients) {
        if (await client.isServerInstalled(local)) {
            installedClients.push(client);
        }
    }
    return installedClients;
};
exports.getInstalledClients = getInstalledClients;
const addMCPServer = async (clients, personalApiKey, selectedFeatures, local, zone = 'us') => {
    for (const client of clients) {
        await client.addServer(personalApiKey, selectedFeatures, local, zone);
    }
};
exports.addMCPServer = addMCPServer;
const removeMCPServer = async (clients, local) => {
    for (const client of clients) {
        await client.removeServer(local);
    }
};
exports.removeMCPServer = removeMCPServer;
