"use strict";
/**
 * Shared extended context helpers used by CLI + UI.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.likelySupportsClaudeExtendedContext = exports.isClaudeModelId = exports.applyExtendedContextPreferenceToAnthropicModels = exports.hasAnthropicExtendedContextEnabled = exports.stripModelConfigurationSuffixes = exports.isAnthropicModelEnvKey = exports.stripExtendedContextSuffix = exports.applyExtendedContextSuffix = exports.hasExtendedContextSuffix = exports.isNativeGeminiModel = exports.ANTHROPIC_MODEL_ENV_KEYS = exports.EXTENDED_CONTEXT_SUFFIX = void 0;
/** Extended context suffix recognized by Claude Code. */
exports.EXTENDED_CONTEXT_SUFFIX = '[1m]';
exports.ANTHROPIC_MODEL_ENV_KEYS = [
    'ANTHROPIC_MODEL',
    'ANTHROPIC_DEFAULT_OPUS_MODEL',
    'ANTHROPIC_DEFAULT_SONNET_MODEL',
    'ANTHROPIC_DEFAULT_HAIKU_MODEL',
];
const ANTHROPIC_MODEL_ENV_KEY_SET = new Set(exports.ANTHROPIC_MODEL_ENV_KEYS);
/** Check if model is a native Gemini model (auto-enabled behavior). */
function isNativeGeminiModel(modelId) {
    return modelId.toLowerCase().startsWith('gemini-');
}
exports.isNativeGeminiModel = isNativeGeminiModel;
/** Check if model already has [1m] suffix. */
function hasExtendedContextSuffix(model) {
    return model.toLowerCase().endsWith(exports.EXTENDED_CONTEXT_SUFFIX.toLowerCase());
}
exports.hasExtendedContextSuffix = hasExtendedContextSuffix;
/** Apply [1m] suffix to model if not already present. */
function applyExtendedContextSuffix(model) {
    if (!model)
        return model;
    if (hasExtendedContextSuffix(model))
        return model;
    return `${model}${exports.EXTENDED_CONTEXT_SUFFIX}`;
}
exports.applyExtendedContextSuffix = applyExtendedContextSuffix;
/** Strip [1m] suffix from model string. */
function stripExtendedContextSuffix(model) {
    if (!model)
        return model;
    return hasExtendedContextSuffix(model) ? model.slice(0, -exports.EXTENDED_CONTEXT_SUFFIX.length) : model;
}
exports.stripExtendedContextSuffix = stripExtendedContextSuffix;
/** True when key belongs to Anthropic model mapping state. */
function isAnthropicModelEnvKey(key) {
    return ANTHROPIC_MODEL_ENV_KEY_SET.has(key);
}
exports.isAnthropicModelEnvKey = isAnthropicModelEnvKey;
/** Strip transient config suffixes so model IDs can be checked against catalogs. */
function stripModelConfigurationSuffixes(modelId) {
    return stripExtendedContextSuffix(modelId.trim()).replace(/\([^)]+\)$/, '');
}
exports.stripModelConfigurationSuffixes = stripModelConfigurationSuffixes;
/** Whether any saved Anthropic model mapping explicitly requests [1m]. */
function hasAnthropicExtendedContextEnabled(env) {
    return exports.ANTHROPIC_MODEL_ENV_KEYS.some((key) => {
        const value = env[key];
        return typeof value === 'string' && hasExtendedContextSuffix(value);
    });
}
exports.hasAnthropicExtendedContextEnabled = hasAnthropicExtendedContextEnabled;
/** Apply or strip [1m] across Anthropic model mappings while honoring compatibility. */
function applyExtendedContextPreferenceToAnthropicModels(env, enabled, options = {}) {
    const nextEnv = { ...env };
    for (const key of exports.ANTHROPIC_MODEL_ENV_KEYS) {
        const value = nextEnv[key];
        if (typeof value !== 'string' || value.trim().length === 0) {
            continue;
        }
        const modelId = stripModelConfigurationSuffixes(value);
        const supported = options.supportsExtendedContext?.(modelId, key) ?? true;
        nextEnv[key] =
            enabled && supported ? applyExtendedContextSuffix(value) : stripExtendedContextSuffix(value);
    }
    return nextEnv;
}
exports.applyExtendedContextPreferenceToAnthropicModels = applyExtendedContextPreferenceToAnthropicModels;
/** Detect Claude model identifiers, regardless of transient [1m]/(thinking) suffixes. */
function isClaudeModelId(modelId) {
    return stripModelConfigurationSuffixes(modelId).toLowerCase().startsWith('claude-');
}
exports.isClaudeModelId = isClaudeModelId;
/**
 * Conservative Claude long-context support heuristic for generic API profile flows.
 * Haiku stays plain; Opus/Sonnet default to opt-in [1m].
 */
function likelySupportsClaudeExtendedContext(modelId) {
    const baseModel = stripModelConfigurationSuffixes(modelId).toLowerCase();
    return baseModel.startsWith('claude-') && !baseModel.startsWith('claude-haiku-');
}
exports.likelySupportsClaudeExtendedContext = likelySupportsClaudeExtendedContext;
//# sourceMappingURL=extended-context-utils.js.map