/**
 * useTimedCoaching — escalating coaching messages on long-running screens.
 *
 * Spinners that spin forever with no acknowledgement are the worst UX sin in
 * a CLI. This hook gives screens a tiered escalation pattern: after T1
 * seconds of no progress, surface a calm reassurance line; after T2 escalate
 * to alternative actions; after T3 offer manual fallbacks. When a "progress
 * signal" changes (a task counter, a poll counter, anything monotonic) the
 * timer resets — the user is making forward motion, so we shouldn't nag.
 *
 * Returns the current `tier` (0–thresholds.length) plus `elapsedSeconds`,
 * which screens use to render the right copy.
 *
 * Example:
 *   const { tier, elapsedSeconds } = useTimedCoaching({
 *     thresholds: [60, 120, 300],
 *     progressSignal: tasks.length,
 *   });
 *   if (tier >= 1) <Text>Still working — press ← / → for Logs…</Text>
 *
 * Reference: DataIngestionCheckScreen has a hand-rolled version of this at
 * 60/90/120/180s; this hook generalises that pattern.
 */
import { useEffect, useRef, useState } from 'react';
export function useTimedCoaching({ thresholds, progressSignal, tickIntervalMs = 1000, }) {
    const [elapsedSeconds, setElapsedSeconds] = useState(0);
    const startedAtRef = useRef(Date.now());
    const lastSignalRef = useRef(progressSignal);
    // Reset on progress.
    useEffect(() => {
        if (lastSignalRef.current !== progressSignal) {
            lastSignalRef.current = progressSignal;
            startedAtRef.current = Date.now();
            setElapsedSeconds(0);
        }
    }, [progressSignal]);
    // Tick the elapsed counter.
    useEffect(() => {
        const id = setInterval(() => {
            setElapsedSeconds(Math.floor((Date.now() - startedAtRef.current) / 1000));
        }, tickIntervalMs);
        return () => clearInterval(id);
    }, [tickIntervalMs]);
    let tier = 0;
    for (const threshold of thresholds) {
        if (elapsedSeconds >= threshold)
            tier += 1;
        else
            break;
    }
    return { tier, elapsedSeconds };
}
