import { exec } from 'node:child_process';
import { existsSync, copyFileSync, mkdirSync } from 'node:fs';
import { homedir } from 'node:os';
import process__default from 'node:process';
import { promisify } from 'node:util';
import a from './index5.mjs';
import { d as dayjs } from '../shared/ccjk.RyizuzOI.mjs';
import { i as inquirer } from './index6.mjs';
import { ensureI18nInitialized, i18n } from './index2.mjs';
import { h as setPrimaryApiKey, i as addCompletedOnboarding } from './claude-config.mjs';
import { r as resolveClaudeFamilySettingsTarget, n as normalizeClaudeFamilySettings } from '../shared/ccjk.DDL-4C-k.mjs';
import { h as clearLegacyTopLevelRuntimeSettings, b as backupExistingConfig } from './config.mjs';
import { readJsonConfig, writeJsonConfig } from './json-config.mjs';
import { p as promptBoolean } from '../shared/ccjk.DZ2LLOa-.mjs';
import { j as join } from '../shared/ccjk.bQ7Dh1g4.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:readline';
import 'stream';
import 'node:tty';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'tty';
import 'fs';
import 'child_process';
import 'node:path';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import 'node:url';
import './constants.mjs';
import './platform.mjs';
import './main.mjs';
import 'module';
import 'node:stream';
import './ccjk-config.mjs';
import '../shared/ccjk.BBtCGd_g.mjs';
import './index3.mjs';
import './fs-operations.mjs';
import 'node:fs/promises';
import '../shared/ccjk.DeWpAShp.mjs';

const PROVIDER_PRESETS_URL = "https://pub-0dc3e1677e894f07bbea11b17a29e032.r2.dev/providers.json";
async function fetchProviderPresets() {
  try {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), 5e3);
    const response = await fetch(PROVIDER_PRESETS_URL, {
      signal: controller.signal
    });
    clearTimeout(timeoutId);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    const presets = [];
    if (Array.isArray(data)) {
      for (const provider of data) {
        if (provider && typeof provider === "object") {
          presets.push({
            name: provider.name || "",
            provider: provider.name || "",
            baseURL: provider.api_base_url || provider.baseURL || provider.url,
            requiresApiKey: provider.api_key === "" || provider.requiresApiKey !== false,
            models: provider.models || [],
            description: provider.description || provider.name || "",
            transformer: provider.transformer
          });
        }
      }
    } else if (data && typeof data === "object") {
      for (const [key, value] of Object.entries(data)) {
        if (typeof value === "object" && value !== null) {
          const provider = value;
          presets.push({
            name: provider.name || key,
            provider: key,
            baseURL: provider.api_base_url || provider.baseURL || provider.url,
            requiresApiKey: provider.api_key === "" || provider.requiresApiKey !== false,
            models: provider.models || [],
            description: provider.description || "",
            transformer: provider.transformer
          });
        }
      }
    }
    return presets;
  } catch {
    return getFallbackPresets();
  }
}
function getFallbackPresets() {
  return [
    {
      name: "dashscope",
      provider: "dashscope",
      baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
      requiresApiKey: true,
      models: ["qwen3-coder-plus"],
      description: "Alibaba DashScope",
      transformer: {
        "use": [["maxtoken", { max_tokens: 65536 }]],
        "qwen3-coder-plus": {
          use: ["enhancetool"]
        }
      }
    },
    {
      name: "deepseek",
      provider: "deepseek",
      baseURL: "https://api.deepseek.com/chat/completions",
      requiresApiKey: true,
      models: ["deepseek-chat", "deepseek-reasoner"],
      description: "DeepSeek AI models",
      transformer: {
        "use": ["deepseek"],
        "deepseek-chat": {
          use: ["tooluse"]
        }
      }
    },
    {
      name: "gemini",
      provider: "gemini",
      baseURL: "https://generativelanguage.googleapis.com/v1beta/models/",
      requiresApiKey: true,
      models: ["gemini-2.5-flash", "gemini-2.5-pro"],
      description: "Google Gemini models",
      transformer: {
        use: ["gemini"]
      }
    },
    {
      name: "modelscope",
      provider: "modelscope",
      baseURL: "https://api-inference.modelscope.cn/v1/chat/completions",
      requiresApiKey: true,
      models: ["Qwen/Qwen3-Coder-480B-A35B-Instruct", "Qwen/Qwen3-235B-A22B-Thinking-2507", "ZhipuAI/GLM-4.5"],
      description: "ModelScope AI models",
      transformer: {
        "use": [["maxtoken", { max_tokens: 65536 }]],
        "Qwen/Qwen3-Coder-480B-A35B-Instruct": {
          use: ["enhancetool"]
        },
        "Qwen/Qwen3-235B-A22B-Thinking-2507": {
          use: ["reasoning"]
        }
      }
    },
    {
      name: "openrouter",
      provider: "openrouter",
      baseURL: "https://openrouter.ai/api/v1/chat/completions",
      requiresApiKey: true,
      models: [
        "google/gemini-2.5-pro-preview",
        "anthropic/claude-sonnet-4",
        "anthropic/claude-3.5-sonnet",
        "anthropic/claude-3.7-sonnet:thinking"
      ],
      description: "OpenRouter API",
      transformer: {
        use: ["openrouter"]
      }
    },
    {
      name: "siliconflow",
      provider: "siliconflow",
      baseURL: "https://api.siliconflow.cn/v1/chat/completions",
      requiresApiKey: true,
      models: ["moonshotai/Kimi-K2-Instruct"],
      description: "SiliconFlow AI",
      transformer: {
        use: [["maxtoken", { max_tokens: 16384 }]]
      }
    },
    {
      name: "volcengine",
      provider: "volcengine",
      baseURL: "https://ark.cn-beijing.volces.com/api/v3/chat/completions",
      requiresApiKey: true,
      models: ["deepseek-v3-250324", "deepseek-r1-250528"],
      description: "Volcengine AI",
      transformer: {
        use: ["deepseek"]
      }
    }
  ];
}

const execAsync = promisify(exec);
const CCR_CONFIG_DIR = join(homedir(), ".claude-code-router");
const CCR_CONFIG_FILE = join(CCR_CONFIG_DIR, "config.json");
const CCR_BACKUP_DIR = CCR_CONFIG_DIR;
function ensureCcrConfigDir() {
  if (!existsSync(CCR_CONFIG_DIR)) {
    mkdirSync(CCR_CONFIG_DIR, { recursive: true });
  }
}
async function backupCcrConfig() {
  ensureI18nInitialized();
  try {
    if (!existsSync(CCR_CONFIG_FILE)) {
      return null;
    }
    const timestamp = `${dayjs().format("YYYY-MM-DDTHH-mm-ss-SSS")}Z`;
    const backupFileName = `config.json.${timestamp}.bak`;
    const backupPath = join(CCR_BACKUP_DIR, backupFileName);
    console.log(a.green(`${i18n.t("ccr:backupCcrConfig")}`));
    copyFileSync(CCR_CONFIG_FILE, backupPath);
    console.log(a.green(`\u2714 ${i18n.t("ccr:ccrBackupSuccess").replace("{path}", backupPath)}`));
    return backupPath;
  } catch (error) {
    console.error(a.red(`${i18n.t("ccr:ccrBackupFailed")}:`), error.message);
    return null;
  }
}
function readCcrConfig() {
  if (!existsSync(CCR_CONFIG_FILE)) {
    return null;
  }
  return readJsonConfig(CCR_CONFIG_FILE);
}
function writeCcrConfig(config) {
  ensureCcrConfigDir();
  writeJsonConfig(CCR_CONFIG_FILE, config);
}
async function configureCcrProxy(ccrConfig, codeTool) {
  const target = resolveClaudeFamilySettingsTarget(codeTool);
  const settings = readJsonConfig(target.settingsFile) || {};
  const host = ccrConfig.HOST || "127.0.0.1";
  const port = ccrConfig.PORT || 3456;
  const apiKey = ccrConfig.APIKEY || "sk-ccjk-x-ccr";
  if (!settings.env) {
    settings.env = {};
  }
  clearLegacyTopLevelRuntimeSettings(settings);
  delete settings.env.ANTHROPIC_AUTH_TOKEN;
  settings.env.ANTHROPIC_BASE_URL = `http://${host}:${port}`;
  settings.env.ANTHROPIC_API_KEY = apiKey;
  normalizeClaudeFamilySettings(settings);
  writeJsonConfig(target.settingsFile, settings);
  try {
    setPrimaryApiKey(target.codeTool);
  } catch (error) {
    ensureI18nInitialized();
    console.error(i18n.t("mcp:primaryApiKeySetFailed"), error);
  }
}
async function selectCcrPreset() {
  ensureI18nInitialized();
  console.log(a.green(`${i18n.t("ccr:fetchingPresets")}`));
  const presets = await fetchProviderPresets();
  if (!presets || presets.length === 0) {
    console.log(a.yellow(`${i18n.t("ccr:noPresetsAvailable")}`));
    return null;
  }
  try {
    const choices = [
      {
        name: `1. ${i18n.t("ccr:skipOption")}`,
        value: "skip"
      },
      ...presets.map((p, index) => ({
        name: `${index + 2}. ${p.name}`,
        value: p
      }))
    ];
    const { preset } = await inquirer.prompt({
      type: "list",
      name: "preset",
      message: i18n.t("ccr:selectCcrPreset"),
      choices
    });
    return preset;
  } catch (error) {
    if (error.name === "ExitPromptError") {
      console.log(a.yellow(i18n.t("common:cancelled")));
      return null;
    }
    throw error;
  }
}
async function configureCcrWithPreset(preset) {
  ensureI18nInitialized();
  const provider = {
    name: preset.name,
    // Use the original name from JSON
    api_base_url: preset.baseURL || "",
    api_key: "",
    models: preset.models
  };
  if (preset.transformer) {
    provider.transformer = preset.transformer;
  }
  if (preset.requiresApiKey) {
    try {
      const { apiKey } = await inquirer.prompt({
        type: "input",
        name: "apiKey",
        message: i18n.t("ccr:enterApiKeyForProvider").replace("{provider}", preset.name),
        validate: async (value) => !!value || i18n.t("api:keyRequired")
      });
      provider.api_key = apiKey;
    } catch (error) {
      if (error.name === "ExitPromptError") {
        throw error;
      }
      throw error;
    }
  } else {
    provider.api_key = "sk-free";
  }
  let defaultModel = preset.models[0];
  if (preset.models.length > 1) {
    try {
      const { model } = await inquirer.prompt({
        type: "list",
        name: "model",
        message: i18n.t("ccr:selectDefaultModelForProvider").replace("{provider}", preset.name),
        choices: preset.models.map((m, index) => ({
          name: `${index + 1}. ${m}`,
          value: m
        }))
      });
      defaultModel = model;
    } catch (error) {
      if (error.name === "ExitPromptError") {
        throw error;
      }
      throw error;
    }
  }
  const router = {
    default: `${preset.name},${defaultModel}`,
    // Use the original name
    background: `${preset.name},${defaultModel}`,
    think: `${preset.name},${defaultModel}`,
    longContext: `${preset.name},${defaultModel}`,
    longContextThreshold: 6e4,
    webSearch: `${preset.name},${defaultModel}`
  };
  const config = {
    LOG: true,
    CLAUDE_PATH: "",
    HOST: "127.0.0.1",
    PORT: 3456,
    APIKEY: "sk-ccjk-x-ccr",
    API_TIMEOUT_MS: "600000",
    PROXY_URL: "",
    transformers: [],
    Providers: [provider],
    Router: router
  };
  return config;
}
async function restartAndCheckCcrStatus() {
  ensureI18nInitialized();
  try {
    console.log(a.green(`${i18n.t("ccr:restartingCcr")}`));
    await execAsync("ccr restart");
    console.log(a.green(`\u2714 ${i18n.t("ccr:ccrRestartSuccess")}`));
    console.log(a.green(`${i18n.t("ccr:checkingCcrStatus")}`));
    const { stdout } = await execAsync("ccr status");
    console.log(a.gray(stdout));
  } catch (error) {
    console.error(a.red(`${i18n.t("ccr:ccrRestartFailed")}:`), error.message || error);
    if (process__default.env.DEBUG) {
      console.error("Full error:", error);
    }
  }
}
async function showConfigurationTips(apiKey) {
  ensureI18nInitialized();
  console.log(a.bold.cyan(`
\u{1F4CC} ${i18n.t("ccr:configTips")}:`));
  console.log(a.green(`  \u2022 ${i18n.t("ccr:advancedConfigTip")}`));
  console.log(a.green(`  \u2022 ${i18n.t("ccr:manualConfigTip")}`));
  console.log(a.bold.yellow(`  \u2022 ${i18n.t("ccr:useClaudeCommand")}`));
  if (apiKey) {
    console.log(a.bold.green(`  \u2022 ${i18n.t("ccr:ccrUiApiKey") || "CCR UI API Key"}: ${apiKey}`));
    console.log(a.gray(`    ${i18n.t("ccr:ccrUiApiKeyHint") || "Use this API key to login to CCR UI"}`));
  }
  console.log("");
}
function createDefaultCcrConfig() {
  return {
    LOG: false,
    CLAUDE_PATH: "",
    HOST: "127.0.0.1",
    PORT: 3456,
    APIKEY: "sk-ccjk-x-ccr",
    API_TIMEOUT_MS: "600000",
    PROXY_URL: "",
    transformers: [],
    Providers: [],
    // Empty providers array - user configures in UI
    Router: {}
    // Empty router configuration - user configures in UI
  };
}
async function setupCcrConfiguration(codeTool) {
  ensureI18nInitialized();
  try {
    const existingConfig = readCcrConfig();
    if (existingConfig) {
      console.log(a.green(`\u2139 ${i18n.t("ccr:existingCcrConfig")}`));
      let shouldBackupAndReconfigure = false;
      try {
        shouldBackupAndReconfigure = await promptBoolean({
          message: i18n.t("ccr:overwriteCcrConfig"),
          defaultValue: false
        });
      } catch (error) {
        if (error.name === "ExitPromptError") {
          console.log(a.yellow(i18n.t("common:cancelled")));
          return false;
        }
        throw error;
      }
      if (!shouldBackupAndReconfigure) {
        console.log(a.yellow(`${i18n.t("ccr:keepingExistingConfig")}`));
        await configureCcrProxy(existingConfig, codeTool);
        try {
          const { manageApiKeyApproval } = await import('./claude-config.mjs').then(function (n) { return n.n; });
          const apiKey = existingConfig.APIKEY || "sk-ccjk-x-ccr";
          manageApiKeyApproval(apiKey, resolveClaudeFamilySettingsTarget(codeTool).codeTool);
          console.log(a.green(`\u2714 ${i18n.t("ccr:apiKeyApprovalSuccess")}`));
        } catch (error) {
          console.error(a.red(`${i18n.t("ccr:apiKeyApprovalFailed")}:`), error);
        }
        return true;
      }
      backupCcrConfig();
    }
    const preset = await selectCcrPreset();
    if (!preset) {
      return false;
    }
    let config;
    if (preset === "skip") {
      console.log(a.yellow(`${i18n.t("ccr:skipConfiguring")}`));
      config = createDefaultCcrConfig();
    } else {
      config = await configureCcrWithPreset(preset);
    }
    writeCcrConfig(config);
    console.log(a.green(`\u2714 ${i18n.t("ccr:ccrConfigSuccess")}`));
    await configureCcrProxy(config, codeTool);
    console.log(a.green(`\u2714 ${i18n.t("ccr:proxyConfigSuccess")}`));
    await restartAndCheckCcrStatus();
    await showConfigurationTips(config.APIKEY);
    try {
      addCompletedOnboarding(resolveClaudeFamilySettingsTarget(codeTool).codeTool);
    } catch (error) {
      console.error(a.red(i18n.t("errors:failedToSetOnboarding")), error);
    }
    try {
      const { manageApiKeyApproval } = await import('./claude-config.mjs').then(function (n) { return n.n; });
      const apiKey = config.APIKEY || "sk-ccjk-x-ccr";
      manageApiKeyApproval(apiKey, resolveClaudeFamilySettingsTarget(codeTool).codeTool);
      console.log(a.green(`\u2714 ${i18n.t("ccr:apiKeyApprovalSuccess")}`));
    } catch (error) {
      console.error(a.red(`${i18n.t("ccr:apiKeyApprovalFailed")}:`), error);
    }
    return true;
  } catch (error) {
    if (error.name === "ExitPromptError") {
      console.log(a.yellow(i18n.t("common:cancelled")));
      return false;
    }
    console.error(a.red(`${i18n.t("ccr:ccrConfigFailed")}:`), error);
    return false;
  }
}
async function configureCcrFeature() {
  ensureI18nInitialized();
  const backupDir = backupExistingConfig();
  if (backupDir) {
    console.log(a.gray(`\u2714 ${i18n.t("configuration:backupSuccess")}: ${backupDir}`));
  }
  await setupCcrConfiguration();
}

export { backupCcrConfig, configureCcrFeature, configureCcrProxy, configureCcrWithPreset, createDefaultCcrConfig, ensureCcrConfigDir, readCcrConfig, restartAndCheckCcrStatus, selectCcrPreset, setupCcrConfiguration, showConfigurationTips, writeCcrConfig };
