import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useMCPStore } from '../stores/mcpStore';
import { useEffect, useRef, useCallback } from 'react';
import { listMcpServers, startMcpServer, stopMcpServer, addMcpServer, removeMcpServer, initiateMcpAuth, revokeMcpAuth, getMcpAuthStatus, completeMcpAuth, } from '@ottocode/api';
export function useMCPServers() {
    const setServers = useMCPStore((s) => s.setServers);
    const query = useQuery({
        queryKey: ['mcp', 'servers'],
        queryFn: async () => {
            const { data } = await listMcpServers();
            return data;
        },
        refetchInterval: 10000,
    });
    useEffect(() => {
        if (query.data?.servers) {
            setServers(query.data.servers);
        }
    }, [query.data, setServers]);
    return query;
}
export function useStartMCPServer() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (name) => {
            const { data, error } = await startMcpServer({
                path: { name },
            });
            if (error)
                throw new Error('Failed to start server');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to start server');
            return result;
        },
        onSettled: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useStopMCPServer() {
    const queryClient = useQueryClient();
    const setLoading = useMCPStore((s) => s.setLoading);
    return useMutation({
        mutationFn: async (name) => {
            setLoading(name, true);
            const { data, error } = await stopMcpServer({
                path: { name },
            });
            if (error)
                throw new Error('Failed to stop server');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to stop server');
            return result;
        },
        onSettled: (_data, _error, name) => {
            setLoading(name, false);
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useAddMCPServer() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (params) => {
            const { data, error } = await addMcpServer({
                body: params,
            });
            if (error)
                throw new Error('Failed to add MCP server');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to add MCP server');
            return result;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useRemoveMCPServer() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (name) => {
            const { data, error } = await removeMcpServer({
                path: { name },
            });
            if (error)
                throw new Error('Failed to remove MCP server');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to remove MCP server');
            return result;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useAuthenticateMCPServer() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (name) => {
            const { data, error } = await initiateMcpAuth({
                path: { name },
            });
            if (error)
                throw new Error('Failed to initiate auth');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to initiate auth');
            return result;
        },
        onSettled: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useRevokeMCPAuth() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (name) => {
            const { data, error } = await revokeMcpAuth({
                path: { name },
            });
            if (error)
                throw new Error('Failed to revoke auth');
            const result = data;
            if (!result.ok)
                throw new Error(result.error || 'Failed to revoke auth');
            return result;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
        },
    });
}
export function useMCPAuthStatus(name) {
    return useQuery({
        queryKey: ['mcp', 'auth', name],
        queryFn: async () => {
            if (!name)
                return { authenticated: false };
            const { data } = await getMcpAuthStatus({
                path: { name },
            });
            return data;
        },
        enabled: !!name,
        refetchInterval: 3000,
    });
}
export function useCopilotDevicePoller() {
    const copilotDevice = useMCPStore((s) => s.copilotDevice);
    const setCopilotDevice = useMCPStore((s) => s.setCopilotDevice);
    const setLoading = useMCPStore((s) => s.setLoading);
    const queryClient = useQueryClient();
    const timerRef = useRef(null);
    const stopPolling = useCallback(() => {
        if (timerRef.current) {
            clearInterval(timerRef.current);
            timerRef.current = null;
        }
    }, []);
    useEffect(() => {
        if (!copilotDevice) {
            stopPolling();
            return;
        }
        const { sessionId, serverName, interval } = copilotDevice;
        const pollMs = (interval || 5) * 1000 + 1000;
        timerRef.current = setInterval(async () => {
            try {
                const { data } = await completeMcpAuth({
                    path: { name: serverName },
                    body: { sessionId },
                });
                const result = data;
                if (result?.status === 'complete') {
                    stopPolling();
                    setCopilotDevice(null);
                    setLoading(serverName, false);
                    queryClient.invalidateQueries({ queryKey: ['mcp', 'servers'] });
                }
                else if (result?.status === 'error') {
                    stopPolling();
                    setCopilotDevice(null);
                    setLoading(serverName, false);
                }
            }
            catch {
                stopPolling();
                setCopilotDevice(null);
                setLoading(serverName, false);
            }
        }, pollMs);
        return stopPolling;
    }, [copilotDevice, setCopilotDevice, setLoading, queryClient, stopPolling]);
    return copilotDevice;
}
//# sourceMappingURL=useMCP.js.map