import { getApiKeyForCompany } from "./billing.js";
/**
 * Resolves platform-managed API keys for the cloud adapter.
 * In SaaS mode, zmeel provides its own API keys so users don't need to.
 */
export function getManagedApiKey(provider) {
    switch (provider) {
        case "glm":
            return process.env.ZMEEL_GLM_API_KEY?.trim() || null;
        case "anthropic":
            return process.env.ZMEEL_ANTHROPIC_API_KEY?.trim() || null;
        case "openai":
            return process.env.ZMEEL_OPENAI_API_KEY?.trim() || null;
        default:
            return null;
    }
}
/**
 * Injects a managed API key into the adapter config if the agent
 * is configured to use managed keys (apiKeySource === "managed" or unset).
 */
export function injectManagedKeyIfNeeded(adapterType, config) {
    if (adapterType !== "cloud")
        return config;
    const source = config.apiKeySource || "managed";
    if (source !== "managed")
        return config;
    const provider = config.provider || "glm";
    const key = getManagedApiKey(provider);
    if (!key)
        return config;
    return { ...config, apiKey: key };
}
/**
 * Subscription-aware version of injectManagedKeyIfNeeded.
 * Checks the company's subscription to determine whether to use
 * a BYOK key or the platform managed key.
 */
export async function injectApiKeyForCompany(db, companyId, adapterType, config) {
    if (adapterType !== "cloud")
        return config;
    const source = config.apiKeySource || "managed";
    if (source !== "managed")
        return config;
    const provider = config.provider || "anthropic";
    const key = await getApiKeyForCompany(db, companyId, provider);
    if (!key)
        return config;
    return { ...config, apiKey: key };
}
//# sourceMappingURL=managed-api-keys.js.map