import a from './index5.mjs';
import { g as getMcpServices, r as readMcpConfig } from './claude-config.mjs';
import { i18n } from './index2.mjs';
import { i as installMcpService, u as uninstallMcpService } from '../shared/ccjk.CwGZSTAK.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:child_process';
import 'node:process';
import './constants.mjs';
import 'node:os';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import '../shared/ccjk.DDL-4C-k.mjs';
import './ccjk-config.mjs';
import 'node:fs';
import '../shared/ccjk.BBtCGd_g.mjs';
import './index3.mjs';
import './fs-operations.mjs';
import 'node:crypto';
import 'node:fs/promises';
import './json-config.mjs';
import '../shared/ccjk.RyizuzOI.mjs';
import './platform.mjs';
import './main.mjs';
import 'module';
import 'node:path';
import 'node:stream';
import 'node:readline';
import 'node:url';
import './index6.mjs';
import 'stream';
import 'node:tty';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'buffer';
import 'string_decoder';
import './codex.mjs';
import './index8.mjs';
import '../shared/ccjk.DeWpAShp.mjs';
import '../shared/ccjk.CxpGa6MC.mjs';
import './config.mjs';
import '../shared/ccjk.BFQ7yr5S.mjs';
import './prompts.mjs';
import '../shared/ccjk.gDEDGD_t.mjs';
import '../shared/ccjk.DZ2LLOa-.mjs';

async function mcpInstallCli(options) {
  const lang = options.lang || i18n.language || "en";
  const isZh = lang === "zh-CN";
  if (!options.services || options.services.length === 0) {
    console.log(a.red(isZh ? "\u9519\u8BEF: \u5FC5\u987B\u6307\u5B9A\u670D\u52A1\u540D\u79F0" : "Error: Service names required"));
    console.log(a.dim(isZh ? "\u7528\u6CD5: ccjk mcp install <service1> [service2...]" : "Usage: ccjk mcp install <service1> [service2...]"));
    process.exit(1);
  }
  const availableServices = await getMcpServices();
  const invalidServices = options.services.filter(
    (s) => !availableServices.some((svc) => svc.id === s)
  );
  if (invalidServices.length > 0) {
    console.log(a.red(isZh ? `\u9519\u8BEF: \u672A\u77E5\u7684\u670D\u52A1: ${invalidServices.join(", ")}` : `Error: Unknown services: ${invalidServices.join(", ")}`));
    console.log(a.dim(isZh ? "\u63D0\u793A: \u4F7F\u7528 ccjk mcp list \u67E5\u770B\u53EF\u7528\u670D\u52A1" : "Tip: Use ccjk mcp list to see available services"));
    process.exit(1);
  }
  if (!options.yes) {
    const inquirer = await import('./index6.mjs').then(function (n) { return n.c; });
    const { confirm } = await inquirer.default.prompt([{
      type: "confirm",
      name: "confirm",
      message: isZh ? `\u786E\u8BA4\u5B89\u88C5 ${options.services.length} \u4E2A MCP \u670D\u52A1?` : `Install ${options.services.length} MCP service(s)?`,
      default: true
    }]);
    if (!confirm) {
      console.log(a.yellow(isZh ? "\u5DF2\u53D6\u6D88" : "Cancelled"));
      return;
    }
  }
  let successCount = 0;
  let failCount = 0;
  for (const serviceId of options.services) {
    const result = await installMcpService(serviceId, options.tool);
    if (result.success) {
      successCount++;
    } else {
      failCount++;
      console.log(a.red(isZh ? `\u2717 \u5B89\u88C5 ${serviceId} \u5931\u8D25: ${result.error || "\u672A\u77E5\u9519\u8BEF"}` : `\u2717 Failed to install ${serviceId}: ${result.error || "Unknown error"}`));
    }
  }
  if (successCount > 0) {
    console.log(a.green(isZh ? `\u2713 \u5DF2\u5B89\u88C5 ${successCount} \u4E2A\u670D\u52A1` : `\u2713 Installed ${successCount} service(s)`));
  }
  if (failCount > 0) {
    console.log(a.red(isZh ? `\u2717 ${failCount} \u4E2A\u670D\u52A1\u5B89\u88C5\u5931\u8D25` : `\u2717 ${failCount} service(s) failed`));
  }
  if (successCount > 0) {
    console.log(a.dim(isZh ? "\u63D0\u793A: \u91CD\u542F Claude Code \u4EE5\u5E94\u7528\u66F4\u6539" : "Tip: Restart Claude Code to apply changes"));
  }
}
async function mcpUninstallCli(options) {
  const lang = options.lang || i18n.language || "en";
  const isZh = lang === "zh-CN";
  if (!options.services || options.services.length === 0) {
    console.log(a.red(isZh ? "\u9519\u8BEF: \u5FC5\u987B\u6307\u5B9A\u670D\u52A1\u540D\u79F0" : "Error: Service names required"));
    console.log(a.dim(isZh ? "\u7528\u6CD5: ccjk mcp uninstall <service1> [service2...]" : "Usage: ccjk mcp uninstall <service1> [service2...]"));
    process.exit(1);
  }
  const config = readMcpConfig(options.tool);
  if (!config || !config.mcpServers) {
    console.log(a.yellow(isZh ? "\u672A\u5B89\u88C5\u4EFB\u4F55 MCP \u670D\u52A1" : "No MCP services installed"));
    return;
  }
  const notInstalled = options.services.filter((s) => !config.mcpServers[s]);
  if (notInstalled.length > 0) {
    console.log(a.yellow(isZh ? `\u8B66\u544A: \u4EE5\u4E0B\u670D\u52A1\u672A\u5B89\u88C5: ${notInstalled.join(", ")}` : `Warning: Not installed: ${notInstalled.join(", ")}`));
  }
  const toRemove = options.services.filter((s) => config.mcpServers[s]);
  if (toRemove.length === 0) {
    console.log(a.yellow(isZh ? "\u6CA1\u6709\u670D\u52A1\u9700\u8981\u5378\u8F7D" : "No services to uninstall"));
    return;
  }
  if (!options.yes) {
    const inquirer = await import('./index6.mjs').then(function (n) { return n.c; });
    const { confirm } = await inquirer.default.prompt([{
      type: "confirm",
      name: "confirm",
      message: isZh ? `\u786E\u8BA4\u5378\u8F7D ${toRemove.length} \u4E2A MCP \u670D\u52A1?` : `Uninstall ${toRemove.length} MCP service(s)?`,
      default: true
    }]);
    if (!confirm) {
      console.log(a.yellow(isZh ? "\u5DF2\u53D6\u6D88" : "Cancelled"));
      return;
    }
  }
  let successCount = 0;
  let failCount = 0;
  for (const serviceId of toRemove) {
    const result = await uninstallMcpService(serviceId, options.tool);
    if (result.success) {
      successCount++;
    } else {
      failCount++;
      console.log(a.red(isZh ? `\u2717 \u5378\u8F7D ${serviceId} \u5931\u8D25: ${result.error || "\u672A\u77E5\u9519\u8BEF"}` : `\u2717 Failed to uninstall ${serviceId}: ${result.error || "Unknown error"}`));
    }
  }
  if (successCount > 0) {
    console.log(a.green(isZh ? `\u2713 \u5DF2\u5378\u8F7D ${successCount} \u4E2A\u670D\u52A1` : `\u2713 Uninstalled ${successCount} service(s)`));
  }
  if (failCount > 0) {
    console.log(a.red(isZh ? `\u2717 ${failCount} \u4E2A\u670D\u52A1\u5378\u8F7D\u5931\u8D25` : `\u2717 ${failCount} service(s) failed`));
  }
  if (successCount > 0) {
    console.log(a.dim(isZh ? "\u63D0\u793A: \u91CD\u542F Claude Code \u4EE5\u5E94\u7528\u66F4\u6539" : "Tip: Restart Claude Code to apply changes"));
  }
}
async function mcpListCli(options = {}) {
  const lang = options.lang || i18n.language || "en";
  const isZh = lang === "zh-CN";
  const availableServices = await getMcpServices();
  const config = readMcpConfig(options.tool);
  const installedServices = Object.keys(config?.mcpServers || {});
  if (options.json) {
    const output = {
      installed: installedServices,
      available: availableServices.map((s) => ({
        id: s.id,
        name: s.name,
        description: s.description,
        installed: installedServices.includes(s.id)
      }))
    };
    console.log(JSON.stringify(output, null, 2));
    return;
  }
  console.log("");
  console.log(a.bold.cyan(isZh ? "\u{1F4E6} MCP \u670D\u52A1" : "\u{1F4E6} MCP Services"));
  console.log(a.dim("\u2500".repeat(50)));
  console.log("");
  if (options.installed) {
    if (installedServices.length === 0) {
      console.log(a.yellow(isZh ? "\u672A\u5B89\u88C5\u4EFB\u4F55\u670D\u52A1" : "No services installed"));
    } else {
      console.log(a.green(isZh ? `\u5DF2\u5B89\u88C5 ${installedServices.length} \u4E2A\u670D\u52A1:` : `${installedServices.length} service(s) installed:`));
      for (const serviceId of installedServices) {
        const service = availableServices.find((s) => s.id === serviceId);
        const name = service ? service.name : serviceId;
        console.log(`  ${a.green("\u25CF")} ${name} ${a.dim(`(${serviceId})`)}`);
      }
    }
  } else {
    console.log(a.dim(isZh ? "\u53EF\u7528\u670D\u52A1:" : "Available services:"));
    for (const service of availableServices) {
      const installed = installedServices.includes(service.id);
      const icon = installed ? a.green("\u2713") : a.dim("\u25CB");
      const name = service.name;
      const desc = service.description;
      console.log(`  ${icon} ${a.bold(name)} ${a.dim(`(${service.id})`)}`);
      console.log(`    ${a.dim(desc)}`);
    }
  }
  console.log("");
  console.log(a.dim(isZh ? "\u63D0\u793A: \u4F7F\u7528 ccjk mcp install <service> \u5B89\u88C5\u670D\u52A1" : "Tip: Use ccjk mcp install <service> to install a service"));
}

export { mcpInstallCli, mcpListCli, mcpUninstallCli };
