"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isQuotaSupportedProvider = exports.mapExternalProviderName = exports.getProviderTokenTypeValues = exports.getProviderAuthFilePrefixes = exports.isRefreshDelegatedToCLIProxy = exports.getTokenRefreshOwnership = exports.getCLIProxyAuthUrlProviderName = exports.getCLIProxyCallbackProviderName = exports.getOAuthCallbackPort = exports.getOAuthFlowType = exports.getDeviceCodeVerificationProviders = exports.isBrowserUrlAuthProvider = exports.getProvidersByOAuthFlow = exports.getProviderDescription = exports.getProviderDisplayName = exports.getProviderCapabilities = exports.isCLIProxyProvider = exports.buildProviderAliasMap = exports.buildProviderMap = exports.QUOTA_PROVIDER_HELP_TEXT = exports.QUOTA_PROVIDER_OPTION_VALUES = exports.QUOTA_SUPPORTED_PROVIDER_IDS = exports.BROWSER_URL_AUTH_PROVIDER_IDS = exports.CLIPROXY_PROVIDER_IDS = exports.PROVIDER_CAPABILITIES = void 0;
exports.PROVIDER_CAPABILITIES = {
    gemini: {
        displayName: 'Google Gemini',
        description: 'Gemini Pro/Flash models',
        oauthFlow: 'authorization_code',
        callbackPort: 8085,
        callbackProviderName: 'gemini',
        authUrlProviderName: 'gemini-cli',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['gemini-', 'google-'],
        tokenTypeValues: ['gemini'],
        aliases: ['gemini-cli'],
    },
    codex: {
        displayName: 'OpenAI Codex',
        description: 'GPT-4 and codex models',
        oauthFlow: 'authorization_code',
        callbackPort: 1455,
        callbackProviderName: 'codex',
        authUrlProviderName: 'codex',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['codex-', 'openai-'],
        tokenTypeValues: ['codex'],
        aliases: [],
    },
    agy: {
        displayName: 'Antigravity',
        description: 'Antigravity AI models',
        oauthFlow: 'authorization_code',
        callbackPort: 51121,
        callbackProviderName: 'antigravity',
        authUrlProviderName: 'antigravity',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['antigravity-', 'agy-'],
        tokenTypeValues: ['antigravity'],
        aliases: ['antigravity'],
    },
    qwen: {
        displayName: 'Alibaba Qwen',
        description: 'Qwen Code models',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'qwen',
        authUrlProviderName: 'qwen',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['qwen-'],
        tokenTypeValues: ['qwen'],
        aliases: [],
    },
    iflow: {
        displayName: 'iFlow',
        description: 'iFlow AI models',
        oauthFlow: 'authorization_code',
        callbackPort: 11451,
        callbackProviderName: 'iflow',
        authUrlProviderName: 'iflow',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['iflow-'],
        tokenTypeValues: ['iflow'],
        aliases: [],
    },
    kiro: {
        displayName: 'Kiro (AWS)',
        description: 'AWS CodeWhisperer models',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'kiro',
        authUrlProviderName: 'kiro',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['kiro-', 'aws-', 'codewhisperer-'],
        tokenTypeValues: ['kiro', 'codewhisperer'],
        aliases: ['codewhisperer'],
    },
    ghcp: {
        displayName: 'GitHub Copilot (OAuth)',
        description: 'Deprecated GitHub Copilot compatibility via OAuth',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'copilot',
        authUrlProviderName: 'github',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['github-copilot-', 'copilot-', 'gh-'],
        tokenTypeValues: ['github-copilot', 'copilot'],
        aliases: ['github-copilot', 'copilot'],
    },
    claude: {
        displayName: 'Claude (Anthropic)',
        description: 'Claude Opus/Sonnet models',
        oauthFlow: 'authorization_code',
        callbackPort: 54545,
        callbackProviderName: 'anthropic',
        authUrlProviderName: 'anthropic',
        refreshOwnership: 'unsupported',
        authFilePrefixes: ['claude-', 'anthropic-'],
        tokenTypeValues: ['claude', 'anthropic'],
        aliases: ['anthropic'],
    },
    kimi: {
        displayName: 'Kimi (Moonshot)',
        description: 'Moonshot AI K2/K2.5 models',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'kimi',
        authUrlProviderName: 'kimi',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['kimi-'],
        tokenTypeValues: ['kimi'],
        aliases: ['moonshot'],
    },
    cursor: {
        displayName: 'Cursor',
        description: 'Cursor browser-authenticated provider',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'cursor',
        authUrlProviderName: 'cursor',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['cursor.', 'cursor-'],
        tokenTypeValues: ['cursor'],
        aliases: [],
    },
    gitlab: {
        displayName: 'GitLab Duo',
        description: 'GitLab Duo with OAuth or PAT auth',
        oauthFlow: 'authorization_code',
        callbackPort: 17171,
        callbackProviderName: 'gitlab',
        authUrlProviderName: 'gitlab',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['gitlab-'],
        tokenTypeValues: ['gitlab'],
        aliases: ['gitlab-duo'],
    },
    codebuddy: {
        displayName: 'CodeBuddy (Tencent)',
        description: 'Tencent CodeBuddy AI assistant',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'codebuddy',
        authUrlProviderName: 'codebuddy',
        refreshOwnership: 'cliproxy',
        authFilePrefixes: ['codebuddy-'],
        tokenTypeValues: ['codebuddy'],
        aliases: ['tencent'],
    },
    kilo: {
        displayName: 'Kilo AI',
        description: 'Kilo AI coding assistant',
        oauthFlow: 'device_code',
        callbackPort: null,
        callbackProviderName: 'kilo',
        authUrlProviderName: 'kilo',
        refreshOwnership: 'unsupported',
        authFilePrefixes: ['kilo-'],
        tokenTypeValues: ['kilo'],
        aliases: [],
    },
};
exports.CLIPROXY_PROVIDER_IDS = Object.freeze(Object.keys(exports.PROVIDER_CAPABILITIES));
exports.BROWSER_URL_AUTH_PROVIDER_IDS = Object.freeze([
    'cursor',
]);
const BROWSER_URL_AUTH_PROVIDER_SET = new Set(exports.BROWSER_URL_AUTH_PROVIDER_IDS);
/** Providers currently supported by quota status fetchers. */
exports.QUOTA_SUPPORTED_PROVIDER_IDS = Object.freeze([
    'agy',
    'codex',
    'claude',
    'gemini',
    'ghcp',
]);
const QUOTA_SUPPORTED_PROVIDER_SET = new Set(exports.QUOTA_SUPPORTED_PROVIDER_IDS);
exports.QUOTA_PROVIDER_OPTION_VALUES = Object.freeze([
    ...exports.QUOTA_SUPPORTED_PROVIDER_IDS,
    ...exports.QUOTA_SUPPORTED_PROVIDER_IDS.flatMap((provider) => exports.PROVIDER_CAPABILITIES[provider].aliases),
    'all',
].filter((value, index, values) => values.indexOf(value) === index));
exports.QUOTA_PROVIDER_HELP_TEXT = exports.QUOTA_PROVIDER_OPTION_VALUES.join(', ');
function buildProviderMap(valueFor) {
    return exports.CLIPROXY_PROVIDER_IDS.reduce((acc, provider) => {
        acc[provider] = valueFor(provider);
        return acc;
    }, {});
}
exports.buildProviderMap = buildProviderMap;
const PROVIDER_ID_SET = new Set(exports.CLIPROXY_PROVIDER_IDS);
function buildProviderAliasMap(capabilities = exports.PROVIDER_CAPABILITIES) {
    const aliasMap = new Map();
    const providers = Object.keys(capabilities);
    const registerAlias = (alias, provider) => {
        const normalized = alias.trim().toLowerCase();
        if (!normalized) {
            return;
        }
        const existingProvider = aliasMap.get(normalized);
        if (existingProvider && existingProvider !== provider) {
            throw new Error(`Provider alias collision for "${normalized}": ${existingProvider} and ${provider}`);
        }
        aliasMap.set(normalized, provider);
    };
    for (const provider of providers) {
        registerAlias(provider, provider);
        for (const alias of capabilities[provider].aliases) {
            registerAlias(alias, provider);
        }
    }
    return aliasMap;
}
exports.buildProviderAliasMap = buildProviderAliasMap;
const PROVIDER_ALIAS_MAP = buildProviderAliasMap();
function isCLIProxyProvider(provider) {
    return PROVIDER_ID_SET.has(provider);
}
exports.isCLIProxyProvider = isCLIProxyProvider;
function getProviderCapabilities(provider) {
    return exports.PROVIDER_CAPABILITIES[provider];
}
exports.getProviderCapabilities = getProviderCapabilities;
function getProviderDisplayName(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].displayName;
}
exports.getProviderDisplayName = getProviderDisplayName;
function getProviderDescription(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].description;
}
exports.getProviderDescription = getProviderDescription;
function getProvidersByOAuthFlow(flowType) {
    return exports.CLIPROXY_PROVIDER_IDS.filter((provider) => exports.PROVIDER_CAPABILITIES[provider].oauthFlow === flowType);
}
exports.getProvidersByOAuthFlow = getProvidersByOAuthFlow;
function isBrowserUrlAuthProvider(provider) {
    return BROWSER_URL_AUTH_PROVIDER_SET.has(provider);
}
exports.isBrowserUrlAuthProvider = isBrowserUrlAuthProvider;
function getDeviceCodeVerificationProviders() {
    return getProvidersByOAuthFlow('device_code').filter((provider) => !isBrowserUrlAuthProvider(provider));
}
exports.getDeviceCodeVerificationProviders = getDeviceCodeVerificationProviders;
function getOAuthFlowType(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].oauthFlow;
}
exports.getOAuthFlowType = getOAuthFlowType;
function getOAuthCallbackPort(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].callbackPort;
}
exports.getOAuthCallbackPort = getOAuthCallbackPort;
function getCLIProxyCallbackProviderName(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].callbackProviderName;
}
exports.getCLIProxyCallbackProviderName = getCLIProxyCallbackProviderName;
function getCLIProxyAuthUrlProviderName(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].authUrlProviderName;
}
exports.getCLIProxyAuthUrlProviderName = getCLIProxyAuthUrlProviderName;
function getTokenRefreshOwnership(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].refreshOwnership;
}
exports.getTokenRefreshOwnership = getTokenRefreshOwnership;
function isRefreshDelegatedToCLIProxy(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].refreshOwnership === 'cliproxy';
}
exports.isRefreshDelegatedToCLIProxy = isRefreshDelegatedToCLIProxy;
function getProviderAuthFilePrefixes(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].authFilePrefixes;
}
exports.getProviderAuthFilePrefixes = getProviderAuthFilePrefixes;
function getProviderTokenTypeValues(provider) {
    return exports.PROVIDER_CAPABILITIES[provider].tokenTypeValues;
}
exports.getProviderTokenTypeValues = getProviderTokenTypeValues;
function mapExternalProviderName(providerName) {
    const normalized = providerName.trim().toLowerCase();
    if (!normalized) {
        return null;
    }
    return PROVIDER_ALIAS_MAP.get(normalized) ?? null;
}
exports.mapExternalProviderName = mapExternalProviderName;
function isQuotaSupportedProvider(provider) {
    return QUOTA_SUPPORTED_PROVIDER_SET.has(provider);
}
exports.isQuotaSupportedProvider = isQuotaSupportedProvider;
//# sourceMappingURL=provider-capabilities.js.map