import { fetchUserInfo as defaultFetchUserInfo } from '../app/services/user-context.js';
import { SHORTCUT_PROVIDER_ID } from '../model-ids.js';
export async function resolveShortcutUserInfo(authStorage, fetchUserInfo = defaultFetchUserInfo) {
    try {
        const accessToken = await authStorage.getApiKey(SHORTCUT_PROVIDER_ID);
        return accessToken ? fetchUserInfo(accessToken) : null;
    }
    catch {
        return null;
    }
}
export function applyShortcutUserInfoToLogger(clientLogger, userInfo) {
    if (!userInfo) {
        clientLogger.clearUserId();
        clientLogger.clearUserContext();
        return;
    }
    clientLogger.setUserId(userInfo.userId);
    clientLogger.setUserContext({
        planType: userInfo.plan,
        planTier: userInfo.proCreditsTier,
        teamId: userInfo.teamId,
        teamRole: userInfo.enterpriseRole,
        email: userInfo.email
    });
}
export async function refreshShortcutUserInfoLoggerContext(authStorage, clientLogger, fetchUserInfo = defaultFetchUserInfo) {
    const userInfo = await resolveShortcutUserInfo(authStorage, fetchUserInfo);
    applyShortcutUserInfoToLogger(clientLogger, userInfo);
    return userInfo;
}
export async function applyRuntimeApiKeyAndRefreshShortcutLoggerContext(options) {
    options.authStorage.setRuntimeApiKey(options.providerId, options.apiKey);
    if (options.providerId !== SHORTCUT_PROVIDER_ID) {
        return undefined;
    }
    return refreshShortcutUserInfoLoggerContext(options.authStorage, options.clientLogger, options.fetchUserInfo);
}
//# sourceMappingURL=shortcut-user-logger-context.js.map