"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenBudgetManager = void 0;
/**
 * Token budget manager for enforcing token limits
 */
class TokenBudgetManager {
    constructor(initialBudget = 8000) {
        this.budget = initialBudget;
        this.used = 0;
        this.compressionRatio = 0; // Will be updated as we compress
    }
    /**
     * Set the token budget
     */
    setBudget(budget) {
        this.budget = budget;
    }
    /**
     * Get remaining tokens
     */
    getRemaining() {
        return Math.max(0, this.budget - this.used);
    }
    /**
     * Get used tokens
     */
    getUsed() {
        return this.used;
    }
    /**
     * Get total budget
     */
    getTotal() {
        return this.budget;
    }
    /**
     * Add tokens to used count
     */
    useTokens(tokens) {
        this.used += tokens;
    }
    /**
     * Reset used tokens
     */
    reset() {
        this.used = 0;
    }
    /**
     * Check if we're over budget
     */
    isOverBudget() {
        return this.used > this.budget;
    }
    /**
     * Get compression ratio achieved
     */
    getCompressionRatio() {
        return this.compressionRatio;
    }
    /**
     * Update compression ratio based on savings
     */
    updateCompressionRatio(original, saved) {
        if (original > 0) {
            this.compressionRatio = saved / original;
        }
    }
    /**
     * Truncate text to fit within remaining budget
     */
    truncateToFit(text) {
        const estimatedTokens = this.estimateTokens(text);
        const remaining = this.getRemaining();
        if (estimatedTokens <= remaining) {
            return { result: text, truncated: false };
        }
        // Calculate how many characters we can keep
        // Roughly 4 chars per token
        const maxChars = remaining * 4;
        if (maxChars <= 0) {
            return { result: "", truncated: true };
        }
        const truncated = text.substring(0, maxChars);
        return { result: truncated, truncated: true };
    }
    /**
     * Estimate token count for text
     */
    estimateTokens(text) {
        if (!text)
            return 0;
        return Math.ceil(text.length / 4);
    }
    /**
     * Get budget status as percentage
     */
    getBudgetUsagePercent() {
        if (this.budget === 0)
            return 0;
        return Math.min(100, (this.used / this.budget) * 100);
    }
}
exports.TokenBudgetManager = TokenBudgetManager;
//# sourceMappingURL=budget.js.map