/**
 * Model and thinking level management — pure logic.
 *
 * Extracted from AgentSession. All functions are pure (no side effects).
 * AgentSession delegates to these for cycling math and level clamping,
 * then handles persistence and extension events itself.
 */
import { modelsAreEqual, supportsXhigh } from '../../ai/models.js';
// ============================================================================
// Constants
// ============================================================================
/** Standard thinking levels (ordered low → high) */
export const THINKING_LEVELS = ['off', 'minimal', 'low', 'medium', 'high'];
/** Thinking levels including xhigh (for supported models) */
export const THINKING_LEVELS_WITH_XHIGH = [
    'off',
    'minimal',
    'low',
    'medium',
    'high',
    'xhigh'
];
// ============================================================================
// Thinking Level Logic
// ============================================================================
/** Check if a model supports thinking/reasoning. */
export function supportsThinking(model) {
    return !!model?.reasoning;
}
/** Check if a model supports xhigh thinking. */
export function supportsXhighThinking(model) {
    return model ? supportsXhigh(model) : false;
}
/** Get available thinking levels for a given model. */
export function getAvailableThinkingLevels(model) {
    if (!supportsThinking(model))
        return ['off'];
    return supportsXhighThinking(model) ? THINKING_LEVELS_WITH_XHIGH : THINKING_LEVELS;
}
/**
 * Clamp a thinking level to the nearest available level.
 * Searches forward first (higher levels), then backward (lower levels).
 */
export function clampThinkingLevel(level, availableLevels) {
    if (availableLevels.includes(level))
        return level;
    const ordered = THINKING_LEVELS_WITH_XHIGH;
    const available = new Set(availableLevels);
    const requestedIndex = ordered.indexOf(level);
    if (requestedIndex === -1) {
        return availableLevels[0] ?? 'off';
    }
    // Search forward (higher levels)
    for (let i = requestedIndex; i < ordered.length; i++) {
        const candidate = ordered[i];
        if (available.has(candidate))
            return candidate;
    }
    // Search backward (lower levels)
    for (let i = requestedIndex - 1; i >= 0; i--) {
        const candidate = ordered[i];
        if (available.has(candidate))
            return candidate;
    }
    return availableLevels[0] ?? 'off';
}
/**
 * Compute the next thinking level in a cycle.
 * @returns The next level, or undefined if model doesn't support thinking.
 */
export function cycleThinkingLevel(currentLevel, model) {
    if (!supportsThinking(model))
        return undefined;
    const levels = getAvailableThinkingLevels(model);
    const currentIndex = levels.indexOf(currentLevel);
    const nextIndex = (currentIndex + 1) % levels.length;
    return levels[nextIndex];
}
// ============================================================================
// Model Cycling Logic
// ============================================================================
/**
 * Compute the next model in the cycle.
 * Pure index math — does not mutate state.
 * @returns The next model, or undefined if ≤1 model available.
 */
export function computeNextModel(availableModels, currentModel, direction) {
    if (availableModels.length <= 1)
        return undefined;
    let currentIndex = currentModel
        ? availableModels.findIndex((m) => modelsAreEqual(m, currentModel))
        : -1;
    if (currentIndex === -1)
        currentIndex = 0;
    const len = availableModels.length;
    const nextIndex = direction === 'forward' ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;
    return availableModels[nextIndex];
}
//# sourceMappingURL=session-models.js.map