"use strict";
/**
 * Provider Token Refreshers
 *
 * Exports refresh functions for each OAuth provider.
 *
 * Refresh responsibility:
 * - CLIProxy-delegated: gemini, codex, agy, kiro, ghcp, qwen, iflow, kimi
 *   (CLIProxyAPIPlus handles refresh automatically in background)
 * - Not implemented: claude
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.refreshToken = exports.isRefreshDelegated = void 0;
const account_manager_1 = require("../../accounts/account-manager");
const provider_capabilities_1 = require("../../provider-capabilities");
function assertNever(value) {
    throw new Error(`Unhandled token refresh ownership: ${String(value)}`);
}
/**
 * Check if a provider's token refresh is delegated to CLIProxy
 */
function isRefreshDelegated(provider) {
    return (0, provider_capabilities_1.isRefreshDelegatedToCLIProxy)(provider);
}
exports.isRefreshDelegated = isRefreshDelegated;
/**
 * Refresh token for a specific provider and account
 * @param provider Provider to refresh
 * @param accountId Account ID used to refresh the correct provider token
 * @returns Refresh result with success status and optional error
 */
async function refreshToken(provider, accountId) {
    const normalizedAccountId = accountId.trim();
    if (!normalizedAccountId) {
        return {
            success: false,
            error: 'Account ID is required for token refresh',
        };
    }
    const hasAccount = (0, account_manager_1.getProviderAccounts)(provider).some((account) => account.id === normalizedAccountId);
    if (!hasAccount) {
        return {
            success: false,
            error: `Account not found for ${provider}: ${normalizedAccountId}`,
        };
    }
    const ownership = (0, provider_capabilities_1.getTokenRefreshOwnership)(provider);
    switch (ownership) {
        case 'cliproxy':
            // CLIProxyAPIPlus handles refresh for these providers automatically.
            // No action needed from CCS — report success with delegated flag.
            return { success: true, delegated: true };
        case 'ccs':
            return {
                success: false,
                error: `Token refresh not yet implemented for ${provider}`,
            };
        case 'unsupported':
            return {
                success: false,
                error: `Token refresh not yet implemented for ${provider}`,
            };
        default:
            return assertNever(ownership);
    }
}
exports.refreshToken = refreshToken;
//# sourceMappingURL=index.js.map