import { joinProviderModel } from "./model-id.js";
const OPEN_CODE_PROVIDER_IDS = ["anthropic", "openai", "google"];
const DEFAULT_AI_PROVIDER_ID = "openai";
const OPEN_CODE_PROVIDERS = {
  anthropic: {
    id: "anthropic",
    name: "Anthropic",
    envKeys: ["ANTHROPIC_API_KEY", "OPENCODE_ANTHROPIC_API_KEY"],
    defaultModel: "claude-haiku-4-5-20251001"
  },
  openai: {
    id: "openai",
    name: "OpenAI",
    envKeys: ["OPENAI_API_KEY", "OPENCODE_OPENAI_API_KEY"],
    defaultModel: "gpt-5-mini"
  },
  google: {
    id: "google",
    name: "Google",
    envKeys: ["GOOGLE_GENERATIVE_AI_API_KEY", "OPENCODE_GOOGLE_API_KEY"],
    defaultModel: "gemini-3-flash"
  }
};
function resolveAiProviderIdFromEnv(env = process.env, fallback = DEFAULT_AI_PROVIDER_ID) {
  const candidates = [env.OM_AI_PROVIDER, env.OPENCODE_PROVIDER];
  for (const candidate of candidates) {
    const normalized = normalizeToken(candidate)?.toLowerCase();
    if (normalized && isOpenCodeProviderId(normalized)) {
      return normalized;
    }
  }
  return fallback;
}
function normalizeToken(value) {
  if (typeof value !== "string") return null;
  const normalized = value.trim();
  return normalized.length > 0 ? normalized : null;
}
function parseModelToken(token) {
  const slashIndex = token.indexOf("/");
  if (slashIndex <= 0) {
    return {
      providerPrefix: null,
      modelId: token
    };
  }
  return {
    providerPrefix: token.slice(0, slashIndex).trim().toLowerCase(),
    modelId: token.slice(slashIndex + 1).trim()
  };
}
function isOpenCodeProviderId(value) {
  return OPEN_CODE_PROVIDER_IDS.includes(value);
}
function resolveOpenCodeProviderId(providerId, fallback = "anthropic") {
  const normalized = normalizeToken(providerId)?.toLowerCase();
  if (normalized && isOpenCodeProviderId(normalized)) {
    return normalized;
  }
  return fallback;
}
function resolveFirstConfiguredOpenCodeProvider(options) {
  const env = options?.env ?? process.env;
  const order = options?.order ?? OPEN_CODE_PROVIDER_IDS;
  for (const providerId of order) {
    if (isOpenCodeProviderConfigured(providerId, env)) {
      return providerId;
    }
  }
  return null;
}
function resolveOpenCodeProviderApiKey(providerId, env = process.env) {
  const provider = OPEN_CODE_PROVIDERS[providerId];
  for (const key of provider.envKeys) {
    const value = normalizeToken(env[key]);
    if (value) {
      return value;
    }
  }
  return null;
}
function requireOpenCodeProviderApiKey(providerId, env = process.env) {
  const apiKey = resolveOpenCodeProviderApiKey(providerId, env);
  if (apiKey) {
    return apiKey;
  }
  const provider = OPEN_CODE_PROVIDERS[providerId];
  const envKeysHint = provider.envKeys.join(" or ");
  throw new Error(
    `Missing API key for provider "${providerId}". Set ${envKeysHint} in your .env file.`
  );
}
function getOpenCodeProviderConfiguredEnvKey(providerId, env = process.env) {
  const provider = OPEN_CODE_PROVIDERS[providerId];
  for (const key of provider.envKeys) {
    if (normalizeToken(env[key])) {
      return key;
    }
  }
  return provider.envKeys[0];
}
function isOpenCodeProviderConfigured(providerId, env = process.env) {
  return resolveOpenCodeProviderApiKey(providerId, env) !== null;
}
function resolveOpenCodeModel(providerId, options) {
  const env = options?.env ?? process.env;
  const overrideModel = normalizeToken(options?.overrideModel);
  const omAiModel = normalizeToken(env.OM_AI_MODEL);
  const opencodeModel = normalizeToken(env.OPENCODE_MODEL);
  let source = "default";
  let selectedModel = OPEN_CODE_PROVIDERS[providerId].defaultModel;
  if (opencodeModel) {
    selectedModel = opencodeModel;
    source = "opencode_model";
  }
  if (omAiModel) {
    selectedModel = omAiModel;
    source = "om_ai_model";
  }
  if (overrideModel) {
    selectedModel = overrideModel;
    source = "override";
  }
  const parsed = parseModelToken(selectedModel);
  if (parsed.providerPrefix && parsed.providerPrefix !== providerId) {
    throw new Error(
      `Model "${selectedModel}" does not match configured provider "${providerId}"`
    );
  }
  const modelId = normalizeToken(parsed.modelId);
  if (!modelId) {
    throw new Error(`Model "${selectedModel}" is invalid`);
  }
  return {
    modelId,
    modelWithProvider: joinProviderModel(providerId, modelId),
    source
  };
}
export {
  DEFAULT_AI_PROVIDER_ID,
  OPEN_CODE_PROVIDERS,
  OPEN_CODE_PROVIDER_IDS,
  getOpenCodeProviderConfiguredEnvKey,
  isOpenCodeProviderConfigured,
  isOpenCodeProviderId,
  requireOpenCodeProviderApiKey,
  resolveAiProviderIdFromEnv,
  resolveFirstConfiguredOpenCodeProvider,
  resolveOpenCodeModel,
  resolveOpenCodeProviderApiKey,
  resolveOpenCodeProviderId
};
//# sourceMappingURL=opencode-provider.js.map
