/**
 * 4Runr OS Animation Helper
 * Controlled motion with static fallback
 * Max duration enforcement
 */
import { AnimationConfig } from './copy.js';
import { C_ACCENT, C_MUTED, RESET } from './theme.js';
let activeAnimations = new Set();
/**
 * Spinner animation (subtle, for runs)
 * Has static fallback
 */
export function createSpinner(label) {
    const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
    let frameIndex = 0;
    let interval = null;
    let startTime = null;
    return {
        start: () => {
            if (!AnimationConfig.enabled) {
                process.stdout.write(`${C_ACCENT}${label}${RESET} ${C_MUTED}...${RESET}`);
                return;
            }
            startTime = Date.now();
            process.stdout.write(`${C_ACCENT}${label}${RESET} `);
            interval = setInterval(() => {
                if (startTime && Date.now() - startTime > AnimationConfig.maxBootDuration) {
                    // Stop after max duration
                    if (interval) {
                        clearInterval(interval);
                        interval = null;
                    }
                    return;
                }
                process.stdout.write(`\r${C_ACCENT}${label}${RESET} ${frames[frameIndex % frames.length]}`);
                frameIndex++;
            }, AnimationConfig.spinnerInterval);
            if (interval) {
                activeAnimations.add(interval);
            }
        },
        stop: (success, message) => {
            if (interval) {
                clearInterval(interval);
                activeAnimations.delete(interval);
                interval = null;
            }
            if (AnimationConfig.enabled) {
                process.stdout.write(`\r${' '.repeat(50)}\r`); // Clear line
            }
            else {
                process.stdout.write('\n');
            }
            const status = success ? 'OK' : 'ERROR';
            const msg = message ? ` ${message}` : '';
            console.log(`${C_ACCENT}${label}${RESET} ${status}${msg}`);
        },
    };
}
/**
 * Progress bar (for boot sequence)
 * Static fallback: "CHECKING..." → "OK"
 */
export function createProgress(label, maxDuration = AnimationConfig.maxBootDuration) {
    let startTime = null;
    let timeout = null;
    return {
        start: () => {
            startTime = Date.now();
            if (!AnimationConfig.enabled) {
                process.stdout.write(`${C_ACCENT}${label}${RESET} ${C_MUTED}CHECKING...${RESET}`);
                return;
            }
            // Static fallback: just show the label
            process.stdout.write(`${C_ACCENT}${label}${RESET} ${C_MUTED}...${RESET}`);
            // Auto-stop after max duration
            timeout = setTimeout(() => {
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = null;
                }
            }, maxDuration);
            if (timeout) {
                activeAnimations.add(timeout);
            }
        },
        stop: (success) => {
            if (timeout) {
                clearTimeout(timeout);
                activeAnimations.delete(timeout);
                timeout = null;
            }
            if (!AnimationConfig.enabled) {
                process.stdout.write(`\r${C_ACCENT}${label}${RESET} ${success ? 'OK' : 'ERROR'}\n`);
                return;
            }
            process.stdout.write(`\r${C_ACCENT}${label}${RESET} ${success ? 'OK' : 'ERROR'}\n`);
        },
    };
}
/**
 * Stop all active animations (cleanup)
 */
export function stopAllAnimations() {
    activeAnimations.forEach(interval => {
        clearInterval(interval);
        clearTimeout(interval);
    });
    activeAnimations.clear();
}
//# sourceMappingURL=animations.js.map