import a from './index5.mjs';
import { i18n, resolveSupportedLanguage, format } from './index2.mjs';
import { STATUS, COLORS as theme } from './banner.mjs';
import { r as runConfigWizard, t as testApiConnection, d as displayCurrentStatus, q as quickSetup } from '../shared/ccjk.B2f-cwUP.mjs';
import { g as getAllPresets } from '../shared/ccjk.DopKzo3z.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:fs';
import 'node:process';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import '../shared/ccjk.gDEDGD_t.mjs';
import './index6.mjs';
import 'node:readline';
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 'node:path';
import 'node:os';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import './fs-operations.mjs';
import 'node:fs/promises';

function resolveApiCommandLanguage(lang) {
  return resolveSupportedLanguage(lang || i18n.language);
}
function listProviders(lang = "en") {
  const presets = getAllPresets();
  console.log("");
  console.log(theme.primary("\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557"));
  console.log(theme.primary("\u2551") + theme.accent(`                    ${i18n.t("api:providersTitle")}                    `.slice(0, 60)) + theme.primary("\u2551"));
  console.log(theme.primary("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"));
  console.log("");
  const categories = {
    "official": i18n.t("api:categoryOfficial"),
    "openai-compatible": i18n.t("api:categoryOpenaiCompatible"),
    "chinese": i18n.t("api:categoryChinese"),
    "free": i18n.t("api:categoryFree"),
    "local": i18n.t("api:categoryLocal")
  };
  for (const [category, label] of Object.entries(categories)) {
    const categoryPresets = presets.filter((p) => p.category === category);
    if (categoryPresets.length === 0)
      continue;
    console.log(theme.secondary(`  ${label}:`));
    for (const preset of categoryPresets) {
      const name = lang === "zh-CN" ? preset.nameZh : preset.name;
      const desc = lang === "zh-CN" ? preset.descriptionZh : preset.description;
      const keyIcon = preset.requiresApiKey ? "\u{1F511}" : "\u{1F193}";
      console.log(`    ${keyIcon} ${a.green(preset.id.padEnd(15))} ${name}`);
      console.log(a.gray(`       ${desc}`));
    }
    console.log("");
  }
}
function setupApi(providerId, apiKey, _lang = "en") {
  const result = quickSetup(providerId, apiKey);
  if (result.success) {
    console.log(STATUS.success(format(i18n.t("api:configSuccess"), { provider: result.provider || providerId })));
  } else {
    console.log(STATUS.error(format(i18n.t("api:configFailed"), { error: result.error || "Unknown error" })));
  }
}
function showStatus(lang) {
  displayCurrentStatus(resolveApiCommandLanguage(lang));
}
async function testApi(lang) {
  await testApiConnection(resolveApiCommandLanguage(lang));
}
async function runWizard(lang) {
  await runConfigWizard(resolveApiCommandLanguage(lang));
}
async function apiCommand(action = "wizard", args = [], options = {}) {
  const lang = resolveApiCommandLanguage(options.lang);
  switch (action) {
    case "list":
    case "ls":
    case "providers":
      listProviders(lang);
      break;
    case "setup":
    case "set":
      if (options.provider && options.key) {
        setupApi(options.provider, options.key, lang);
      } else if (args.length >= 2) {
        setupApi(args[0], args[1], lang);
      } else {
        console.log(STATUS.error(i18n.t("api:setupUsage")));
        console.log(a.gray(`  ${i18n.t("api:setupExample")}`));
      }
      break;
    case "status":
    case "s":
      showStatus(lang);
      break;
    case "test":
    case "t":
      await testApi(lang);
      break;
    case "wizard":
    case "w":
    case "config":
    default:
      await runWizard(lang);
      break;
  }
}

export { apiCommand, apiCommand as default, listProviders, runWizard, setupApi, showStatus, testApi };
