import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect, useCallback } from "react";
import { Box, Text, useInput, useApp } from "ink";
import { StatusBar } from "./StatusBar.js";
import { AgentTable } from "./AgentTable.js";
import { EventLog } from "./EventLog.js";
import { HelpOverlay } from "./HelpOverlay.js";
import { apiGet } from "../utils/api.js";
export function Overview({ port }) {
    const { exit } = useApp();
    const [data, setData] = useState(null);
    const [error, setError] = useState(null);
    const [showHelp, setShowHelp] = useState(false);
    const [showActiveOnly, setShowActiveOnly] = useState(false);
    const [lastUpdate, setLastUpdate] = useState(new Date());
    const fetchData = useCallback(async () => {
        try {
            const result = await apiGet("/api/overview", port);
            setData(result);
            setError(null);
            setLastUpdate(new Date());
        }
        catch (err) {
            if (err && typeof err === "object" && "status" in err) {
                const apiErr = err;
                if (apiErr.status === 0) {
                    setError("Server not running. Start with 'agentgazer start' first.");
                }
                else {
                    setError(`Error: ${apiErr.message}`);
                }
            }
            else {
                setError("Connection lost");
            }
        }
    }, [port]);
    useEffect(() => {
        fetchData();
        const interval = setInterval(fetchData, 2500);
        return () => clearInterval(interval);
    }, [fetchData]);
    useInput((input, key) => {
        if (input === "q" || input === "Q" || key.escape) {
            exit();
        }
        else if (input === "r" || input === "R") {
            fetchData();
        }
        else if (input === "a" || input === "A") {
            setShowActiveOnly((prev) => !prev);
        }
        else if (input === "?") {
            setShowHelp((prev) => !prev);
        }
    });
    if (showHelp) {
        return _jsx(HelpOverlay, { onClose: () => setShowHelp(false) });
    }
    if (error && !data) {
        return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsx(Text, { bold: true, color: "red", children: "AgentGazer Overview" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "yellow", children: error }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Press Q or ESC to exit. Waiting for server..." }) })] }));
    }
    if (!data) {
        return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsx(Text, { bold: true, children: "AgentGazer Overview" }), _jsx(Text, { dimColor: true, children: "Loading..." })] }));
    }
    const agents = showActiveOnly ? data.agents.filter((a) => a.status === "active") : data.agents;
    return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsx(StatusBar, { uptimeSeconds: data.uptime_seconds, requestsToday: data.total_requests_today, costToday: data.total_cost_today, serverStatus: error ? "disconnected" : data.server_status, proxyStatus: data.proxy_status }), _jsx(Box, { marginTop: 1, children: _jsx(AgentTable, { agents: agents }) }), _jsx(Box, { marginTop: 1, children: _jsx(EventLog, { events: data.recent_events }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { dimColor: true, children: ["Q/ESC: Exit | R: Refresh | A: Toggle active only", showActiveOnly ? " (on)" : "", " | ?: Help"] }) })] }));
}
//# sourceMappingURL=Overview.js.map