import React from 'react';
import { InfoMessage } from '../../../components/message-box.js';
import { DELAY_COMMAND_COMPLETE_MS } from '../../../constants.js';
import { resetSessionContextLimit, resolveModelContextLimit, setSessionContextLimit, } from '../../../models/index.js';
import { generateKey } from '../../../session/key-generator.js';
import { errorMsg, infoMsg, successMsg } from '../../../utils/message-factory.js';
/**
 * Parses a context limit value string, supporting k/K suffix.
 * e.g. "8192" -> 8192, "128k" -> 128000, "128K" -> 128000
 */
export function parseContextLimit(value) {
    const trimmed = value.trim().toLowerCase();
    let multiplier = 1;
    let numStr = trimmed;
    if (trimmed.endsWith('k')) {
        multiplier = 1000;
        numStr = trimmed.slice(0, -1);
    }
    const parsed = Number.parseFloat(numStr);
    if (Number.isNaN(parsed) || parsed <= 0) {
        return null;
    }
    return Math.round(parsed * multiplier);
}
/**
 * Handles /context-max command. Returns true if handled.
 */
export async function handleContextMaxCommand(commandParts, options) {
    const { onAddToChatQueue, onCommandComplete, model, providerConfig } = options;
    if (commandParts[0] !== 'context-max') {
        return false;
    }
    const args = commandParts.slice(1);
    if (args[0] === '--reset') {
        resetSessionContextLimit();
        onAddToChatQueue(successMsg('Session context limit override cleared.', 'context-max-reset'));
        setTimeout(() => onCommandComplete?.(), DELAY_COMMAND_COMPLETE_MS);
        return true;
    }
    if (args.length > 0) {
        const limit = parseContextLimit(args[0]);
        if (limit === null) {
            onAddToChatQueue(errorMsg('Invalid context limit. Use a positive number, e.g. /context-max 8192 or /context-max 128k', 'context-max-error'));
            setTimeout(() => onCommandComplete?.(), DELAY_COMMAND_COMPLETE_MS);
            return true;
        }
        setSessionContextLimit(limit);
        onAddToChatQueue(successMsg(`Session context limit set to ${limit.toLocaleString()} tokens.`, 'context-max-set'));
        setTimeout(() => onCommandComplete?.(), DELAY_COMMAND_COMPLETE_MS);
        return true;
    }
    const resolved = await resolveModelContextLimit(model, {
        providerConfig: providerConfig ?? undefined,
    });
    const sourceLabels = {
        session: 'session override',
        'provider-model-config': 'provider model config',
        'provider-config': 'provider config',
        env: 'NANOCODER_CONTEXT_LIMIT env',
        'model-lookup': 'model lookup',
        unknown: 'unknown',
    };
    if (resolved.limit !== null) {
        onAddToChatQueue(React.createElement(InfoMessage, {
            key: generateKey('context-max-info'),
            message: `Context limit: ${resolved.limit.toLocaleString()} tokens (${sourceLabels[resolved.source]})`,
            hideBox: true,
            marginTop: 1,
        }));
    }
    else {
        onAddToChatQueue(infoMsg('Context limit: Unknown. Use /context-max <number> to set one.', 'context-max-info'));
    }
    setTimeout(() => onCommandComplete?.(), DELAY_COMMAND_COMPLETE_MS);
    return true;
}
//# sourceMappingURL=context-max-handler.js.map