/**
 * webhook/budget.ts
 *
 * Daily budget tracking for webhook-triggered evaluations.
 *
 * Evaluations cost $0.50–$5+ per run. Without guardrails, rapid-fire
 * content edits could exhaust the evaluation budget. This module provides
 * a simple in-memory counter that resets daily.
 *
 * For persistent budget tracking across process restarts, store the
 * counter in Sanity or a KV store (future enhancement).
 *
 * @see docs/design-docs/report-store/visibility-workflows.md — Budget caps
 */
/**
 * Create a daily budget tracker.
 *
 * The counter resets automatically when the date changes (UTC).
 * State is in-memory only — restarting the process resets the counter.
 *
 * @param dailyLimit - Maximum evaluations per day (default: 20)
 */
export function createBudgetTracker(dailyLimit = 20) {
    const state = {
        count: 0,
        date: todayUTC(),
        limit: dailyLimit,
    };
    function maybeReset() {
        const today = todayUTC();
        if (state.date !== today) {
            state.count = 0;
            state.date = today;
        }
    }
    return {
        canDispatch() {
            maybeReset();
            return state.count < state.limit;
        },
        currentState() {
            maybeReset();
            return { ...state };
        },
        record() {
            maybeReset();
            state.count++;
        },
        remaining() {
            maybeReset();
            return Math.max(0, state.limit - state.count);
        },
    };
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function todayUTC() {
    return new Date().toISOString().slice(0, 10);
}
