import React from 'react';
export const useStreamingStatus = ({ currentAssistantResponse, pendingInterruptions, showToolCalls }) => {
    const streamingContent = currentAssistantResponse.text ?? '';
    const streamingToolCalls = currentAssistantResponse.toolCalls ?? [];
    const reasoningEvents = currentAssistantResponse.reasoningEvents ?? [];
    const messageEvents = currentAssistantResponse.messageEvents ?? [];
    const streamingInterruptions = React.useMemo(() => {
        const liveInterruptions = currentAssistantResponse.interruptions ?? [];
        if (liveInterruptions.length > 0) {
            return liveInterruptions;
        }
        return pendingInterruptions;
    }, [currentAssistantResponse.interruptions, pendingInterruptions]);
    const activeReasoningEvents = React.useMemo(() => {
        if (reasoningEvents.length === 0)
            return [];
        const latestById = new Map();
        for (const event of reasoningEvents) {
            latestById.set(event.id, event);
        }
        return Array.from(latestById.values())
            .filter((event) => event.phase === 'started')
            .sort((a, b) => a.timestamp - b.timestamp);
    }, [reasoningEvents]);
    const activeMessageEvents = React.useMemo(() => {
        if (messageEvents.length === 0)
            return [];
        const latestById = new Map();
        for (const event of messageEvents) {
            latestById.set(event.id, event);
        }
        return Array.from(latestById.values())
            .filter((event) => event.phase === 'started')
            .sort((a, b) => a.timestamp - b.timestamp);
    }, [messageEvents]);
    const [expandedToolCalls, setExpandedToolCalls] = React.useState({});
    const toggleToolCallExpansion = React.useCallback((id) => {
        setExpandedToolCalls((prev) => ({
            ...prev,
            [id]: !(prev[id] ?? false)
        }));
    }, []);
    const hasStreamingText = streamingContent.trim().length > 0;
    const hasVisibleStreamingTools = showToolCalls && streamingToolCalls.length > 0;
    const isShowingStream = hasStreamingText || hasVisibleStreamingTools;
    return {
        streamingContent,
        streamingToolCalls,
        streamingInterruptions,
        activeReasoningEvents,
        activeMessageEvents,
        expandedToolCalls,
        toggleToolCallExpansion,
        hasStreamingText,
        hasVisibleStreamingTools,
        isShowingStream
    };
};
