import { ensureI18nInitialized, i18n } from './index2.mjs';
import { a as readCodexConfig, d as backupCodexComplete, w as writeCodexConfig, e as writeAuthFile } from './codex.mjs';
import 'node:fs';
import 'node:process';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import './index5.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import '../shared/ccjk.RyizuzOI.mjs';
import './index6.mjs';
import 'node:readline';
import 'stream';
import 'node:tty';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'node:path';
import 'node:os';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import './index8.mjs';
import '../shared/ccjk.DeWpAShp.mjs';
import '../shared/ccjk.CxpGa6MC.mjs';
import '../shared/ccjk.BBtCGd_g.mjs';
import './main.mjs';
import 'module';
import 'node:child_process';
import 'node:stream';
import './constants.mjs';
import './ccjk-config.mjs';
import './index3.mjs';
import './fs-operations.mjs';
import 'node:fs/promises';
import './json-config.mjs';
import './config.mjs';
import './claude-config.mjs';
import '../shared/ccjk.DDL-4C-k.mjs';
import './platform.mjs';
import '../shared/ccjk.BFQ7yr5S.mjs';
import './prompts.mjs';
import '../shared/ccjk.gDEDGD_t.mjs';
import '../shared/ccjk.DZ2LLOa-.mjs';

async function addProviderToExisting(provider, apiKey, allowOverwrite = false) {
  ensureI18nInitialized();
  try {
    const existingConfig = readCodexConfig();
    const existingProviderIndex = existingConfig?.providers.findIndex((p) => p.id === provider.id) ?? -1;
    if (existingProviderIndex !== -1 && !allowOverwrite) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.providerExists", { id: provider.id })
      };
    }
    let updatedConfig;
    if (!existingConfig) {
      updatedConfig = {
        model: provider.model || null,
        modelProvider: provider.id,
        providers: [provider],
        mcpServices: [],
        managed: true,
        features: { goals: true },
        otherConfig: []
      };
    } else if (existingProviderIndex !== -1) {
      const updatedProviders = [...existingConfig.providers];
      updatedProviders[existingProviderIndex] = provider;
      updatedConfig = {
        ...existingConfig,
        providers: updatedProviders,
        modelProvider: existingConfig.modelProvider || provider.id
      };
    } else {
      updatedConfig = {
        ...existingConfig,
        providers: [...existingConfig.providers, provider],
        modelProvider: existingConfig.modelProvider || provider.id
      };
    }
    let backupPath;
    if (existingConfig) {
      const backup = backupCodexComplete();
      if (!backup) {
        return {
          success: false,
          error: i18n.t("codex:providerManager.backupFailed")
        };
      }
      backupPath = backup || void 0;
    }
    writeCodexConfig(updatedConfig);
    const authEntries = {};
    authEntries[provider.tempEnvKey] = apiKey;
    writeAuthFile(authEntries);
    return {
      success: true,
      backupPath,
      addedProvider: provider
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : i18n.t("codex:providerManager.unknownError")
    };
  }
}
async function editExistingProvider(providerId, updates) {
  ensureI18nInitialized();
  try {
    const existingConfig = readCodexConfig();
    if (!existingConfig) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.noConfig")
      };
    }
    const providerIndex = existingConfig.providers.findIndex((p) => p.id === providerId);
    if (providerIndex === -1) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.providerNotFound", { id: providerId })
      };
    }
    const backupPath = backupCodexComplete();
    if (!backupPath) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.backupFailed")
      };
    }
    const updatedProvider = {
      ...existingConfig.providers[providerIndex],
      ...updates.name && { name: updates.name },
      ...updates.baseUrl && { baseUrl: updates.baseUrl },
      ...updates.wireApi && { wireApi: updates.wireApi },
      ...updates.model && { model: updates.model }
    };
    const updatedProviders = [...existingConfig.providers];
    updatedProviders[providerIndex] = updatedProvider;
    const updatedConfig = {
      ...existingConfig,
      providers: updatedProviders
    };
    writeCodexConfig(updatedConfig);
    if (updates.apiKey) {
      const authEntries = {};
      authEntries[updatedProvider.tempEnvKey] = updates.apiKey;
      writeAuthFile(authEntries);
    }
    return {
      success: true,
      backupPath,
      updatedProvider
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : i18n.t("codex:providerManager.unknownError")
    };
  }
}
async function deleteProviders(providerIds) {
  ensureI18nInitialized();
  try {
    const existingConfig = readCodexConfig();
    if (!existingConfig) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.noConfig")
      };
    }
    if (!providerIds || providerIds.length === 0) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.noProvidersSpecified")
      };
    }
    const notFoundProviders = providerIds.filter(
      (id) => !existingConfig.providers.some((p) => p.id === id)
    );
    if (notFoundProviders.length > 0) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.providersNotFound", {
          providers: notFoundProviders.join(", ")
        })
      };
    }
    const remainingProviders = existingConfig.providers.filter(
      (p) => !providerIds.includes(p.id)
    );
    if (remainingProviders.length === 0) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.cannotDeleteAll")
      };
    }
    const backupPath = backupCodexComplete();
    if (!backupPath) {
      return {
        success: false,
        error: i18n.t("codex:providerManager.backupFailed")
      };
    }
    let newDefaultProvider = existingConfig.modelProvider;
    if (providerIds.includes(existingConfig.modelProvider || "")) {
      newDefaultProvider = remainingProviders[0].id;
    }
    const updatedConfig = {
      ...existingConfig,
      modelProvider: newDefaultProvider,
      providers: remainingProviders
    };
    writeCodexConfig(updatedConfig);
    const result = {
      success: true,
      backupPath,
      deletedProviders: providerIds,
      remainingProviders
    };
    if (newDefaultProvider !== existingConfig.modelProvider) {
      result.newDefaultProvider = newDefaultProvider || void 0;
    }
    return result;
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : i18n.t("codex:providerManager.unknownError")
    };
  }
}

export { addProviderToExisting, deleteProviders, editExistingProvider };
