import { getEnvApiKey } from "@earendil-works/pi-ai";
import { getApiKeyForProvider } from "../config/schema.js";
import { resolveProviderApiKey } from "../models/catalog.js";
import { AuthManager } from "./auth_manager.js";
export function createCredentialResolver(options) {
    const authManager = new AuthManager(options.authStorage);
    return {
        getApiKey: async (provider, getOptions) => {
            const authKey = await authManager.getApiKey(provider, { sessionId: getOptions?.sessionId });
            if (authKey) {
                return authKey;
            }
            const config = options.getConfig();
            const extensionApiKey = resolveProviderApiKey({
                provider,
                apiKeys: config.apiKeys,
                env: process.env,
            });
            if (extensionApiKey) {
                return extensionApiKey;
            }
            const configKey = getApiKeyForProvider(config, provider);
            if (configKey) {
                return configKey;
            }
            return getEnvApiKey(provider);
        },
        noteProviderError: async (provider, getOptions) => {
            await authManager.noteProviderError(provider, {
                sessionId: getOptions?.sessionId,
                error: getOptions?.error,
            });
        },
    };
}
//# sourceMappingURL=credential_resolver.js.map