import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * OutageScreen — Shown when AI services are degraded (v2).
 *
 * Reads session.serviceStatus and provides a ConfirmationInput to continue
 * or exit. While shown, polls the same statuspage rollup that gated the
 * overlay every 30s and auto-dismisses when status flips back to healthy
 * — no more hollow "temporarily unavailable" promises that never resolve.
 *
 * Polling caps at 10 attempts (5min) before stepping aside and letting
 * the user decide manually. Status checks have a 5s internal timeout
 * (see statuspage.ts) so a hung status host can never freeze this screen.
 */
import { Box, Text } from 'ink';
import { useEffect, useRef, useState } from 'react';
import { useWizardStore } from '../hooks/useWizardStore.js';
import { ConfirmationInput, TerminalLink } from '../primitives/index.js';
import { Colors, Icons } from '../styles.js';
import { wizardAbort } from '../../../utils/wizard-abort.js';
import { BrailleSpinner } from '../components/BrailleSpinner.js';
import { checkAmplitudeOverallHealth } from '../../../lib/health-checks/index.js';
import { ServiceHealthStatus } from '../../../lib/health-checks/types.js';
import { logToFile } from '../../../utils/debug.js';
const POLL_INTERVAL_MS = 30_000;
const MAX_POLL_ATTEMPTS = 10;
export const OutageScreen = ({ store }) => {
    useWizardStore(store);
    const serviceStatus = store.session.serviceStatus;
    const [attempts, setAttempts] = useState(0);
    const [recheckInFlight, setRecheckInFlight] = useState(false);
    const cancelledRef = useRef(false);
    // Background poll: every 30s call the same statuspage rollup that
    // pushed this overlay. On healthy → popOverlay() and the user resumes
    // wherever they were. The `cancelledRef` guard prevents a late fetch
    // from acting on an already-popped overlay.
    useEffect(() => {
        if (!serviceStatus)
            return;
        cancelledRef.current = false;
        const tick = async () => {
            if (cancelledRef.current)
                return;
            if (attempts >= MAX_POLL_ATTEMPTS)
                return;
            setRecheckInFlight(true);
            try {
                const result = await checkAmplitudeOverallHealth();
                if (cancelledRef.current)
                    return;
                logToFile(`[OutageScreen] recheck attempt=${attempts + 1} status=${result.status}`);
                if (result.status === ServiceHealthStatus.Healthy) {
                    store.popOverlay();
                    return;
                }
            }
            catch (err) {
                logToFile(`[OutageScreen] recheck failed: ${err instanceof Error ? err.message : String(err)}`);
            }
            finally {
                if (!cancelledRef.current) {
                    setRecheckInFlight(false);
                    setAttempts((a) => a + 1);
                }
            }
        };
        const id = setTimeout(() => void tick(), POLL_INTERVAL_MS);
        return () => {
            cancelledRef.current = true;
            clearTimeout(id);
        };
        // attempts is intentionally a dep so the timer chain advances each tick.
    }, [serviceStatus, attempts]);
    if (!serviceStatus) {
        return null;
    }
    const giveUp = attempts >= MAX_POLL_ATTEMPTS;
    return (_jsxs(Box, { flexDirection: "column", flexGrow: 1, children: [_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { color: Colors.warning, bold: true, children: [Icons.diamond, " The setup agent is temporarily unavailable."] }), _jsx(Text, { children: " " }), _jsxs(Text, { color: Colors.body, children: [_jsx(Text, { color: Colors.warning, children: "Status:" }), ' ', serviceStatus.description] }), _jsxs(Text, { color: Colors.body, children: [_jsx(Text, { color: Colors.warning, children: "Status page:" }), ' ', _jsx(TerminalLink, { url: serviceStatus.statusPageUrl, children: serviceStatus.statusPageUrl })] }), _jsx(Text, { children: " " }), _jsx(Text, { color: Colors.secondary, children: "The wizard may not work reliably while services are affected." }), !giveUp && (_jsxs(Box, { marginTop: 1, gap: 1, children: [recheckInFlight ? (_jsx(BrailleSpinner, { color: Colors.accent })) : (_jsx(Text, { color: Colors.subtle, children: Icons.dot })), _jsxs(Text, { color: Colors.muted, children: ["Re-checking", Icons.ellipsis, " (attempt ", attempts + 1, " of", ' ', MAX_POLL_ATTEMPTS, ")"] })] })), giveUp && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: Colors.muted, children: ["Still degraded after ", MAX_POLL_ATTEMPTS, " checks. Continue if you want to try anyway, or cancel and retry later."] }) }))] }), _jsx(ConfirmationInput, { message: "Continue anyway?", onConfirm: () => store.popOverlay(), onCancel: () => {
                    // Outage detected and user chose to bail — route through
                    // wizardAbort so the cancel reason is captured to analytics
                    // and the user sees a proper exit message instead of a
                    // half-rendered overlay.
                    void wizardAbort({
                        message: 'Setup paused — services were degraded. Re-run the wizard once the status page clears.',
                        exitCode: 0,
                    });
                } })] }));
};
