import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getTerminals, postTerminals, deleteTerminalsById, getTerminalsById, } from '@agi-cli/api';
export function useTerminals() {
    return useQuery({
        queryKey: ['terminals'],
        queryFn: async () => {
            const response = await getTerminals();
            if (response.error) {
                throw new Error('Failed to fetch terminals');
            }
            return response.data;
        },
        refetchInterval: 2000,
    });
}
export function useCreateTerminal() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (params) => {
            const response = await postTerminals({
                body: params,
            });
            if (response.error) {
                throw new Error('Failed to create terminal');
            }
            return response.data;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['terminals'] });
        },
    });
}
export function useKillTerminal() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (terminalId) => {
            const response = await deleteTerminalsById({
                path: { id: terminalId },
            });
            if (response.error) {
                throw new Error('Failed to kill terminal');
            }
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['terminals'] });
        },
    });
}
export function useTerminalOutput(terminalId) {
    return useQuery({
        queryKey: ['terminal', terminalId],
        queryFn: async () => {
            if (!terminalId)
                throw new Error('No terminal ID');
            const response = await getTerminalsById({
                path: { id: terminalId },
            });
            if (response.error) {
                throw new Error('Failed to fetch terminal');
            }
            return response.data?.terminal;
        },
        enabled: !!terminalId,
        refetchInterval: 1000,
    });
}
//# sourceMappingURL=useTerminals.js.map