import { DEFAULT_MODEL_CONTEXT_TOKENS } from './types.js';
const DEFAULT_WARN_RATIO = 0.9;
export class ContextAdvisor {
    warnRatio = DEFAULT_WARN_RATIO;
    hardLimit = false;
    configure(config) {
        if (config && typeof config.warnRatio === 'number' && Number.isFinite(config.warnRatio)) {
            this.warnRatio = clampWarnRatio(config.warnRatio);
        }
        else {
            this.warnRatio = DEFAULT_WARN_RATIO;
        }
        this.hardLimit = Boolean(config?.hardLimit);
    }
    classify(pool, estimatedTokens, resolveProfile) {
        const normalizedTokens = typeof estimatedTokens === 'number' && Number.isFinite(estimatedTokens) && estimatedTokens > 0
            ? estimatedTokens
            : 0;
        const safe = [];
        const risky = [];
        const overflow = [];
        const usage = {};
        for (const providerKey of pool) {
            let limit = DEFAULT_MODEL_CONTEXT_TOKENS;
            try {
                const profile = resolveProfile(providerKey);
                if (profile?.maxContextTokens && Number.isFinite(profile.maxContextTokens)) {
                    limit = profile.maxContextTokens;
                }
            }
            catch {
                limit = DEFAULT_MODEL_CONTEXT_TOKENS;
            }
            if (!limit || limit <= 0) {
                limit = DEFAULT_MODEL_CONTEXT_TOKENS;
            }
            const ratio = limit > 0 ? normalizedTokens / limit : 0;
            usage[providerKey] = { ratio, limit };
            if (normalizedTokens === 0 || ratio < this.warnRatio) {
                safe.push(providerKey);
                continue;
            }
            if (ratio < 1) {
                risky.push(providerKey);
                continue;
            }
            overflow.push(providerKey);
        }
        return {
            safe,
            risky,
            overflow,
            usage,
            estimatedTokens: normalizedTokens,
            allOverflow: safe.length === 0 && risky.length === 0 && overflow.length > 0
        };
    }
    getConfig() {
        return { warnRatio: this.warnRatio, hardLimit: this.hardLimit };
    }
}
function clampWarnRatio(value) {
    if (!Number.isFinite(value)) {
        return DEFAULT_WARN_RATIO;
    }
    return Math.max(0.1, Math.min(0.99, value));
}
