import { useState, useEffect, useCallback, useRef } from "react";
/**
 * Hook that runs blocking status checks at startup.
 * Returns the state, any issues found, and a recheck function.
 */
export function useStartupChecks(statusChecks) {
    // Start in loading so the startup spinner appears immediately on first render.
    const [state, setState] = useState("loading");
    const [issues, setIssues] = useState([]);
    const hasRun = useRef(false);
    // Filter to only blocking checks
    const blockingChecks = statusChecks?.filter((check) => check.isBlocking) || [];
    const runChecks = useCallback(async (options) => {
        if (blockingChecks.length === 0) {
            setState("complete");
            setIssues([]);
            return;
        }
        if (!options?.silent) {
            setState("loading");
        }
        // Run all blocking checks in parallel
        const checkPromises = blockingChecks.map(async (check) => {
            try {
                return await check.check();
            }
            catch (e) {
                return [
                    {
                        id: `${check.id}-error`,
                        title: `${check.name} Check Failed`,
                        description: e?.message || "An error occurred during the check",
                        severity: "error",
                    },
                ];
            }
        });
        const results = await Promise.all(checkPromises);
        const allIssues = results.flat();
        setIssues(allIssues);
        setState("complete");
    }, [blockingChecks]);
    // Run checks on mount (only once)
    useEffect(() => {
        if (!hasRun.current && statusChecks !== undefined) {
            hasRun.current = true;
            void runChecks();
        }
    }, [statusChecks, runChecks]);
    return {
        state,
        hasBlockingIssues: issues.some(issue => issue.severity === "error"),
        issues,
        recheck: () => runChecks(),
        recheckQuiet: () => runChecks({ silent: true }),
        clearIssuesByIdPrefix: (prefix) => {
            setIssues((currentIssues) => currentIssues.filter((issue) => !issue.id.startsWith(prefix)));
        },
    };
}
//# sourceMappingURL=useStartupChecks.js.map