import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { API_BASE_URL } from '../lib/config';
import { usePendingResearchStore } from '../stores/pendingResearchStore';
class ResearchApiClient {
    get baseUrl() {
        // Check for runtime injected URL first
        const win = window;
        if (win.AGI_SERVER_URL) {
            return win.AGI_SERVER_URL;
        }
        // Check for Vite env var
        if (import.meta.env?.VITE_API_BASE_URL) {
            return import.meta.env.VITE_API_BASE_URL;
        }
        return API_BASE_URL;
    }
    async listResearchSessions(parentSessionId) {
        const response = await fetch(`${this.baseUrl}/v1/sessions/${parentSessionId}/research`, {
            method: 'GET',
            headers: { 'Content-Type': 'application/json' },
        });
        if (!response.ok) {
            const error = await response
                .json()
                .catch(() => ({ error: 'Failed to fetch research sessions' }));
            throw new Error(error.error || 'Failed to fetch research sessions');
        }
        return response.json();
    }
    async createResearchSession(parentSessionId, data) {
        const response = await fetch(`${this.baseUrl}/v1/sessions/${parentSessionId}/research`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data),
        });
        if (!response.ok) {
            const error = await response
                .json()
                .catch(() => ({ error: 'Failed to create research session' }));
            throw new Error(error.error || 'Failed to create research session');
        }
        return response.json();
    }
    async deleteResearchSession(researchId) {
        const response = await fetch(`${this.baseUrl}/v1/research/${researchId}`, {
            method: 'DELETE',
            headers: { 'Content-Type': 'application/json' },
        });
        if (!response.ok) {
            const error = await response
                .json()
                .catch(() => ({ error: 'Failed to delete research session' }));
            throw new Error(error.error || 'Failed to delete research session');
        }
        return response.json();
    }
    async injectContext(parentSessionId, researchSessionId, label) {
        const response = await fetch(`${this.baseUrl}/v1/sessions/${parentSessionId}/inject`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ researchSessionId, label }),
        });
        if (!response.ok) {
            const error = await response
                .json()
                .catch(() => ({ error: 'Failed to inject context' }));
            throw new Error(error.error || 'Failed to inject context');
        }
        return response.json();
    }
    async exportToNewSession(researchId, data) {
        const response = await fetch(`${this.baseUrl}/v1/research/${researchId}/export`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data ?? {}),
        });
        if (!response.ok) {
            const error = await response
                .json()
                .catch(() => ({ error: 'Failed to export to session' }));
            throw new Error(error.error || 'Failed to export to session');
        }
        return response.json();
    }
}
const researchApi = new ResearchApiClient();
export function useResearchSessions(parentSessionId) {
    return useQuery({
        queryKey: ['research', 'sessions', parentSessionId],
        queryFn: () => researchApi.listResearchSessions(parentSessionId),
        enabled: !!parentSessionId,
        staleTime: 30000,
    });
}
export function useCreateResearchSession() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({ parentSessionId, data, }) => researchApi.createResearchSession(parentSessionId, data ?? {}),
        onSuccess: (_, { parentSessionId }) => {
            queryClient.invalidateQueries({
                queryKey: ['research', 'sessions', parentSessionId],
            });
        },
    });
}
export function useDeleteResearchSession() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: (researchId) => researchApi.deleteResearchSession(researchId),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['research', 'sessions'] });
        },
    });
}
export function useInjectContext() {
    const addContext = usePendingResearchStore((state) => state.addContext);
    return useMutation({
        mutationFn: ({ parentSessionId, researchSessionId, label, }) => researchApi.injectContext(parentSessionId, researchSessionId, label),
        onSuccess: (data, { parentSessionId }) => {
            addContext(parentSessionId, {
                id: data.sessionId,
                sessionId: data.sessionId,
                label: data.label,
                content: data.content,
            });
        },
    });
}
export function useExportToSession() {
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({ researchId, data, }) => researchApi.exportToNewSession(researchId, data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['sessions'] });
        },
    });
}
//# sourceMappingURL=useResearch.js.map