import { readdirSync } from 'node:fs';
import process__default from 'node:process';
import { fileURLToPath } from 'node:url';
import a from './index5.mjs';
import { d as dayjs } from '../shared/ccjk.RyizuzOI.mjs';
import { i as inquirer } from './index6.mjs';
import ora from './index8.mjs';
import { a as semver } from '../shared/ccjk.CxpGa6MC.mjs';
import { p as parse } from '../shared/ccjk.BBtCGd_g.mjs';
import { x as K } from './main.mjs';
import { CODEX_CONFIG_FILE, CODEX_AUTH_FILE, CODEX_DIR, SUPPORTED_LANGS, CODEX_AGENTS_FILE, CODEX_PROMPTS_DIR, ZCF_CONFIG_FILE, AI_OUTPUT_LANGUAGES } from './constants.mjs';
import { ensureI18nInitialized, i18n, format } from './index2.mjs';
import { updateZcfConfig, readZcfConfig, readDefaultTomlConfig, updateTomlConfig } from './ccjk-config.mjs';
import { i as applyAiLanguageDirective } from './config.mjs';
import { exists, readFile, ensureDir, writeFileAtomic, writeFile, copyDir, copyFile } from './fs-operations.mjs';
import { readJsonConfig, writeJsonConfig } from './json-config.mjs';
import { i as isWindows, m as getMcpCommand, g as getSystemRoot, w as wrapCommandWithSudo, n as normalizeTomlPath } from './platform.mjs';
import { a as addNumbersToChoices } from '../shared/ccjk.BFQ7yr5S.mjs';
import { resolveAiOutputLanguage } from './prompts.mjs';
import { p as promptBoolean } from '../shared/ccjk.DZ2LLOa-.mjs';
import { M as MCP_SERVICE_CONFIGS, j as isMcpServiceCompatible, g as getMcpServices, k as getMcpServicesWithCompatibility } from './claude-config.mjs';
import { j as join, d as dirname } from '../shared/ccjk.bQ7Dh1g4.mjs';

function detectConfigManagementMode() {
  try {
    const config = readCodexConfig();
    if (!config || !config.providers || config.providers.length === 0) {
      return {
        mode: "initial",
        hasProviders: false,
        providerCount: 0
      };
    }
    return {
      mode: "management",
      hasProviders: true,
      providerCount: config.providers.length,
      currentProvider: config.modelProvider,
      providers: config.providers,
      isUnmanaged: config.managed === false ? true : void 0
    };
  } catch (error) {
    return {
      mode: "initial",
      hasProviders: false,
      providerCount: 0,
      error: error instanceof Error ? error.message : "Unknown error"
    };
  }
}

function applyCodexPlatformCommand(config) {
  if (isWindows() && config.command) {
    const mcpCmd = getMcpCommand(config.command);
    if (mcpCmd[0] === "cmd") {
      config.command = mcpCmd[0];
      config.args = [...mcpCmd.slice(1), ...config.args || []];
    }
  }
}

const CODEX_RECOMMENDED_MCP_SERVICE_IDS = ["context7", "open-websearch", "mcp-deepwiki", "serena"];
const CODEX_MANAGED_MCP_SERVICE_IDS = new Set(MCP_SERVICE_CONFIGS.map((service) => service.id.toLowerCase()));
function getRecommendedCodexMcpServiceIds() {
  return CODEX_RECOMMENDED_MCP_SERVICE_IDS.filter((id) => {
    const config = MCP_SERVICE_CONFIGS.find((service) => service.id === id);
    if (!config) {
      return false;
    }
    return isMcpServiceCompatible(id).compatible;
  });
}
function reconcileCodexMcpServices(existingServices, selectedServices) {
  const preservedServices = existingServices.filter((service) => !CODEX_MANAGED_MCP_SERVICE_IDS.has(service.id.toLowerCase()));
  return [...preservedServices, ...selectedServices];
}
function applyWindowsEnvToCodexMcpServices(services) {
  return services.map((svc) => {
    if (isWindows()) {
      const systemRoot = getSystemRoot();
      if (systemRoot) {
        return {
          ...svc,
          env: {
            ...svc.env || {},
            SYSTEMROOT: systemRoot
          }
        };
      }
    }
    return svc;
  });
}
async function selectCodexMcpServices(existingServiceIds) {
  ensureI18nInitialized();
  const services = await getMcpServicesWithCompatibility();
  const recommendedIds = new Set(getRecommendedCodexMcpServiceIds());
  const currentIds = new Set(existingServiceIds.map((id) => id.toLowerCase()));
  const useCurrentSelection = currentIds.size > 0;
  const choices = addNumbersToChoices(services.map((service) => ({
    name: `${service.name} - ${a.gray(service.description)}`,
    value: service.id,
    checked: useCurrentSelection ? currentIds.has(service.id.toLowerCase()) : recommendedIds.has(service.id),
    disabled: service.compatible ? false : service.incompatibleReason || true
  })));
  const { services: selectedIds } = await inquirer.prompt({
    type: "checkbox",
    name: "services",
    message: `${i18n.t("mcp:selectMcpServices")}${i18n.t("common:multiSelectHint")}`,
    choices
  });
  if (selectedIds === void 0) {
    console.log(a.yellow(i18n.t("common:cancelled")));
    return void 0;
  }
  return selectedIds;
}
async function configureCodexMcp(options) {
  ensureI18nInitialized();
  const { skipPrompt = false } = options ?? {};
  const existingConfig = readCodexConfig();
  const backupPath = backupCodexComplete();
  if (backupPath)
    console.log(a.gray(getBackupMessage(backupPath)));
  if (skipPrompt) {
    const { runCodexWorkflowSelection } = await Promise.resolve().then(function () { return codex; });
    await runCodexWorkflowSelection({ skipPrompt: true, workflows: options?.workflows ?? [] });
    if (options?.mcpServices === false) {
      updateZcfConfig({ codeToolType: "codex" });
      console.log(a.green(i18n.t("codex:mcpConfigured")));
      return;
    }
    const defaultServiceIds = Array.isArray(options?.mcpServices) ? options.mcpServices : getRecommendedCodexMcpServiceIds();
    const baseProviders2 = existingConfig?.providers || [];
    const existingServices2 = existingConfig?.mcpServices || [];
    const selection2 = [];
    for (const id of defaultServiceIds) {
      const configInfo = MCP_SERVICE_CONFIGS.find((service) => service.id === id);
      if (!configInfo)
        continue;
      if (!isMcpServiceCompatible(id).compatible)
        continue;
      let command = configInfo.config.command || id;
      let args = (configInfo.config.args || []).map((arg) => String(arg));
      if (id === "serena") {
        const idx = args.indexOf("--context");
        if (idx >= 0 && idx + 1 < args.length)
          args[idx + 1] = "codex";
        else
          args.push("--context", "codex");
      }
      const serviceConfig = { id: id.toLowerCase(), command, args };
      applyCodexPlatformCommand(serviceConfig);
      command = serviceConfig.command;
      args = serviceConfig.args || [];
      const env = { ...configInfo.config.env || {} };
      if (isWindows()) {
        const systemRoot = getSystemRoot();
        if (systemRoot)
          env.SYSTEMROOT = systemRoot;
      }
      selection2.push({
        id: id.toLowerCase(),
        command,
        args,
        env: Object.keys(env).length > 0 ? env : void 0,
        startup_timeout_sec: 30
      });
    }
    const finalServices2 = applyWindowsEnvToCodexMcpServices(reconcileCodexMcpServices(existingServices2, selection2));
    writeCodexConfig({
      model: existingConfig?.model || null,
      modelProvider: existingConfig?.modelProvider || null,
      providers: baseProviders2,
      mcpServices: finalServices2,
      features: existingConfig?.features,
      otherConfig: existingConfig?.otherConfig || []
    });
    updateZcfConfig({ codeToolType: "codex" });
    console.log(a.green(i18n.t("codex:mcpConfigured")));
    return;
  }
  const selectedIds = await selectCodexMcpServices(existingConfig?.mcpServices?.map((service) => service.id) || []);
  if (!selectedIds)
    return;
  const servicesMeta = await getMcpServices();
  const baseProviders = existingConfig?.providers || [];
  const selection = [];
  const existingServices = existingConfig?.mcpServices || [];
  if (selectedIds.length === 0) {
    console.log(a.yellow(i18n.t("codex:noMcpConfigured")));
    const preserved = applyWindowsEnvToCodexMcpServices(reconcileCodexMcpServices(existingServices, []));
    writeCodexConfig({
      model: existingConfig?.model || null,
      modelProvider: existingConfig?.modelProvider || null,
      providers: baseProviders,
      mcpServices: preserved,
      features: existingConfig?.features,
      otherConfig: existingConfig?.otherConfig || []
    });
    updateZcfConfig({ codeToolType: "codex" });
    return;
  }
  for (const id of selectedIds) {
    const configInfo = MCP_SERVICE_CONFIGS.find((service) => service.id === id);
    if (!configInfo)
      continue;
    const serviceMeta = servicesMeta.find((service) => service.id === id);
    let command = configInfo.config.command || id;
    let args = (configInfo.config.args || []).map((arg) => String(arg));
    if (id === "serena") {
      const idx = args.indexOf("--context");
      if (idx >= 0 && idx + 1 < args.length) {
        args[idx + 1] = "codex";
      } else {
        args.push("--context", "codex");
      }
    }
    const serviceConfig = { id: id.toLowerCase(), command, args };
    applyCodexPlatformCommand(serviceConfig);
    command = serviceConfig.command;
    args = serviceConfig.args || [];
    const env = { ...configInfo.config.env || {} };
    if (isWindows()) {
      const systemRoot = getSystemRoot();
      if (systemRoot)
        env.SYSTEMROOT = systemRoot;
    }
    if (configInfo.requiresApiKey && configInfo.apiKeyEnvVar) {
      const promptMessage = serviceMeta?.apiKeyPrompt || i18n.t("mcp:apiKeyPrompt");
      const { apiKey } = await inquirer.prompt([{
        type: "input",
        name: "apiKey",
        message: promptMessage,
        validate: (input) => !!input || i18n.t("api:keyRequired")
      }]);
      if (!apiKey)
        continue;
      env[configInfo.apiKeyEnvVar] = apiKey;
    }
    selection.push({
      id: id.toLowerCase(),
      command: serviceConfig.command,
      args: serviceConfig.args,
      env: Object.keys(env).length > 0 ? env : void 0,
      startup_timeout_sec: 30
    });
  }
  const finalServices = applyWindowsEnvToCodexMcpServices(reconcileCodexMcpServices(existingServices, selection));
  writeCodexConfig({
    model: existingConfig?.model || null,
    modelProvider: existingConfig?.modelProvider || null,
    providers: baseProviders,
    mcpServices: finalServices,
    features: existingConfig?.features,
    otherConfig: existingConfig?.otherConfig || []
  });
  updateZcfConfig({ codeToolType: "codex" });
  console.log(a.green(i18n.t("codex:mcpConfigured")));
}

let cachedSkipPromptBackup = null;
function getRootDir() {
  const currentFilePath = fileURLToPath(import.meta.url);
  let dir = dirname(currentFilePath);
  while (dir !== dirname(dir)) {
    if (exists(join(dir, "templates"))) {
      return dir;
    }
    dir = dirname(dir);
  }
  return dirname(currentFilePath);
}
async function detectCodexInstallMethod() {
  try {
    const brewResult = await K("brew", ["list", "--cask", "codex"], { throwOnError: false });
    if (brewResult.exitCode === 0) {
      return "homebrew";
    }
  } catch {
  }
  try {
    const npmResult = await K("npm", ["list", "-g", "@openai/codex"], { throwOnError: false });
    if (npmResult.exitCode === 0 && npmResult.stdout.includes("@openai/codex")) {
      return "npm";
    }
  } catch {
  }
  return "unknown";
}
async function executeCodexInstallation(isUpdate, skipMethodSelection = false) {
  if (isUpdate) {
    console.log(a.green(i18n.t("codex:updatingCli")));
    const installMethod = await detectCodexInstallMethod();
    if (installMethod === "homebrew") {
      console.log(a.gray(i18n.t("codex:detectedHomebrew")));
      const result = await K("brew", ["upgrade", "--cask", "codex"]);
      if (result.exitCode !== 0) {
        throw new Error(`Failed to update codex via Homebrew: exit code ${result.exitCode}`);
      }
    } else if (installMethod === "npm") {
      console.log(a.gray(i18n.t("codex:detectedNpm")));
      const { command, args, usedSudo } = wrapCommandWithSudo("npm", ["install", "-g", "@openai/codex@latest"]);
      if (usedSudo)
        console.log(a.yellow(i18n.t("codex:usingSudo")));
      const result = await K(command, args);
      if (result.exitCode !== 0) {
        throw new Error(`Failed to update codex CLI: exit code ${result.exitCode}`);
      }
    } else {
      console.log(a.yellow(i18n.t("codex:unknownInstallMethod")));
      console.log(a.gray(i18n.t("codex:fallingBackToNpm")));
      const { command, args, usedSudo } = wrapCommandWithSudo("npm", ["install", "-g", "@openai/codex@latest"]);
      if (usedSudo)
        console.log(a.yellow(i18n.t("codex:usingSudo")));
      const result = await K(command, args);
      if (result.exitCode !== 0) {
        throw new Error(`Failed to update codex CLI: exit code ${result.exitCode}`);
      }
    }
    console.log(a.green(i18n.t("codex:updateSuccess")));
  } else {
    const { installCodex } = await import('./installer.mjs');
    await installCodex(skipMethodSelection);
  }
}
function getUninstallOptions() {
  return [
    { name: i18n.t("codex:uninstallItemConfig"), value: "config" },
    { name: i18n.t("codex:uninstallItemAuth"), value: "auth" },
    { name: i18n.t("codex:uninstallItemApiConfig"), value: "api-config" },
    { name: i18n.t("codex:uninstallItemMcpConfig"), value: "mcp-config" },
    { name: i18n.t("codex:uninstallItemSystemPrompt"), value: "system-prompt" },
    { name: i18n.t("codex:uninstallItemWorkflow"), value: "workflow" },
    { name: i18n.t("codex:uninstallItemCliPackage"), value: "cli-package" },
    { name: i18n.t("codex:uninstallItemBackups"), value: "backups" }
  ];
}
function handleUninstallCancellation() {
  console.log(a.yellow(i18n.t("codex:uninstallCancelled")));
}
function createBackupDirectory(timestamp) {
  const backupBaseDir = join(CODEX_DIR, "backup");
  const backupDir = join(backupBaseDir, `backup_${timestamp}`);
  ensureDir(backupDir);
  return backupDir;
}
function backupCodexFiles() {
  if (!exists(CODEX_DIR))
    return null;
  if (process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP === "true" && cachedSkipPromptBackup)
    return cachedSkipPromptBackup;
  const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");
  const backupDir = createBackupDirectory(timestamp);
  const filter = (path) => {
    return !path.includes("/backup");
  };
  copyDir(CODEX_DIR, backupDir, { filter });
  if (process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP === "true")
    cachedSkipPromptBackup = backupDir;
  return backupDir;
}
function backupCodexComplete() {
  return backupCodexFiles();
}
function backupCodexConfig() {
  if (!exists(CODEX_CONFIG_FILE))
    return null;
  try {
    const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");
    const backupDir = createBackupDirectory(timestamp);
    const backupPath = join(backupDir, "config.toml");
    copyFile(CODEX_CONFIG_FILE, backupPath);
    return backupPath;
  } catch {
    return null;
  }
}
function backupCodexAgents() {
  if (process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP === "true" && cachedSkipPromptBackup)
    return cachedSkipPromptBackup;
  if (!exists(CODEX_AGENTS_FILE))
    return null;
  try {
    const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");
    const backupDir = createBackupDirectory(timestamp);
    const backupPath = join(backupDir, "AGENTS.md");
    copyFile(CODEX_AGENTS_FILE, backupPath);
    return backupPath;
  } catch {
    return null;
  }
}
function backupCodexPrompts() {
  if (process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP === "true" && cachedSkipPromptBackup)
    return cachedSkipPromptBackup;
  if (!exists(CODEX_PROMPTS_DIR))
    return null;
  try {
    const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");
    const backupDir = createBackupDirectory(timestamp);
    const backupPath = join(backupDir, "prompts");
    copyDir(CODEX_PROMPTS_DIR, backupPath);
    return backupPath;
  } catch {
    return null;
  }
}
function getBackupMessage(path) {
  if (!path)
    return "";
  if (!i18n.isInitialized) {
    return `Backup created: ${path}`;
  }
  return i18n.t("codex:backupSuccess", { path });
}
function needsEnvKeyMigration() {
  if (!exists(CODEX_CONFIG_FILE))
    return false;
  try {
    const content = readFile(CODEX_CONFIG_FILE);
    const hasOldEnvKey = /^\s*env_key\s*=/m.test(content);
    return hasOldEnvKey;
  } catch {
    return false;
  }
}
function migrateEnvKeyToTempEnvKey() {
  if (!exists(CODEX_CONFIG_FILE))
    return false;
  try {
    const content = readFile(CODEX_CONFIG_FILE);
    if (!needsEnvKeyMigration())
      return false;
    const backupPath = backupCodexConfig();
    if (backupPath) {
      console.log(a.gray(getBackupMessage(backupPath)));
    }
    const migratedContent = migrateEnvKeyInContent(content);
    writeFileAtomic(CODEX_CONFIG_FILE, migratedContent);
    updateTomlConfig(ZCF_CONFIG_FILE, {
      codex: {
        envKeyMigrated: true
      }
    });
    const message = i18n.isInitialized ? i18n.t("codex:envKeyMigrationComplete") : "\u2714 env_key to temp_env_key migration completed";
    console.log(a.green(message));
    return true;
  } catch (error) {
    console.error(a.yellow(`env_key migration warning: ${error.message}`));
    return false;
  }
}
function migrateEnvKeyInContent(content) {
  const lines = content.split("\n");
  const result = [];
  let currentSectionHasTempEnvKey = false;
  let currentSection = "";
  const sectionHasTempEnvKey = /* @__PURE__ */ new Map();
  let tempSection = "";
  for (const line of lines) {
    const sectionMatch = line.match(/^\s*\[([^\]]+)\]/);
    if (sectionMatch) {
      tempSection = sectionMatch[1];
    }
    if (tempSection && /^\s*temp_env_key\s*=/.test(line)) {
      sectionHasTempEnvKey.set(tempSection, true);
    }
  }
  for (const line of lines) {
    const sectionMatch = line.match(/^\s*\[([^\]]+)\]/);
    if (sectionMatch) {
      currentSection = sectionMatch[1];
      currentSectionHasTempEnvKey = sectionHasTempEnvKey.get(currentSection) || false;
    }
    const envKeyMatch = line.match(/^(\s*)env_key(\s*=.*)$/);
    if (envKeyMatch) {
      if (currentSectionHasTempEnvKey) {
        continue;
      } else {
        result.push(`${envKeyMatch[1]}temp_env_key${envKeyMatch[2]}`);
        continue;
      }
    }
    result.push(line);
  }
  return result.join("\n");
}
function isCodexGoalsFeatureEnabled(content) {
  try {
    const parsed = parse(content);
    return parsed.features?.goals === true;
  } catch {
    return false;
  }
}
function escapeRegExp(input) {
  return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function setCodexFeatureFlagInContent(content, key, enabled) {
  const normalized = content.trimEnd();
  const lines = normalized ? normalized.split("\n") : [];
  const featuresHeaderIndex = lines.findIndex((line) => line.trim() === "[features]");
  if (featuresHeaderIndex === -1) {
    const prefix = normalized ? `${normalized}

` : "";
    return `${prefix}[features]
${key} = ${enabled}
`;
  }
  let insertIndex = lines.length;
  let featureIndex = -1;
  const keyPattern = new RegExp(`^${escapeRegExp(key)}\\s*=`);
  for (let index = featuresHeaderIndex + 1; index < lines.length; index += 1) {
    const trimmed = lines[index].trim();
    if (/^\[[^\]]+\]\s*$/.test(trimmed)) {
      insertIndex = index;
      break;
    }
    if (keyPattern.test(trimmed)) {
      featureIndex = index;
      break;
    }
  }
  if (featureIndex !== -1) {
    const replacePattern = new RegExp(`^(\\s*)${escapeRegExp(key)}\\s*=.*$`);
    lines[featureIndex] = lines[featureIndex].replace(replacePattern, `$1${key} = ${enabled}`);
  } else {
    lines.splice(insertIndex, 0, `${key} = ${enabled}`);
  }
  return `${lines.join("\n")}
`;
}
function normalizeCodexFeatureFlags(input) {
  if (!input || typeof input !== "object" || Array.isArray(input)) {
    return void 0;
  }
  const flags = {};
  for (const [key, value] of Object.entries(input)) {
    if (/^\w+$/.test(key) && typeof value === "boolean") {
      flags[key] = value;
    }
  }
  return Object.keys(flags).length > 0 ? flags : void 0;
}
function applyCodexFeatureFlagsToContent(content, features) {
  let nextContent = content;
  for (const [key, enabled] of Object.entries(features).sort(([a], [b]) => a.localeCompare(b))) {
    nextContent = setCodexFeatureFlagInContent(nextContent, key, enabled);
  }
  return nextContent;
}
function setCodexGoalsFeatureInContent(content, enabled = true) {
  return setCodexFeatureFlagInContent(content, "goals", enabled);
}
function readCodexGoalsFeatureEnabled() {
  if (!exists(CODEX_CONFIG_FILE)) {
    return false;
  }
  try {
    return isCodexGoalsFeatureEnabled(readFile(CODEX_CONFIG_FILE));
  } catch {
    return false;
  }
}
function buildCodexGoalsFeatureConfigContent(existingContent = "") {
  return setCodexGoalsFeatureInContent(existingContent, true);
}
function ensureEnvKeyMigration() {
  const tomlConfig = readDefaultTomlConfig();
  if (tomlConfig?.codex?.envKeyMigrated)
    return;
  if (needsEnvKeyMigration()) {
    migrateEnvKeyToTempEnvKey();
  }
}
function sanitizeProviderName(input) {
  const cleaned = input.trim();
  if (!cleaned)
    return "";
  return cleaned.toLowerCase().replace(/\./g, "-").replace(/\s+/g, "-").replace(/[^a-z0-9\-]/g, "");
}
function parseCodexConfig(content) {
  if (!content.trim()) {
    return {
      model: null,
      modelProvider: null,
      providers: [],
      mcpServices: [],
      managed: false,
      features: void 0,
      otherConfig: [],
      modelProviderCommented: void 0
    };
  }
  try {
    const normalizedContent = content.replace(/(SYSTEMROOT\s*=\s*")[^"\n]+("?)/g, (match) => {
      return match.replace(/\\\\/g, "/").replace(/\\/g, "/").replace('C:/Windows"?', 'C:/Windows"');
    });
    const tomlData = parse(normalizedContent);
    const features = normalizeCodexFeatureFlags(tomlData.features);
    const providers = [];
    if (tomlData.model_providers) {
      for (const [id, providerData] of Object.entries(tomlData.model_providers)) {
        const provider = providerData;
        providers.push({
          id,
          name: provider.name || id,
          baseUrl: provider.base_url || "",
          wireApi: provider.wire_api || "responses",
          tempEnvKey: provider.temp_env_key || "OPENAI_API_KEY",
          requiresOpenaiAuth: provider.requires_openai_auth !== false,
          model: provider.model || void 0
          // Parse model field from provider
        });
      }
    }
    const mcpServices = [];
    if (tomlData.mcp_servers) {
      const KNOWN_MCP_FIELDS = /* @__PURE__ */ new Set(["command", "args", "env", "startup_timeout_sec"]);
      for (const [id, mcpData] of Object.entries(tomlData.mcp_servers)) {
        const mcp = mcpData;
        const extraFields = {};
        for (const [key, value] of Object.entries(mcp)) {
          if (!KNOWN_MCP_FIELDS.has(key)) {
            extraFields[key] = value;
          }
        }
        mcpServices.push({
          id,
          command: mcp.command || id,
          args: mcp.args || [],
          env: Object.keys(mcp.env || {}).length > 0 ? mcp.env : void 0,
          startup_timeout_sec: mcp.startup_timeout_sec,
          // Only add extraFields if there are any extra fields
          extraFields: Object.keys(extraFields).length > 0 ? extraFields : void 0
        });
      }
    }
    const model = tomlData.model || null;
    let modelProvider = null;
    let modelProviderCommented;
    const commentedMatch = content.match(/^(\s*)#\s*model_provider\s*=\s*"([^"]+)"/m);
    if (commentedMatch) {
      modelProvider = commentedMatch[2];
      modelProviderCommented = true;
    } else {
      const lines2 = content.split("\n");
      let inSection = false;
      for (const line of lines2) {
        const trimmedLine = line.trim();
        if (!trimmedLine)
          continue;
        if (trimmedLine.startsWith("[")) {
          inSection = true;
          continue;
        }
        if (trimmedLine.startsWith("#")) {
          if (trimmedLine.includes("--- model provider added by CCJK ---")) {
            inSection = false;
          }
          continue;
        }
        if (!inSection && trimmedLine.startsWith("model_provider")) {
          const match = trimmedLine.match(/model_provider\s*=\s*"([^"]+)"/);
          if (match) {
            modelProvider = match[1];
            modelProviderCommented = false;
            break;
          }
        }
      }
      if (!modelProvider) {
        modelProvider = tomlData.model_provider || null;
        modelProviderCommented = false;
      }
    }
    const otherConfig = [];
    const lines = content.split("\n");
    let skipCurrentSection = false;
    for (const line of lines) {
      const trimmed = line.trim();
      if (!trimmed)
        continue;
      if (/^#\s*---\s*model provider added by CCJK\s*---\s*$/i.test(trimmed))
        continue;
      if (/^#\s*---\s*MCP servers added by CCJK\s*---\s*$/i.test(trimmed))
        continue;
      if (/Managed by CCJK/i.test(trimmed))
        continue;
      const sec = trimmed.match(/^\[([^\]]+)\]/);
      if (sec) {
        const name = sec[1];
        skipCurrentSection = name.startsWith("model_providers.") || name.startsWith("mcp_servers.");
        if (skipCurrentSection)
          continue;
        otherConfig.push(line);
        continue;
      }
      if (/^#?\s*model_provider\s*=/.test(trimmed))
        continue;
      if (/^model\s*=/.test(trimmed))
        continue;
      if (!skipCurrentSection) {
        otherConfig.push(line);
      }
    }
    const managed = providers.length > 0 || mcpServices.length > 0 || modelProvider !== null || model !== null;
    return {
      model,
      modelProvider,
      providers,
      mcpServices,
      managed,
      features,
      otherConfig,
      modelProviderCommented
    };
  } catch (error) {
    if (process__default.env.NODE_ENV === "development" || process__default.env.DEBUG) {
      console.warn("TOML parsing failed, falling back to basic parsing:", error);
    }
    const cleaned = content.replace(/^\s*#\s*---\s*model provider added by CCJK\s*---\s*$/gim, "").replace(/^\s*#\s*---\s*MCP servers added by CCJK\s*---\s*$/gim, "").replace(/^\[model_providers\.[^\]]+\][\s\S]*?(?=^\[|$)/gim, "").replace(/^\[mcp_servers\.[^\]]+\][\s\S]*?(?=^\[|$)/gim, "").replace(/^\s*(?:#\s*)?model_provider\s*=.*$/gim, "").replace(/^\s*model\s*=.*$/gim, "").replace(/\n{3,}/g, "\n\n");
    const otherConfig = cleaned.split("\n").map((l) => l.replace(/\s+$/g, "")).filter((l) => l.trim().length > 0);
    return {
      model: null,
      modelProvider: null,
      providers: [],
      mcpServices: [],
      managed: false,
      features: void 0,
      otherConfig,
      modelProviderCommented: void 0
    };
  }
}
function formatInlineTableValue(value) {
  if (value === null || value === void 0) {
    return "";
  }
  if (typeof value === "string") {
    const normalized = normalizeTomlPath(value);
    return `'${normalized}'`;
  }
  if (typeof value === "number" || typeof value === "boolean") {
    return String(value);
  }
  if (Array.isArray(value)) {
    const items = value.map((item) => {
      if (typeof item === "string") {
        const normalized = normalizeTomlPath(item);
        return `'${normalized}'`;
      }
      if (typeof item === "object" && item !== null && !Array.isArray(item)) {
        return formatInlineTable(item);
      }
      return String(item);
    }).join(", ");
    return `[${items}]`;
  }
  if (typeof value === "object") {
    return formatInlineTable(value);
  }
  return String(value);
}
function formatInlineTable(obj) {
  const entries = Object.entries(obj).filter(([_, v]) => v !== null && v !== void 0).map(([k, v]) => `${k} = ${formatInlineTableValue(v)}`).join(", ");
  return `{${entries}}`;
}
function formatTomlField(key, value) {
  if (value === null || value === void 0) {
    return "";
  }
  if (typeof value === "string") {
    const normalized = normalizeTomlPath(value);
    const escaped = normalized.replace(/"/g, '\\"');
    return `${key} = "${escaped}"`;
  }
  if (typeof value === "number" || typeof value === "boolean") {
    return `${key} = ${value}`;
  }
  if (Array.isArray(value)) {
    const items = value.map((item) => {
      if (typeof item === "string") {
        const normalized = normalizeTomlPath(item);
        const escaped = normalized.replace(/"/g, '\\"');
        return `"${escaped}"`;
      }
      if (typeof item === "object" && item !== null && !Array.isArray(item)) {
        return formatInlineTable(item);
      }
      return String(item);
    }).join(", ");
    return `${key} = [${items}]`;
  }
  if (typeof value === "object") {
    return `${key} = ${formatInlineTable(value)}`;
  }
  return "";
}
function readCodexConfig() {
  if (!exists(CODEX_CONFIG_FILE))
    return null;
  ensureEnvKeyMigration();
  try {
    const content = readFile(CODEX_CONFIG_FILE);
    return parseCodexConfig(content);
  } catch {
    return null;
  }
}
function renderCodexConfig(data) {
  const lines = [];
  if (data.model || data.modelProvider || data.providers.length > 0 || data.modelProviderCommented) {
    lines.push("# --- model provider added by CCJK ---");
    if (data.model) {
      lines.push(`model = "${data.model}"`);
    }
    if (data.modelProvider) {
      const commentPrefix = data.modelProviderCommented ? "# " : "";
      lines.push(`${commentPrefix}model_provider = "${data.modelProvider}"`);
    }
    lines.push("");
  }
  if (data.otherConfig && data.otherConfig.length > 0) {
    const preserved = data.otherConfig.filter((raw) => {
      const l = String(raw).trim();
      if (!l)
        return false;
      if (/^#\s*---\s*model provider added by CCJK\s*---\s*$/i.test(l))
        return false;
      if (/^#\s*---\s*MCP servers added by CCJK\s*---\s*$/i.test(l))
        return false;
      if (/^\[\s*mcp_servers\./i.test(l))
        return false;
      if (/^\[\s*model_providers\./i.test(l))
        return false;
      if (/^#?\s*model_provider\s*=/.test(l))
        return false;
      if (/^\s*model\s*=/.test(l) && !l.includes("["))
        return false;
      return true;
    });
    if (preserved.length > 0) {
      lines.push(...preserved);
      if (data.providers.length > 0 || data.mcpServices.length > 0) {
        lines.push("");
      }
    }
  }
  if (data.providers.length > 0) {
    for (const provider of data.providers) {
      lines.push("");
      lines.push(`[model_providers.${provider.id}]`);
      lines.push(`name = "${provider.name}"`);
      lines.push(`base_url = "${provider.baseUrl}"`);
      lines.push(`wire_api = "${provider.wireApi}"`);
      lines.push(`temp_env_key = "${provider.tempEnvKey}"`);
      lines.push(`requires_openai_auth = ${provider.requiresOpenaiAuth}`);
      if (provider.model) {
        lines.push(`model = "${provider.model}"`);
      }
    }
  }
  if (data.mcpServices.length > 0) {
    lines.push("");
    lines.push("# --- MCP servers added by CCJK ---");
    for (const service of data.mcpServices) {
      lines.push(`[mcp_servers.${service.id}]`);
      const normalizedCommand = normalizeTomlPath(service.command);
      lines.push(`command = "${normalizedCommand}"`);
      const argsString = service.args.length > 0 ? service.args.map((arg) => `"${arg}"`).join(", ") : "";
      lines.push(`args = [${argsString}]`);
      if (service.env && Object.keys(service.env).length > 0) {
        const envEntries = Object.entries(service.env).map(([key, value]) => `${key} = '${value}'`).join(", ");
        lines.push(`env = {${envEntries}}`);
      }
      if (service.startup_timeout_sec) {
        lines.push(`startup_timeout_sec = ${service.startup_timeout_sec}`);
      }
      if (service.extraFields) {
        for (const [key, value] of Object.entries(service.extraFields)) {
          const formatted = formatTomlField(key, value);
          if (formatted) {
            lines.push(formatted);
          }
        }
      }
      lines.push("");
    }
    if (lines[lines.length - 1] === "") {
      lines.pop();
    }
  }
  let result = lines.join("\n");
  if (result && !result.endsWith("\n")) {
    result += "\n";
  }
  if (data.features && Object.keys(data.features).length > 0) {
    result = applyCodexFeatureFlagsToContent(result, data.features);
  }
  return result;
}
function writeCodexConfig(data) {
  ensureEnvKeyMigration();
  ensureDir(CODEX_DIR);
  writeFileAtomic(CODEX_CONFIG_FILE, renderCodexConfig(data));
}
function writeAuthFile(newEntries, authMode) {
  ensureDir(CODEX_DIR);
  const existing = readJsonConfig(CODEX_AUTH_FILE, { defaultValue: {} }) || {};
  const merged = { ...existing, ...newEntries };
  if (authMode) {
    merged.auth_mode = authMode;
  }
  writeJsonConfig(CODEX_AUTH_FILE, merged, { pretty: true });
}
async function isCodexInstalled() {
  try {
    const npmResult = await K("npm", ["list", "-g", "--depth=0"]);
    if (npmResult.exitCode === 0 && npmResult.stdout.includes("@openai/codex@")) {
      return true;
    }
  } catch {
  }
  try {
    const brewResult = await K("brew", ["list", "--cask", "codex"], { throwOnError: false });
    if (brewResult.exitCode === 0) {
      return true;
    }
  } catch {
  }
  return false;
}
async function getCodexVersion() {
  try {
    const npmResult = await K("npm", ["list", "-g", "--depth=0"]);
    if (npmResult.exitCode === 0) {
      const match = npmResult.stdout.match(/@openai\/codex@(\S+)/);
      if (match) {
        return match[1];
      }
    }
  } catch {
  }
  try {
    const brewResult = await K("brew", ["info", "--cask", "codex", "--json=v2"], { throwOnError: false });
    if (brewResult.exitCode === 0) {
      const info = JSON.parse(brewResult.stdout);
      if (info.casks && Array.isArray(info.casks) && info.casks.length > 0) {
        const cask = info.casks[0];
        if (cask.installed && typeof cask.installed === "string") {
          return cask.installed;
        }
      }
    }
  } catch {
  }
  return null;
}
async function checkCodexUpdate() {
  try {
    const currentVersion = await getCodexVersion();
    if (!currentVersion) {
      return {
        installed: false,
        currentVersion: null,
        latestVersion: null,
        needsUpdate: false
      };
    }
    const result = await K("npm", ["view", "@openai/codex", "--json"]);
    if (result.exitCode !== 0) {
      return {
        installed: true,
        currentVersion,
        latestVersion: null,
        needsUpdate: false
      };
    }
    const packageInfo = JSON.parse(result.stdout);
    const latestVersion = packageInfo["dist-tags"]?.latest;
    if (!latestVersion) {
      return {
        installed: true,
        currentVersion,
        latestVersion: null,
        needsUpdate: false
      };
    }
    const needsUpdate = semver.gt(latestVersion, currentVersion);
    return {
      installed: true,
      currentVersion,
      latestVersion,
      needsUpdate
    };
  } catch {
    return {
      installed: false,
      currentVersion: null,
      latestVersion: null,
      needsUpdate: false
    };
  }
}
async function installCodexCli(skipMethodSelection = false) {
  ensureI18nInitialized();
  if (await isCodexInstalled()) {
    const { needsUpdate } = await checkCodexUpdate();
    if (needsUpdate) {
      await executeCodexInstallation(true, skipMethodSelection);
      return;
    } else {
      console.log(a.yellow(i18n.t("codex:alreadyInstalled")));
      return;
    }
  }
  await executeCodexInstallation(false, skipMethodSelection);
}
async function runCodexWorkflowImportWithLanguageSelection(options) {
  ensureI18nInitialized();
  const zcfConfig = readZcfConfig();
  const { aiOutputLang: commandLineOption, skipPrompt = false } = options ?? {};
  const aiOutputLang = await resolveAiOutputLanguage(
    i18n.language,
    commandLineOption,
    zcfConfig,
    skipPrompt
  );
  updateZcfConfig({ aiOutputLang });
  applyAiLanguageDirective(aiOutputLang);
  await runCodexSystemPromptSelection(skipPrompt);
  ensureCodexAgentsLanguageDirective(aiOutputLang);
  await runCodexWorkflowSelection(options);
  console.log(a.green(i18n.t("codex:workflowInstall")));
  return aiOutputLang;
}
async function runCodexSystemPromptSelection(skipPrompt = false) {
  ensureI18nInitialized();
  const rootDir = getRootDir();
  const zcfConfig = readZcfConfig();
  const { readDefaultTomlConfig: readDefaultTomlConfig2 } = await import('./ccjk-config.mjs');
  const tomlConfig = readDefaultTomlConfig2();
  const { resolveTemplateLanguage } = await import('./prompts.mjs');
  const preferredLang = await resolveTemplateLanguage(
    void 0,
    // No command line option for this function
    zcfConfig,
    skipPrompt
    // Pass skipPrompt flag
  );
  updateZcfConfig({ templateLang: preferredLang });
  let systemPromptSrc = join(rootDir, "templates", "common", "output-styles", preferredLang);
  if (!exists(systemPromptSrc))
    systemPromptSrc = join(rootDir, "templates", "common", "output-styles", "zh-CN");
  if (!exists(systemPromptSrc))
    return;
  const availablePrompts = [
    {
      id: "speed-coder",
      name: i18n.t("configuration:outputStyles.speed-coder.name"),
      description: i18n.t("configuration:outputStyles.speed-coder.description")
    },
    {
      id: "senior-architect",
      name: i18n.t("configuration:outputStyles.senior-architect.name"),
      description: i18n.t("configuration:outputStyles.senior-architect.description")
    },
    {
      id: "pair-programmer",
      name: i18n.t("configuration:outputStyles.pair-programmer.name"),
      description: i18n.t("configuration:outputStyles.pair-programmer.description")
    }
  ].filter((style) => exists(join(systemPromptSrc, `${style.id}.md`)));
  if (availablePrompts.length === 0)
    return;
  const { resolveSystemPromptStyle } = await import('./prompts.mjs');
  const systemPrompt = await resolveSystemPromptStyle(
    availablePrompts,
    void 0,
    // No command line option for this function
    tomlConfig,
    skipPrompt
    // Pass skipPrompt flag
  );
  if (!systemPrompt)
    return;
  const promptFile = join(systemPromptSrc, `${systemPrompt}.md`);
  const content = readFile(promptFile);
  ensureDir(CODEX_DIR);
  const backupPath = backupCodexAgents();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  writeFileAtomic(CODEX_AGENTS_FILE, content);
  try {
    const { updateTomlConfig: updateTomlConfig2 } = await import('./ccjk-config.mjs');
    const { ZCF_CONFIG_FILE: ZCF_CONFIG_FILE2 } = await import('./constants.mjs');
    updateTomlConfig2(ZCF_CONFIG_FILE2, {
      codex: {
        systemPromptStyle: systemPrompt
      }
    });
  } catch (error) {
    console.error("Failed to update CCJK config:", error);
  }
}
async function runCodexWorkflowSelection(options) {
  ensureI18nInitialized();
  const { skipPrompt = false, workflows: presetWorkflows = [] } = options ?? {};
  const rootDir = getRootDir();
  const zcfConfig = readZcfConfig();
  const templateLang = zcfConfig?.templateLang || zcfConfig?.preferredLang || "en";
  let preferredLang = templateLang === "en" ? "en" : "zh-CN";
  const workflowSrc = join(rootDir, "templates", "common", "workflow");
  if (!exists(workflowSrc))
    return;
  let allWorkflows = getCodexWorkflowOptionDefinitions(workflowSrc, preferredLang);
  if (allWorkflows.length === 0 && preferredLang === "en") {
    preferredLang = "zh-CN";
    allWorkflows = getCodexWorkflowOptionDefinitions(workflowSrc, preferredLang);
  }
  if (allWorkflows.length === 0)
    return;
  if (skipPrompt) {
    ensureDir(CODEX_PROMPTS_DIR);
    const backupPath2 = backupCodexPrompts();
    if (backupPath2) {
      console.log(a.gray(getBackupMessage(backupPath2)));
    }
    let workflowsToInstall;
    if (presetWorkflows.length > 0) {
      const selectedWorkflows = allWorkflows.filter(
        (workflow) => presetWorkflows.includes(workflow.id) || presetWorkflows.includes(workflow.name)
      );
      workflowsToInstall = expandSelectedWorkflowPaths(selectedWorkflows.map((w) => w.path), workflowSrc, preferredLang);
    } else {
      workflowsToInstall = expandSelectedWorkflowPaths(allWorkflows.map((w) => w.path), workflowSrc, preferredLang);
    }
    for (const workflowPath of workflowsToInstall) {
      const content = readFile(workflowPath);
      const filename = workflowPath.split("/").pop() || "workflow.md";
      const targetPath = join(CODEX_PROMPTS_DIR, filename);
      writeFile(targetPath, content);
    }
    return;
  }
  const { workflows } = await inquirer.prompt([{
    type: "checkbox",
    name: "workflows",
    message: i18n.t("codex:workflowSelectionPrompt"),
    choices: addNumbersToChoices(allWorkflows.map((workflow) => ({
      name: `${workflow.name} ${a.gray(`- ${workflow.description}`)}`,
      value: workflow.path,
      checked: true
      // Default all selected
    })))
  }]);
  if (!workflows || workflows.length === 0)
    return;
  ensureDir(CODEX_PROMPTS_DIR);
  const backupPath = backupCodexPrompts();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  const finalWorkflowPaths = expandSelectedWorkflowPaths(workflows, workflowSrc, preferredLang);
  for (const workflowPath of finalWorkflowPaths) {
    const content = readFile(workflowPath);
    const filename = workflowPath.split("/").pop() || "workflow.md";
    const targetPath = join(CODEX_PROMPTS_DIR, filename);
    writeFile(targetPath, content);
  }
}
const ESSENTIAL_GROUP_SENTINEL = "::essentialGroup";
const GIT_GROUP_SENTINEL = "::gitGroup";
function getCodexWorkflowOptionDefinitions(workflowSrc, preferredLang) {
  const options = [];
  const pushIfExists = (id, nameKey, descriptionKey, path, existsCheck = () => exists(path)) => {
    if (!existsCheck()) {
      return;
    }
    options.push({
      id,
      name: i18n.t(nameKey),
      description: i18n.t(descriptionKey),
      path
    });
  };
  pushIfExists(
    "sixStepsWorkflow",
    "workflow:workflowOption.sixStepsWorkflow",
    "workflow:workflowDescription.sixStepsWorkflow",
    join(workflowSrc, "sixStep", preferredLang, "workflow.md")
  );
  pushIfExists(
    "essentialTools",
    "workflow:workflowOption.essentialTools",
    "workflow:workflowDescription.essentialTools",
    ESSENTIAL_GROUP_SENTINEL,
    () => getEssentialPromptFiles(workflowSrc, preferredLang).length > 0
  );
  pushIfExists(
    "gitWorkflow",
    "workflow:workflowOption.gitWorkflow",
    "workflow:workflowDescription.gitWorkflow",
    GIT_GROUP_SENTINEL,
    () => getGitPromptFiles(workflowSrc, preferredLang).length > 0
  );
  pushIfExists(
    "interviewWorkflow",
    "workflow:workflowOption.interviewWorkflow",
    "workflow:workflowDescription.interviewWorkflow",
    join(workflowSrc, "interview", preferredLang, "interview.md")
  );
  pushIfExists(
    "specFirstTDD",
    "workflow:workflowOption.specFirstTDD",
    "workflow:workflowDescription.specFirstTDD",
    join(workflowSrc, "specFirstTDD", preferredLang, "spec-first-tdd.md")
  );
  pushIfExists(
    "continuousDelivery",
    "workflow:workflowOption.continuousDelivery",
    "workflow:workflowDescription.continuousDelivery",
    join(workflowSrc, "continuousDelivery", preferredLang, "continuous-delivery.md")
  );
  pushIfExists(
    "refactoringMaster",
    "workflow:workflowOption.refactoringMaster",
    "workflow:workflowDescription.refactoringMaster",
    join(workflowSrc, "refactoringMaster", preferredLang, "refactoring-master.md")
  );
  pushIfExists(
    "linearMethod",
    "workflow:workflowOption.linearMethod",
    "workflow:workflowDescription.linearMethod",
    join(workflowSrc, "linearMethod", preferredLang, "linear-method.md")
  );
  return options;
}
function expandSelectedWorkflowPaths(paths, workflowSrc, preferredLang) {
  const expanded = [];
  for (const p of paths) {
    if (p === ESSENTIAL_GROUP_SENTINEL) {
      expanded.push(...getEssentialPromptFiles(workflowSrc, preferredLang));
    } else if (p === GIT_GROUP_SENTINEL) {
      expanded.push(...getGitPromptFiles(workflowSrc, preferredLang));
    } else {
      expanded.push(p);
    }
  }
  return expanded;
}
function getEssentialPromptFiles(workflowSrc, preferredLang) {
  const essentialDir = join(workflowSrc, "essential", preferredLang);
  const files = [
    "feat.md",
    "goal.md",
    "init-project.md"
  ];
  const resolved = [];
  for (const f of files) {
    const full = join(essentialDir, f);
    if (exists(full))
      resolved.push(full);
  }
  return resolved;
}
function getGitPromptFiles(workflowSrc, preferredLang) {
  const gitPromptsDir = join(workflowSrc, "git", preferredLang);
  const files = [
    "git-commit.md",
    "git-rollback.md",
    "git-cleanBranches.md",
    "git-worktree.md"
  ];
  const resolved = [];
  for (const f of files) {
    const full = join(gitPromptsDir, f);
    if (exists(full))
      resolved.push(full);
  }
  return resolved;
}
function resolveCodexWorkflowNames(workflowIds, preferredLang) {
  const workflowSrc = join(getRootDir(), "templates", "common", "workflow");
  const options = getCodexWorkflowOptionDefinitions(workflowSrc, preferredLang);
  const optionMap = new Map(options.map((option) => [option.id, option.name]));
  return workflowIds.map((workflowId) => optionMap.get(workflowId)).filter((workflowName) => Boolean(workflowName));
}
function toProvidersList(providers) {
  return providers.map((provider) => ({ name: provider.name || provider.id, value: provider.id }));
}
function createApiConfigChoices(providers, currentProvider, isCommented) {
  const choices = [];
  const isOfficialMode = !currentProvider || isCommented;
  choices.push({
    name: isOfficialMode ? `${a.green("\u25CF ")}${i18n.t("codex:useOfficialLogin")} ${a.yellow("(\u5F53\u524D)")}` : `  ${i18n.t("codex:useOfficialLogin")}`,
    value: "official"
  });
  providers.forEach((provider) => {
    const isCurrent = currentProvider === provider.id && !isCommented;
    choices.push({
      name: isCurrent ? `${a.green("\u25CF ")}${provider.name} - ${a.gray(provider.id)} ${a.yellow("(\u5F53\u524D)")}` : `  ${provider.name} - ${a.gray(provider.id)}`,
      value: provider.id
    });
  });
  return choices;
}
async function applyCustomApiConfig(customApiConfig) {
  const { type, token, baseUrl, model } = customApiConfig;
  const backupPath = backupCodexComplete();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  const existingConfig = readCodexConfig();
  const existingAuth = readJsonConfig(CODEX_AUTH_FILE, { defaultValue: {} }) || {};
  const providers = [];
  const authEntries = { ...existingAuth };
  const providerId = type === "auth_token" ? "official-auth-token" : "custom-api-key";
  const providerName = type === "auth_token" ? "Official Auth Token" : "Custom API Key";
  const existingProvider = existingConfig?.providers.find((p) => p.id === providerId);
  providers.push({
    id: providerId,
    name: providerName,
    baseUrl: baseUrl || existingProvider?.baseUrl || "https://api.anthropic.com",
    wireApi: existingProvider?.wireApi || "responses",
    tempEnvKey: existingProvider?.tempEnvKey || `${providerId.toUpperCase()}_API_KEY`,
    requiresOpenaiAuth: existingProvider?.requiresOpenaiAuth ?? false,
    model: model || existingProvider?.model
  });
  if (existingConfig?.providers) {
    providers.push(...existingConfig.providers.filter((p) => p.id !== providerId));
  }
  if (token) {
    authEntries[providerId] = token;
    authEntries.OPENAI_API_KEY = token;
  }
  const configData = {
    model: model || existingConfig?.model || "gpt-5-codex",
    // Prefer provided model, then existing, fallback default
    modelProvider: providerId,
    modelProviderCommented: false,
    providers,
    mcpServices: existingConfig?.mcpServices || [],
    features: existingConfig?.features,
    otherConfig: existingConfig?.otherConfig || []
  };
  writeCodexConfig(configData);
  writeJsonConfig(CODEX_AUTH_FILE, { ...authEntries, auth_mode: "apikey" });
  updateZcfConfig({ codeToolType: "codex" });
  console.log(a.green(`\u2714 ${i18n.t("codex:apiConfigured")}`));
}
async function configureCodexApi(options) {
  ensureI18nInitialized();
  const { skipPrompt = false, apiMode, customApiConfig } = options ?? {};
  const existingConfig = readCodexConfig();
  const existingAuth = readJsonConfig(CODEX_AUTH_FILE, { defaultValue: {} }) || {};
  if (skipPrompt) {
    if (apiMode === "skip") {
      return;
    }
    if (apiMode === "custom" && customApiConfig) {
      await applyCustomApiConfig(customApiConfig);
      return;
    }
    if (apiMode === "official") {
      const success = await switchToOfficialLogin();
      if (success) {
        updateZcfConfig({ codeToolType: "codex" });
      }
      return;
    }
  }
  const hasProviders = existingConfig?.providers && existingConfig.providers.length > 0;
  const modeChoices = [
    { name: i18n.t("codex:apiModeOfficial"), value: "official" },
    { name: i18n.t("codex:apiModeCustom"), value: "custom" }
  ];
  if (hasProviders) {
    modeChoices.push({ name: i18n.t("codex:configSwitchMode"), value: "switch" });
  }
  const { mode } = await inquirer.prompt([{
    type: "list",
    name: "mode",
    message: i18n.t("codex:apiModePrompt"),
    choices: addNumbersToChoices(modeChoices),
    default: "custom"
  }]);
  if (!mode) {
    console.log(a.yellow(i18n.t("common:cancelled")));
    return;
  }
  if (mode === "official") {
    const success = await switchToOfficialLogin();
    if (success) {
      updateZcfConfig({ codeToolType: "codex" });
    }
    return;
  }
  if (mode === "switch") {
    if (!hasProviders) {
      console.log(a.yellow(i18n.t("codex:noProvidersAvailable")));
      return;
    }
    const currentProvider = existingConfig?.modelProvider;
    const isCommented = existingConfig?.modelProviderCommented;
    const choices = createApiConfigChoices(existingConfig.providers, currentProvider, isCommented);
    const { selectedConfig } = await inquirer.prompt([{
      type: "list",
      name: "selectedConfig",
      message: i18n.t("codex:apiConfigSwitchPrompt"),
      choices: addNumbersToChoices(choices)
    }]);
    if (!selectedConfig) {
      console.log(a.yellow(i18n.t("common:cancelled")));
      return;
    }
    let success = false;
    if (selectedConfig === "official") {
      success = await switchToOfficialLogin();
    } else {
      success = await switchToProvider(selectedConfig);
    }
    if (success) {
      updateZcfConfig({ codeToolType: "codex" });
    }
    return;
  }
  const managementMode = detectConfigManagementMode();
  if (managementMode.mode === "management" && managementMode.hasProviders) {
    const { default: { configureIncrementalManagement } } = await import('./codex-config-switch.mjs');
    await configureIncrementalManagement();
    return;
  }
  const backupPath = backupCodexComplete();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  const providers = [];
  const authEntries = {};
  const existingMap = new Map(existingConfig?.providers.map((provider) => [provider.id, provider]));
  const currentSessionProviders = /* @__PURE__ */ new Map();
  let addMore = true;
  const existingValues = existingMap.size ? Array.from(existingMap.values()) : [];
  const firstExisting = existingValues.length === 1 ? existingValues[0] : void 0;
  while (addMore) {
    const { getApiProviders } = await import('./api-providers.mjs');
    const apiProviders = getApiProviders("codex");
    const providerChoices = [
      { name: i18n.t("api:customProvider"), value: "custom" },
      ...apiProviders.map((p) => ({ name: p.name, value: p.id }))
    ];
    const { selectedProvider: selectedProvider2 } = await inquirer.prompt([{
      type: "list",
      name: "selectedProvider",
      message: i18n.t("api:selectApiProvider"),
      choices: addNumbersToChoices(providerChoices)
    }]);
    let prefilledBaseUrl;
    let prefilledWireApi;
    let prefilledModel;
    if (selectedProvider2 !== "custom") {
      const provider = apiProviders.find((p) => p.id === selectedProvider2);
      if (provider?.codex) {
        prefilledBaseUrl = provider.codex.baseUrl;
        prefilledWireApi = provider.codex.wireApi;
        prefilledModel = provider.codex.defaultModel;
        console.log(a.gray(i18n.t("api:providerSelected", { name: provider.name })));
      }
    }
    const answers = await inquirer.prompt([
      {
        type: "input",
        name: "providerName",
        message: i18n.t("codex:providerNamePrompt"),
        default: selectedProvider2 !== "custom" ? apiProviders.find((p) => p.id === selectedProvider2)?.name : firstExisting?.name,
        validate: (input) => {
          const sanitized = sanitizeProviderName(input);
          if (!sanitized)
            return i18n.t("codex:providerNameRequired");
          if (sanitized !== input.trim())
            return i18n.t("codex:providerNameInvalid");
          return true;
        }
      },
      {
        type: "input",
        name: "baseUrl",
        message: i18n.t("codex:providerBaseUrlPrompt"),
        default: prefilledBaseUrl || ((answers2) => existingMap.get(answers2.providerId)?.baseUrl || "https://api.openai.com/v1"),
        when: () => selectedProvider2 === "custom",
        validate: (input) => !!input || i18n.t("codex:providerBaseUrlRequired")
      },
      {
        type: "list",
        name: "wireApi",
        message: i18n.t("codex:providerProtocolPrompt"),
        choices: [
          { name: i18n.t("codex:protocolResponses"), value: "responses" },
          { name: i18n.t("codex:protocolChat"), value: "chat" }
        ],
        default: prefilledWireApi || ((answers2) => existingMap.get(sanitizeProviderName(answers2.providerName))?.wireApi || "responses"),
        when: () => selectedProvider2 === "custom"
        // Only ask if custom
      },
      {
        type: "input",
        name: "apiKey",
        message: selectedProvider2 !== "custom" ? i18n.t("api:enterProviderApiKey", { provider: apiProviders.find((p) => p.id === selectedProvider2)?.name || selectedProvider2 }) : i18n.t("codex:providerApiKeyPrompt"),
        validate: (input) => !!input || i18n.t("codex:providerApiKeyRequired")
      }
    ]);
    let customModel;
    if (selectedProvider2 === "custom") {
      const { model } = await inquirer.prompt([{
        type: "input",
        name: "model",
        message: `${i18n.t("configuration:enterCustomModel")}${i18n.t("common:emptyToSkip")}`,
        default: "gpt-5-codex"
      }]);
      if (model.trim()) {
        customModel = model.trim();
      }
    }
    const providerId = sanitizeProviderName(answers.providerName);
    const tempEnvKey = `${providerId.toUpperCase().replace(/-/g, "_")}_API_KEY`;
    const existingProvider = existingMap.get(providerId);
    const sessionProvider = currentSessionProviders.get(providerId);
    if (existingProvider || sessionProvider) {
      const sourceType = existingProvider ? "existing" : "session";
      const sourceProvider = existingProvider || sessionProvider;
      const shouldOverwrite = await promptBoolean({
        message: i18n.t("codex:providerDuplicatePrompt", {
          name: sourceProvider.name,
          source: sourceType === "existing" ? i18n.t("codex:existingConfig") : i18n.t("codex:currentSession")
        }),
        defaultValue: false
      });
      if (!shouldOverwrite) {
        console.log(a.yellow(i18n.t("codex:providerDuplicateSkipped")));
        continue;
      }
      if (sessionProvider) {
        currentSessionProviders.delete(providerId);
        const sessionIndex = providers.findIndex((p) => p.id === providerId);
        if (sessionIndex !== -1) {
          providers.splice(sessionIndex, 1);
        }
      }
    }
    const newProvider = {
      id: providerId,
      name: answers.providerName,
      baseUrl: selectedProvider2 === "custom" ? answers.baseUrl : prefilledBaseUrl,
      wireApi: selectedProvider2 === "custom" ? answers.wireApi || "responses" : prefilledWireApi,
      tempEnvKey,
      requiresOpenaiAuth: false,
      // Custom/third-party providers use their own API key, not OpenAI OAuth
      model: customModel || prefilledModel || "gpt-5-codex"
      // Use custom model, provider's default model, or fallback
    };
    providers.push(newProvider);
    currentSessionProviders.set(providerId, newProvider);
    authEntries[tempEnvKey] = answers.apiKey;
    const addAnother = await promptBoolean({
      message: i18n.t("codex:addProviderPrompt"),
      defaultValue: false
    });
    addMore = addAnother;
  }
  if (providers.length === 0) {
    console.log(a.yellow(i18n.t("codex:noProvidersConfigured")));
    return;
  }
  const { defaultProvider } = await inquirer.prompt([{
    type: "list",
    name: "defaultProvider",
    message: i18n.t("codex:selectDefaultProviderPrompt"),
    choices: addNumbersToChoices(toProvidersList(providers)),
    default: existingConfig?.modelProvider || providers[0].id
  }]);
  const selectedProvider = providers.find((provider) => provider.id === defaultProvider);
  if (selectedProvider) {
    const tempEnvKey = selectedProvider.tempEnvKey;
    const defaultApiKey = authEntries[tempEnvKey] ?? existingAuth[tempEnvKey] ?? null;
    if (defaultApiKey)
      authEntries.OPENAI_API_KEY = defaultApiKey;
  }
  writeCodexConfig({
    model: existingConfig?.model || null,
    modelProvider: defaultProvider,
    providers,
    mcpServices: existingConfig?.mcpServices || [],
    features: existingConfig?.features,
    otherConfig: existingConfig?.otherConfig || []
  });
  writeAuthFile(authEntries, "apikey");
  updateZcfConfig({ codeToolType: "codex" });
  console.log(a.green(i18n.t("codex:apiConfigured")));
}
const CODEX_PRESET_DEFINITIONS = {
  minimal: {
    id: "minimal",
    workflows: ["sixStepsWorkflow", "gitWorkflow"],
    mcpServices: ["context7", "open-websearch"]
  },
  dev: {
    id: "dev",
    workflows: ["sixStepsWorkflow", "essentialTools", "gitWorkflow"],
    mcpServices: ["context7", "open-websearch", "mcp-deepwiki"]
  },
  full: {
    id: "full",
    workflows: ["sixStepsWorkflow", "essentialTools", "gitWorkflow", "specFirstTDD", "refactoringMaster", "linearMethod", "interviewWorkflow"],
    mcpServices: ["context7", "open-websearch", "mcp-deepwiki", "serena"]
  }
};
function getCodexPresetDefinitions() {
  return Object.values(CODEX_PRESET_DEFINITIONS);
}
async function runCodexFullInit(options) {
  ensureI18nInitialized();
  await installCodexCli(options?.skipPrompt || false);
  const aiOutputLang = await runCodexWorkflowImportWithLanguageSelection(options);
  await configureCodexApi(options);
  await configureCodexMcp(options);
  return aiOutputLang;
}
function resolveCodexPresetTemplateLang() {
  const savedConfig = readZcfConfig();
  const savedTemplateLang = savedConfig?.templateLang;
  if (savedTemplateLang && SUPPORTED_LANGS.includes(savedTemplateLang)) {
    return savedTemplateLang;
  }
  const preferredLang = savedConfig?.preferredLang;
  if (preferredLang && SUPPORTED_LANGS.includes(preferredLang)) {
    return preferredLang;
  }
  return i18n.language === "zh-CN" ? "zh-CN" : "en";
}
async function applyCodexPreset(presetId) {
  ensureI18nInitialized();
  const preset = CODEX_PRESET_DEFINITIONS[presetId];
  if (!preset) {
    throw new Error(`Unknown Codex preset: ${presetId}`);
  }
  const savedConfig = readZcfConfig();
  const templateLang = resolveCodexPresetTemplateLang();
  const preferredLang = savedConfig?.preferredLang || templateLang;
  const aiOutputLang = savedConfig?.aiOutputLang || templateLang;
  const workflowIds = preset.workflows;
  const workflowNames = resolveCodexWorkflowNames(workflowIds, templateLang);
  const previousSingleBackupMode = process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP;
  process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP = "true";
  try {
    updateTomlConfig(ZCF_CONFIG_FILE, {
      general: {
        preferredLang,
        currentTool: "codex",
        templateLang,
        aiOutputLang
      }
    });
    await runCodexWorkflowImportWithLanguageSelection({
      skipPrompt: true,
      aiOutputLang,
      workflows: workflowIds
    });
    await configureCodexMcp({
      skipPrompt: true,
      workflows: workflowIds,
      mcpServices: preset.mcpServices
    });
  } finally {
    cachedSkipPromptBackup = null;
    if (previousSingleBackupMode === void 0) {
      delete process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP;
    } else {
      process__default.env.CCJK_CODEX_SKIP_PROMPT_SINGLE_BACKUP = previousSingleBackupMode;
    }
  }
  console.log(a.green(i18n.t("codex:presetApplied", { preset: i18n.t(`codex:presets.${preset.id}.name`) })));
  console.log(a.gray(i18n.t("codex:presetSummary", {
    workflows: workflowNames.join(", "),
    services: preset.mcpServices.join(", ")
  })));
}
function formatCodexModelSummary(model) {
  if (!model)
    return i18n.t("codex:efficiency.notConfigured");
  const labels = {
    "gpt-5-codex": "GPT-5-Codex",
    "codex-mini-latest": "codex-mini-latest",
    "gpt-5": "GPT-5"
  };
  return labels[model] || model;
}
function formatCodexStyleSummary(style) {
  if (!style)
    return i18n.t("codex:efficiency.notConfigured");
  const styleLabels = {
    "senior-architect": i18n.t("codex:efficiency.styleLabels.seniorArchitect"),
    "speed-coder": i18n.t("codex:efficiency.styleLabels.speedCoder"),
    "pair-programmer": i18n.t("codex:efficiency.styleLabels.pairProgrammer")
  };
  return styleLabels[style] || style;
}
function getInstalledCodexPromptCount() {
  if (!exists(CODEX_PROMPTS_DIR)) {
    return 0;
  }
  return readdirSync(CODEX_PROMPTS_DIR, { withFileTypes: true }).filter((entry) => entry.isFile() && entry.name.endsWith(".md")).length;
}
function buildCodexEfficiencyChoices() {
  const codexConfig = readCodexConfig();
  const zcfConfig = readDefaultTomlConfig();
  const modelSummary = formatCodexModelSummary(codexConfig?.model);
  const mcpSummary = codexConfig?.mcpServices?.length ?? 0;
  const workflowSummary = getInstalledCodexPromptCount();
  const promptSummary = formatCodexStyleSummary(zcfConfig?.codex?.systemPromptStyle);
  const languageSummary = zcfConfig?.general?.aiOutputLang || zcfConfig?.general?.preferredLang || i18n.t("codex:efficiency.notConfigured");
  return addNumbersToChoices([
    {
      name: `${i18n.t("codex:efficiency.actions.quickOptimize.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.quickOptimize.description")}`)}`,
      value: "quick-optimize"
    },
    {
      name: `${i18n.t("codex:efficiency.actions.presetBundles.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.presetBundles.description")}`)}`,
      value: "preset-bundles"
    },
    {
      name: `${i18n.t("codex:efficiency.actions.workflowPrompts.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.workflowPrompts.description", { count: workflowSummary })}`)}`,
      value: "workflow-prompts"
    },
    {
      name: `${i18n.t("codex:efficiency.actions.model.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.model.description", { model: modelSummary })}`)}`,
      value: "model"
    },
    {
      name: `${i18n.t("codex:efficiency.actions.docsMcp.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.docsMcp.description", { count: mcpSummary })}`)}`,
      value: "docs-mcp"
    },
    {
      name: `${i18n.t("codex:efficiency.actions.promptMemory.name")} ${a.gray(`- ${i18n.t("codex:efficiency.actions.promptMemory.description", { prompt: promptSummary, language: languageSummary })}`)}`,
      value: "prompt-memory"
    },
    {
      name: i18n.t("codex:efficiency.actions.back.name"),
      value: "back"
    }
  ]);
}
async function promptCodexPresetBundle() {
  const { preset } = await inquirer.prompt([{
    type: "list",
    name: "preset",
    message: i18n.t("codex:presetPrompt"),
    choices: addNumbersToChoices(getCodexPresetDefinitions().map((item) => ({
      name: `${i18n.t(`codex:presets.${item.id}.name`)} ${a.gray(`- ${i18n.t(`codex:presets.${item.id}.description`)}`)}`,
      value: item.id
    }))),
    default: "dev"
  }]);
  return preset || null;
}
function ensureCodexRecommendedModel(defaultModel = "gpt-5-codex") {
  const existingConfig = readCodexConfig();
  if (existingConfig?.model) {
    return null;
  }
  writeCodexConfig({
    model: defaultModel,
    modelProvider: existingConfig?.modelProvider || null,
    providers: existingConfig?.providers || [],
    mcpServices: existingConfig?.mcpServices || [],
    features: existingConfig?.features,
    otherConfig: existingConfig?.otherConfig || [],
    modelProviderCommented: existingConfig?.modelProviderCommented
  });
  return defaultModel;
}
async function runCodexQuickOptimize() {
  await applyCodexPreset("dev");
  const appliedModel = ensureCodexRecommendedModel();
  if (appliedModel) {
    console.log(a.green(i18n.t("codex:efficiency.quickOptimizeModelApplied", { model: appliedModel })));
  } else {
    const currentModel = readCodexConfig()?.model;
    if (currentModel) {
      console.log(a.gray(i18n.t("codex:efficiency.quickOptimizeModelKept", { model: formatCodexModelSummary(currentModel) })));
    }
  }
}
async function configureCodexPresetFeature() {
  ensureI18nInitialized();
  while (true) {
    const { action } = await inquirer.prompt([{
      type: "list",
      name: "action",
      message: i18n.t("codex:efficiency.prompt"),
      choices: buildCodexEfficiencyChoices(),
      default: "quick-optimize"
    }]);
    if (!action || action === "back") {
      return;
    }
    if (action === "quick-optimize") {
      await runCodexQuickOptimize();
      continue;
    }
    if (action === "preset-bundles") {
      const preset = await promptCodexPresetBundle();
      if (preset) {
        await applyCodexPreset(preset);
      }
      continue;
    }
    if (action === "workflow-prompts") {
      await runCodexWorkflowSelection();
      continue;
    }
    if (action === "model") {
      const { configureCodexDefaultModelFeature: configureCodexDefaultModelFeature2 } = await import('./features.mjs');
      await configureCodexDefaultModelFeature2();
      continue;
    }
    if (action === "docs-mcp") {
      await configureCodexMcp();
      continue;
    }
    if (action === "prompt-memory") {
      const { configureCodexAiMemoryFeature: configureCodexAiMemoryFeature2 } = await import('./features.mjs');
      await configureCodexAiMemoryFeature2();
    }
  }
}
function ensureCodexAgentsLanguageDirective(aiOutputLang) {
  if (!exists(CODEX_AGENTS_FILE))
    return;
  const content = readFile(CODEX_AGENTS_FILE);
  const targetLabel = resolveCodexLanguageLabel(aiOutputLang);
  const directiveRegex = /\*\*Most Important:\s*Always respond in ([^*]+)\*\*/i;
  const existingMatch = directiveRegex.exec(content);
  if (existingMatch && normalizeLanguageLabel(existingMatch[1]) === normalizeLanguageLabel(targetLabel))
    return;
  let updatedContent = content.replace(/\*\*Most Important:\s*Always respond in [^*]+\*\*\s*/gi, "").trimEnd();
  if (updatedContent.length > 0 && !updatedContent.endsWith("\n"))
    updatedContent += "\n";
  updatedContent += `
**Most Important:Always respond in ${targetLabel}**
`;
  const backupPath = backupCodexAgents();
  if (backupPath)
    console.log(a.gray(getBackupMessage(backupPath)));
  writeFileAtomic(CODEX_AGENTS_FILE, updatedContent);
  console.log(a.gray(`  ${i18n.t("configuration:addedLanguageDirective")}: ${targetLabel}`));
}
function resolveCodexLanguageLabel(aiOutputLang) {
  const directive = AI_OUTPUT_LANGUAGES[aiOutputLang]?.directive;
  if (directive) {
    const match = directive.match(/Always respond in\s+(.+)/i);
    if (match)
      return match[1].trim();
  }
  if (typeof aiOutputLang === "string")
    return aiOutputLang;
  return "English";
}
function normalizeLanguageLabel(label) {
  return label.trim().toLowerCase();
}
async function runCodexUpdate(force = false, skipPrompt = false) {
  ensureI18nInitialized();
  console.log(a.bold.cyan(`
\u{1F50D} ${i18n.t("updater:checkingTools")}
`));
  const spinner = ora(i18n.t("updater:checkingVersion")).start();
  try {
    const { installed, currentVersion, latestVersion, needsUpdate } = await checkCodexUpdate();
    spinner.stop();
    if (!installed) {
      console.log(a.yellow(i18n.t("codex:notInstalled")));
      return false;
    }
    if (!needsUpdate && !force) {
      console.log(a.green(format(i18n.t("codex:upToDate"), { version: currentVersion || "" })));
      return true;
    }
    if (!latestVersion) {
      console.log(a.yellow(i18n.t("codex:cannotCheckVersion")));
      return false;
    }
    console.log(a.green(format(i18n.t("codex:currentVersion"), { version: currentVersion || "" })));
    console.log(a.green(format(i18n.t("codex:latestVersion"), { version: latestVersion })));
    if (!skipPrompt) {
      const confirm = await promptBoolean({
        message: i18n.t("codex:confirmUpdate"),
        defaultValue: true
      });
      if (!confirm) {
        console.log(a.gray(i18n.t("codex:updateSkipped")));
        return true;
      }
    } else {
      console.log(a.green(i18n.t("codex:autoUpdating")));
    }
    const updateSpinner = ora(i18n.t("codex:updating")).start();
    try {
      await executeCodexInstallation(true);
      updateSpinner.succeed(i18n.t("codex:updateSuccess"));
      return true;
    } catch (error) {
      updateSpinner.fail(i18n.t("codex:updateFailed"));
      console.error(a.red(error instanceof Error ? error.message : String(error)));
      return false;
    }
  } catch (error) {
    spinner.fail(i18n.t("codex:checkFailed"));
    console.error(a.red(error instanceof Error ? error.message : String(error)));
    return false;
  }
}
async function runCodexUninstall() {
  ensureI18nInitialized();
  const { CodexUninstaller } = await import('./codex-uninstaller.mjs');
  const zcfConfig = readZcfConfig();
  const preferredLang = zcfConfig?.preferredLang;
  const uninstallLang = preferredLang && SUPPORTED_LANGS.includes(preferredLang) ? preferredLang : "en";
  const uninstaller = new CodexUninstaller(uninstallLang);
  const { mode } = await inquirer.prompt([{
    type: "list",
    name: "mode",
    message: i18n.t("codex:uninstallModePrompt"),
    choices: addNumbersToChoices([
      { name: i18n.t("codex:uninstallModeComplete"), value: "complete" },
      { name: i18n.t("codex:uninstallModeCustom"), value: "custom" }
    ]),
    default: "complete"
  }]);
  if (!mode) {
    handleUninstallCancellation();
    return;
  }
  try {
    if (mode === "complete") {
      const confirm = await promptBoolean({
        message: i18n.t("codex:uninstallPrompt"),
        defaultValue: false
      });
      if (!confirm) {
        handleUninstallCancellation();
        return;
      }
      const result = await uninstaller.completeUninstall();
      displayUninstallResults([result]);
    } else if (mode === "custom") {
      const { items } = await inquirer.prompt([{
        type: "checkbox",
        name: "items",
        message: i18n.t("codex:customUninstallPrompt"),
        choices: addNumbersToChoices(getUninstallOptions())
      }]);
      if (!items || items.length === 0) {
        handleUninstallCancellation();
        return;
      }
      const results = await uninstaller.customUninstall(items);
      displayUninstallResults(results);
    }
    console.log(a.green(i18n.t("codex:uninstallSuccess")));
  } catch (error) {
    console.error(a.red(i18n.t("codex:errorDuringUninstall", { error: error.message })));
    throw error;
  }
}
function displayUninstallResults(results) {
  for (const result of results) {
    for (const item of result.removed) {
      console.log(a.green(`\u2714 ${i18n.t("codex:removedItem", { item })}`));
    }
    for (const config of result.removedConfigs) {
      console.log(a.green(`\u2714 ${i18n.t("codex:removedConfig", { config })}`));
    }
    for (const warning of result.warnings) {
      console.log(a.yellow(`\u26A0\uFE0F ${warning}`));
    }
    for (const error of result.errors) {
      console.log(a.red(`\u274C ${error}`));
    }
  }
}
async function listCodexProviders() {
  const config = readCodexConfig();
  return config?.providers || [];
}
async function switchToOfficialLogin() {
  ensureI18nInitialized();
  const existingConfig = readCodexConfig();
  if (!existingConfig) {
    console.log(a.red(i18n.t("codex:configNotFound")));
    return false;
  }
  const backupPath = backupCodexComplete();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  try {
    let preservedModelProvider = existingConfig.modelProvider;
    if (!preservedModelProvider) {
      try {
        const rawContent = readFile(CODEX_CONFIG_FILE);
        const parsedToml = parse(rawContent);
        if (typeof parsedToml.model_provider === "string" && parsedToml.model_provider.trim().length > 0)
          preservedModelProvider = parsedToml.model_provider.trim();
      } catch {
      }
    }
    const shouldCommentModelProvider = typeof preservedModelProvider === "string" && preservedModelProvider.length > 0;
    const updatedConfig = {
      ...existingConfig,
      modelProvider: shouldCommentModelProvider ? preservedModelProvider : existingConfig.modelProvider,
      modelProviderCommented: shouldCommentModelProvider ? true : existingConfig.modelProviderCommented
    };
    writeCodexConfig(updatedConfig);
    const auth = readJsonConfig(CODEX_AUTH_FILE, { defaultValue: {} }) || {};
    auth.OPENAI_API_KEY = null;
    auth.auth_mode = "chatgpt";
    writeJsonConfig(CODEX_AUTH_FILE, auth, { pretty: true });
    console.log(a.green(i18n.t("codex:officialConfigured")));
    return true;
  } catch (error) {
    console.error(a.red(i18n.t("codex:errorSwitchingToOfficialLogin", { error: error.message })));
    return false;
  }
}
async function switchToProvider(providerId) {
  ensureI18nInitialized();
  const existingConfig = readCodexConfig();
  if (!existingConfig) {
    console.log(a.red(i18n.t("codex:configNotFound")));
    return false;
  }
  const provider = existingConfig.providers.find((p) => p.id === providerId);
  if (!provider) {
    console.log(a.red(i18n.t("codex:providerNotFound", { provider: providerId })));
    return false;
  }
  const backupPath = backupCodexComplete();
  if (backupPath) {
    console.log(a.gray(getBackupMessage(backupPath)));
  }
  try {
    let targetModel = existingConfig.model;
    if (provider.model) {
      targetModel = provider.model;
    } else {
      const currentModel = existingConfig.model;
      const stableCodexModels = /* @__PURE__ */ new Set(["gpt-5", "gpt-5-codex", "codex-mini-latest"]);
      if (!currentModel || !stableCodexModels.has(currentModel)) {
        targetModel = "gpt-5-codex";
      }
    }
    const updatedConfig = {
      ...existingConfig,
      model: targetModel,
      modelProvider: providerId,
      modelProviderCommented: false
      // Ensure it's not commented
    };
    writeCodexConfig(updatedConfig);
    const auth = readJsonConfig(CODEX_AUTH_FILE, { defaultValue: {} }) || {};
    const envValue = auth[provider.tempEnvKey] || null;
    auth.OPENAI_API_KEY = envValue;
    auth.auth_mode = "apikey";
    writeJsonConfig(CODEX_AUTH_FILE, auth, { pretty: true });
    console.log(a.green(i18n.t("codex:providerSwitchSuccess", { provider: providerId })));
    return true;
  } catch (error) {
    console.error(a.red(i18n.t("codex:errorSwitchingProvider", { error: error.message })));
    return false;
  }
}
async function configureCodexDefaultModelFeature() {
  const { configureCodexDefaultModelFeature: configureEnhancedCodexDefaultModelFeature } = await import('./features.mjs');
  await configureEnhancedCodexDefaultModelFeature();
}
async function configureCodexAiMemoryFeature() {
  const { configureCodexAiMemoryFeature: configureEnhancedCodexAiMemoryFeature } = await import('./features.mjs');
  await configureEnhancedCodexAiMemoryFeature();
}

const codex = {
  __proto__: null,
  CODEX_DIR: CODEX_DIR,
  applyCodexPlatformCommand: applyCodexPlatformCommand,
  applyCodexPreset: applyCodexPreset,
  backupCodexAgents: backupCodexAgents,
  backupCodexComplete: backupCodexComplete,
  backupCodexConfig: backupCodexConfig,
  backupCodexFiles: backupCodexFiles,
  backupCodexPrompts: backupCodexPrompts,
  buildCodexGoalsFeatureConfigContent: buildCodexGoalsFeatureConfigContent,
  checkCodexUpdate: checkCodexUpdate,
  configureCodexAiMemoryFeature: configureCodexAiMemoryFeature,
  configureCodexApi: configureCodexApi,
  configureCodexDefaultModelFeature: configureCodexDefaultModelFeature,
  configureCodexMcp: configureCodexMcp,
  configureCodexPresetFeature: configureCodexPresetFeature,
  createBackupDirectory: createBackupDirectory,
  ensureEnvKeyMigration: ensureEnvKeyMigration,
  getBackupMessage: getBackupMessage,
  getCodexPresetDefinitions: getCodexPresetDefinitions,
  getCodexVersion: getCodexVersion,
  installCodexCli: installCodexCli,
  isCodexGoalsFeatureEnabled: isCodexGoalsFeatureEnabled,
  isCodexInstalled: isCodexInstalled,
  listCodexProviders: listCodexProviders,
  migrateEnvKeyInContent: migrateEnvKeyInContent,
  migrateEnvKeyToTempEnvKey: migrateEnvKeyToTempEnvKey,
  needsEnvKeyMigration: needsEnvKeyMigration,
  parseCodexConfig: parseCodexConfig,
  readCodexConfig: readCodexConfig,
  readCodexGoalsFeatureEnabled: readCodexGoalsFeatureEnabled,
  renderCodexConfig: renderCodexConfig,
  runCodexFullInit: runCodexFullInit,
  runCodexSystemPromptSelection: runCodexSystemPromptSelection,
  runCodexUninstall: runCodexUninstall,
  runCodexUpdate: runCodexUpdate,
  runCodexWorkflowImportWithLanguageSelection: runCodexWorkflowImportWithLanguageSelection,
  runCodexWorkflowSelection: runCodexWorkflowSelection,
  setCodexGoalsFeatureInContent: setCodexGoalsFeatureInContent,
  switchToOfficialLogin: switchToOfficialLogin,
  switchToProvider: switchToProvider,
  writeAuthFile: writeAuthFile,
  writeCodexConfig: writeCodexConfig
};

export { readCodexConfig as a, applyCodexPlatformCommand as b, switchToProvider as c, backupCodexComplete as d, writeAuthFile as e, detectConfigManagementMode as f, readCodexGoalsFeatureEnabled as g, buildCodexGoalsFeatureConfigContent as h, runCodexFullInit as i, runCodexUninstall as j, configureCodexAiMemoryFeature as k, listCodexProviders as l, configureCodexDefaultModelFeature as m, configureCodexMcp as n, configureCodexApi as o, configureCodexPresetFeature as p, runCodexWorkflowImportWithLanguageSelection as q, runCodexUpdate as r, switchToOfficialLogin as s, codex as t, writeCodexConfig as w };
