import { useCallback, useEffect, useState } from "react";
function normalizeAgentMode(value) {
    if (value === "autonomous" ||
        value === "read-only" ||
        value === "supervised") {
        return value;
    }
    return null;
}
export function useAgentModeState(input) {
    const { agent, agentMetadata, setAgent, setAgentMetadata } = input;
    const [mode, setMode] = useState("supervised");
    const handleSwitchToAutonomous = useCallback(() => {
        const nextMode = "autonomous";
        setMode(nextMode);
        setAgentMetadata((prev) => ({
            ...(prev ?? {}),
            mode: nextMode,
        }));
        setAgent((prev) => {
            if (!prev)
                return prev;
            return {
                ...prev,
                mode: nextMode,
            };
        });
    }, [setAgent, setAgentMetadata]);
    useEffect(() => {
        try {
            const metaMode = normalizeAgentMode(agentMetadata?.mode);
            if (metaMode) {
                setMode(metaMode);
                return;
            }
        }
        catch { }
        try {
            const serverMode = normalizeAgentMode(agent?.mode);
            if (serverMode) {
                setMode(serverMode);
            }
        }
        catch { }
    }, [agentMetadata, agent?.mode]);
    return {
        mode,
        setMode,
        handleSwitchToAutonomous,
    };
}
//# sourceMappingURL=useAgentModeState.js.map