/**
 * Budget Alert Manager
 *
 * Monitors session and budget costs, emitting alerts when
 * spending approaches or exceeds configured thresholds.
 *
 * Default thresholds:
 * - Warning at 70% of budget
 * - Critical at 90% of budget
 * - Limit reached at 100%
 *
 * @module analytics
 */
import { EventEmitter } from 'events';
const DEFAULT_CONFIG = {
    warningThreshold: 0.7,
    criticalThreshold: 0.9,
};
/**
 * Budget Alert Manager
 *
 * Tracks spending against a budget limit and emits alerts
 * when thresholds are crossed. Alerts are deduplicated so
 * the same threshold level is only emitted once per session
 * (until reset).
 *
 * Events:
 * - 'alert' - Emitted with a BudgetAlert when a threshold is crossed
 */
export class BudgetAlertManager extends EventEmitter {
    config;
    alerts = [];
    emittedTypes = new Set();
    constructor(config) {
        super();
        this.config = { ...DEFAULT_CONFIG, ...config };
    }
    /**
     * Check current cost against limits and return an alert if a threshold is crossed.
     *
     * Alerts are deduplicated: once an alert of a given type has been emitted,
     * it will not be emitted again until `reset()` is called.
     *
     * @param currentCost - Current accumulated cost in USD
     * @param limit - Budget limit in USD
     * @returns A BudgetAlert if a threshold was crossed, or null
     */
    check(currentCost, limit) {
        if (limit <= 0) {
            return null;
        }
        const percentage = (currentCost / limit) * 100;
        // Check from most severe to least severe
        if (currentCost >= limit && !this.emittedTypes.has('limit_reached')) {
            const alert = {
                type: 'limit_reached',
                message: `Budget limit reached! Spent $${currentCost.toFixed(4)} of $${limit.toFixed(2)} budget (${percentage.toFixed(1)}%).`,
                currentCost,
                limit,
                percentage,
            };
            this.recordAlert(alert);
            return alert;
        }
        if (currentCost >= limit * this.config.criticalThreshold &&
            !this.emittedTypes.has('critical')) {
            const alert = {
                type: 'critical',
                message: `Critical: Approaching budget limit! Spent $${currentCost.toFixed(4)} of $${limit.toFixed(2)} budget (${percentage.toFixed(1)}%).`,
                currentCost,
                limit,
                percentage,
            };
            this.recordAlert(alert);
            return alert;
        }
        if (currentCost >= limit * this.config.warningThreshold &&
            !this.emittedTypes.has('warning')) {
            const alert = {
                type: 'warning',
                message: `Warning: Budget usage at ${percentage.toFixed(1)}%. Spent $${currentCost.toFixed(4)} of $${limit.toFixed(2)} budget.`,
                currentCost,
                limit,
                percentage,
            };
            this.recordAlert(alert);
            return alert;
        }
        return null;
    }
    /**
     * Get the history of all alerts emitted in this session.
     */
    getAlerts() {
        return [...this.alerts];
    }
    /**
     * Reset all alert state, clearing history and deduplication tracking.
     * This allows alerts to fire again for the same thresholds.
     */
    reset() {
        this.alerts = [];
        this.emittedTypes.clear();
    }
    /**
     * Get the current configuration.
     */
    getConfig() {
        return { ...this.config };
    }
    /**
     * Update configuration thresholds.
     */
    updateConfig(config) {
        this.config = { ...this.config, ...config };
    }
    /**
     * Record an alert and emit the 'alert' event.
     */
    recordAlert(alert) {
        this.alerts.push(alert);
        this.emittedTypes.add(alert.type);
        this.emit('alert', alert);
    }
}
//# sourceMappingURL=budget-alerts.js.map