import { getSecret } from '../connectors/secret-store.js';
import { getHostedMcpOAuthProviderId } from './hosted-oauth-id.js';
import { resolveHostedMcpOAuthTimeoutMs, supportsHostedMcpOAuth } from './oauth-discovery.js';
import { isHostedServerConfig, isStdioServerConfig } from './types.js';
function hasConfiguredHeaders(headers) {
    return !!headers && Object.keys(headers).length > 0;
}
export async function resolveMcpAuthStatus(serverName, config, options = {}) {
    if (isStdioServerConfig(config)) {
        return 'unsupported';
    }
    const staticHeaders = { ...(config.headers ?? {}) };
    const secretHeaders = config.connection ? getSecret(config.connection) : undefined;
    if (hasConfiguredHeaders(staticHeaders) || hasConfiguredHeaders(secretHeaders)) {
        return 'configured';
    }
    if (isHostedServerConfig(config)) {
        const discovery = options.discovery ?? 'cached';
        const providerId = getHostedMcpOAuthProviderId(serverName, config);
        if (options.authStorage?.get(providerId)?.type === 'oauth') {
            return 'oauth';
        }
        if (config.oauth ||
            (discovery !== 'never' && options.mcpOAuthStore?.getDiscoveryState(providerId))) {
            return 'not_logged_in';
        }
        if (discovery === 'network' &&
            (await supportsHostedMcpOAuth(serverName, config, options.mcpOAuthStore, {
                timeoutMs: resolveHostedMcpOAuthTimeoutMs(config)
            }).catch(() => false))) {
            return 'not_logged_in';
        }
        return 'unsupported';
    }
    return 'unsupported';
}
export async function getMcpAuthStatus(serverName, config, authStorage, mcpOAuthStore) {
    return resolveMcpAuthStatus(serverName, config, {
        authStorage,
        mcpOAuthStore,
        discovery: 'cached'
    });
}
//# sourceMappingURL=auth-status.js.map