import { useRef } from "react";
const DEV = process.env.NODE_ENV !== "production";
/** Rolling window size for interval statistics. */
const WINDOW_SIZE = 30;
/** Log a sampled line every Nth tick. */
const LOG_EVERY = 10;
const NOOP_TRACKER = {
    tick() { },
    summary() { },
};
/**
 * Dev-only hook that returns an imperative {@link StreamRateTracker}.
 *
 * The tracker is designed to be called inside a `useEffect` async
 * loop (not during render), so it uses a ref-backed mutable object
 * rather than React state.
 *
 * In production the hook returns a no-op tracker.
 */
export function useStreamRate() {
    const stateRef = useRef(null);
    if (!DEV)
        return NOOP_TRACKER;
    if (!stateRef.current) {
        stateRef.current = {
            tickCount: 0,
            timestamps: [],
            prevMessageCount: 0,
        };
    }
    const state = stateRef.current;
    const tracker = {
        tick(messageCount) {
            const now = performance.now();
            state.tickCount += 1;
            state.timestamps.push(now);
            if (state.timestamps.length > WINDOW_SIZE) {
                state.timestamps.shift();
            }
            const delta = messageCount - state.prevMessageCount;
            state.prevMessageCount = messageCount;
            if (state.tickCount % LOG_EVERY === 0 || state.tickCount === 1) {
                const intervals = computeIntervals(state.timestamps);
                const parts = [
                    `[stgm:perf:stream] tick #${state.tickCount}`,
                    `messages=${messageCount}`,
                    `delta=+${delta}`,
                ];
                if (intervals) {
                    parts.push(`interval=${intervals.avg.toFixed(0)}ms`, `rate=${intervals.rate.toFixed(1)}/s`);
                }
                console.debug(parts.join("  "));
            }
        },
        summary() {
            if (state.tickCount === 0)
                return;
            const intervals = computeIntervals(state.timestamps);
            const parts = [
                `[stgm:perf:stream] ✓ stream complete`,
                `totalTicks=${state.tickCount}`,
                `finalMessages=${state.prevMessageCount}`,
            ];
            if (intervals) {
                parts.push(`avgInterval=${intervals.avg.toFixed(0)}ms`, `minInterval=${intervals.min.toFixed(0)}ms`, `maxInterval=${intervals.max.toFixed(0)}ms`, `avgRate=${intervals.rate.toFixed(1)}/s`);
            }
            console.debug(parts.join("  "));
            state.tickCount = 0;
            state.timestamps.length = 0;
            state.prevMessageCount = 0;
        },
    };
    return tracker;
}
function computeIntervals(timestamps) {
    if (timestamps.length < 2)
        return null;
    let min = Infinity;
    let max = 0;
    let sum = 0;
    const count = timestamps.length - 1;
    for (let i = 1; i < timestamps.length; i++) {
        const dt = timestamps[i] - timestamps[i - 1];
        sum += dt;
        if (dt < min)
            min = dt;
        if (dt > max)
            max = dt;
    }
    const avg = sum / count;
    const rate = avg > 0 ? 1000 / avg : 0;
    return { min, max, avg, rate };
}
//# sourceMappingURL=use-stream-rate.js.map