#!/usr/bin/env node

// src/config/env-loader.ts
var ENV_KEY_MAP = {
  claude: "AICLI_API_KEY_CLAUDE",
  gemini: "AICLI_API_KEY_GEMINI",
  deepseek: "AICLI_API_KEY_DEEPSEEK",
  zhipu: "AICLI_API_KEY_ZHIPU",
  kimi: "AICLI_API_KEY_KIMI",
  minimax: "AICLI_API_KEY_MINIMAX",
  openai: "AICLI_API_KEY_OPENAI",
  openrouter: "AICLI_API_KEY_OPENROUTER",
  "google-search": "AICLI_API_KEY_GOOGLESEARCH",
  ollama: "AICLI_API_KEY_OLLAMA"
};
var EnvLoader = class {
  /**
   * 读取指定 provider 的 API Key 环境变量。
   * 优先级：固定映射（如 AICLI_API_KEY_CLAUDE）> 动态格式（AICLI_API_KEY_<ID大写>）
   * 自定义 provider 示例：id="siliconflow" → 读取 AICLI_API_KEY_SILICONFLOW
   */
  static getApiKey(providerId) {
    const fixedEnvVar = ENV_KEY_MAP[providerId];
    const dynamicEnvVar = `AICLI_API_KEY_${providerId.toUpperCase().replace(/-/g, "_")}`;
    if (fixedEnvVar && fixedEnvVar !== dynamicEnvVar) {
      const fixedVal = process.env[fixedEnvVar];
      const dynVal = process.env[dynamicEnvVar];
      if (fixedVal && dynVal && fixedVal !== dynVal) {
        process.stderr.write(`[warn] env var collision: ${fixedEnvVar} and ${dynamicEnvVar} have different values for provider "${providerId}". Using ${fixedEnvVar}.
`);
      }
    }
    if (fixedEnvVar) {
      const val = process.env[fixedEnvVar];
      if (val) return val;
    }
    return process.env[dynamicEnvVar] || void 0;
  }
  static getDefaultProvider() {
    return process.env["AICLI_PROVIDER"] || void 0;
  }
  static isStreamingDisabled() {
    return process.env["AICLI_NO_STREAM"] === "1";
  }
  /** Google Custom Search Engine ID (cx) 环境变量 */
  static getGoogleSearchEngineId() {
    return process.env["AICLI_GOOGLE_CX"] || void 0;
  }
};

// src/core/errors.ts
var AiCliError = class extends Error {
  constructor(message, options) {
    super(message, options);
    this.name = "AiCliError";
  }
};
var ProviderError = class extends AiCliError {
  constructor(providerId, message, cause) {
    super(`[${providerId}] ${message}`, cause !== void 0 ? { cause } : void 0);
    this.providerId = providerId;
    this.name = "ProviderError";
  }
};
var AuthError = class extends ProviderError {
  constructor(providerId) {
    super(providerId, "Invalid or missing API key. Run: ai-cli config");
    this.name = "AuthError";
  }
};
var RateLimitError = class extends ProviderError {
  constructor(providerId) {
    super(providerId, "Rate limit exceeded. Please wait before trying again.");
    this.name = "RateLimitError";
  }
};
var ConfigError = class extends AiCliError {
  constructor(message) {
    super(message);
    this.name = "ConfigError";
  }
};
var ProviderNotFoundError = class extends AiCliError {
  constructor(providerId) {
    super(
      `Provider '${providerId}' is not configured. Run: ai-cli config`
    );
    this.name = "ProviderNotFoundError";
  }
};
var ToolError = class extends AiCliError {
  constructor(toolName, message, cause) {
    super(`[${toolName}] ${message}`, cause !== void 0 ? { cause } : void 0);
    this.toolName = toolName;
    this.name = "ToolError";
  }
};
var NetworkError = class extends AiCliError {
  constructor(message, statusCode, cause) {
    super(message, cause !== void 0 ? { cause } : void 0);
    this.statusCode = statusCode;
    this.name = "NetworkError";
  }
};

export {
  EnvLoader,
  ProviderError,
  AuthError,
  RateLimitError,
  ConfigError,
  ProviderNotFoundError,
  ToolError,
  NetworkError
};
