import a from './index5.mjs';
import { i as inquirer } from './index6.mjs';
import { isCodeToolType, DEFAULT_CODE_TOOL_TYPE } from './constants.mjs';
import { i18n } from './index2.mjs';
import { readZcfConfig } from './ccjk-config.mjs';
import { C as ClaudeCodeConfigManager } from './clavue-config.mjs';
import { handleCustomApiMode } from './features.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:readline';
import 'stream';
import 'node:tty';
import 'node:process';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'node:path';
import 'node:os';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import 'node:fs';
import 'node:url';
import '../shared/ccjk.BBtCGd_g.mjs';
import './index3.mjs';
import './fs-operations.mjs';
import 'node:fs/promises';
import './json-config.mjs';
import '../shared/ccjk.RyizuzOI.mjs';
import '../shared/ccjk.DDL-4C-k.mjs';
import './config.mjs';
import './claude-config.mjs';
import 'node:child_process';
import './platform.mjs';
import './main.mjs';
import 'module';
import 'node:stream';
import '../shared/ccjk.L7yC58_i.mjs';
import '../shared/ccjk.C0WLUnFV.mjs';
import '../shared/ccjk.BFQ7yr5S.mjs';
import '../shared/ccjk.DZ2LLOa-.mjs';
import '../shared/ccjk.DeWpAShp.mjs';
import '../shared/ccjk.DhJ1kyDR.mjs';
import './simple-config.mjs';

async function syncClavueProviderStateIfNeeded(codeTool, activeProfileId) {
  if (codeTool !== "clavue") {
    return;
  }
  const { syncMyclaudeProviderProfilesFromClaudeConfig } = await import('./clavue-config.mjs').then(function (n) { return n.g; });
  const config = ClaudeCodeConfigManager.readConfig();
  syncMyclaudeProviderProfilesFromClaudeConfig(
    activeProfileId === void 0 || !config ? config : { ...config, currentProfileId: activeProfileId }
  );
}
function getCurrentCodeTool() {
  const config = readZcfConfig();
  if (config?.codeToolType && isCodeToolType(config.codeToolType)) {
    return config.codeToolType;
  }
  return DEFAULT_CODE_TOOL_TYPE;
}
async function showApiConfigMenu(title, options) {
  const lang = i18n.language;
  const isZh = lang === "zh-CN";
  console.log("");
  console.log(a.bold.cyan(title || (isZh ? "\u{1F511} API \u914D\u7F6E\u7BA1\u7406" : "\u{1F511} API Configuration")));
  console.log("");
  const choices = [
    { name: isZh ? "\u4F7F\u7528\u5B98\u65B9\u767B\u5F55" : "Use Official Login", value: "official" },
    { name: isZh ? "\u81EA\u5B9A\u4E49 API \u914D\u7F6E" : "Custom API Configuration", value: "custom" },
    { name: isZh ? "\u4F7F\u7528 CCR \u4EE3\u7406" : "Use CCR Proxy", value: "ccr" },
    { name: isZh ? "\u8DF3\u8FC7\uFF08\u7A0D\u540E\u624B\u52A8\u914D\u7F6E\uFF09" : "Skip (Configure Later)", value: "skip" }
  ];
  const { choice } = await inquirer.prompt({
    type: "list",
    name: "choice",
    message: isZh ? "\u8BF7\u9009\u62E9 API \u914D\u7F6E\u6A21\u5F0F:" : "Select API configuration mode:",
    choices,
    default: "custom",
    pageSize: 8
  });
  const codeTool = getCurrentCodeTool();
  switch (choice) {
    case "official":
      return await handleOfficialLogin(codeTool, isZh);
    case "custom":
      return await handleCustomConfig(isZh, options?.context);
    case "ccr":
      return await handleCcrProxy(codeTool, isZh);
    case "skip":
      return { mode: "skip", success: true, cancelled: false };
    default:
      return { mode: "skip", success: true, cancelled: false };
  }
}
async function handleOfficialLogin(codeTool, isZh) {
  if (codeTool === "claude-code" || codeTool === "clavue") {
    const result = await ClaudeCodeConfigManager.switchToOfficial();
    if (result.success) {
      await syncClavueProviderStateIfNeeded(codeTool, "");
      console.log("");
      console.log(a.green(isZh ? "\u2705 \u5DF2\u5207\u6362\u5230\u5B98\u65B9\u767B\u5F55" : "\u2705 Switched to official login"));
      console.log("");
      return { mode: "official", success: true, cancelled: false };
    } else {
      console.log("");
      console.log(a.red(isZh ? `\u274C \u5207\u6362\u5931\u8D25: ${result.error}` : `\u274C Failed to switch: ${result.error}`));
      console.log("");
      return { mode: "official", success: false, cancelled: false };
    }
  } else {
    console.log("");
    console.log(a.yellow(isZh ? "\u26A0\uFE0F \u5F53\u524D\u4EE3\u7801\u5DE5\u5177\u4E0D\u652F\u6301\u6B64\u529F\u80FD" : "\u26A0\uFE0F Current code tool does not support this feature"));
    console.log("");
    return { mode: "official", success: false, cancelled: false };
  }
}
async function handleCustomConfig(_isZh, context) {
  try {
    const codeTool = getCurrentCodeTool();
    if ((codeTool === "claude-code" || codeTool === "clavue") && context === "init") {
      const { addProfileDirect } = await import('./claude-code-incremental-manager.mjs');
      await addProfileDirect();
    } else {
      await handleCustomApiMode();
    }
    return { mode: "custom", success: true, cancelled: false };
  } catch {
    return { mode: "custom", success: false, cancelled: false };
  }
}
async function handleCcrProxy(codeTool, isZh) {
  if (codeTool === "claude-code" || codeTool === "clavue") {
    const result = await ClaudeCodeConfigManager.switchToCcr();
    if (result.success) {
      await syncClavueProviderStateIfNeeded(codeTool, "ccr-proxy");
      console.log("");
      console.log(a.green(isZh ? "\u2705 \u5DF2\u5207\u6362\u5230 CCR \u4EE3\u7406" : "\u2705 Switched to CCR proxy"));
      console.log("");
      return { mode: "ccr", success: true, cancelled: false };
    } else {
      console.log("");
      console.log(a.red(isZh ? `\u274C \u5207\u6362\u5931\u8D25: ${result.error}` : `\u274C Failed to switch: ${result.error}`));
      console.log("");
      return { mode: "ccr", success: false, cancelled: false };
    }
  } else {
    console.log("");
    console.log(a.yellow(isZh ? "\u26A0\uFE0F \u5F53\u524D\u4EE3\u7801\u5DE5\u5177\u4E0D\u652F\u6301\u6B64\u529F\u80FD" : "\u26A0\uFE0F Current code tool does not support this feature"));
    console.log("");
    return { mode: "ccr", success: false, cancelled: false };
  }
}

export { showApiConfigMenu };
