import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useEffect, useCallback, useRef, useMemo, } from 'react';
import { ToolCallContext } from './ToolCallContext.js';
import { ToolCallTrackerService, DEFAULT_AGENT_ID, } from '@vybestack/llxprt-code-core';
export const ToolCallProvider = ({ children, sessionId, agentId, }) => {
    // Store executing tool calls in state to trigger re-renders
    const [executingToolCalls, setExecutingToolCalls] = useState(new Map());
    // Track if we've already subscribed to avoid duplicate subscriptions
    const unsubscribeRef = useRef(null);
    // Get executing tool calls for a specific todo
    const getExecutingToolCalls = useCallback((todoId) => executingToolCalls.get(todoId) || [], [executingToolCalls]);
    // Update the state with current tool calls
    const scopedAgentId = agentId ?? DEFAULT_AGENT_ID;
    const updateExecutingToolCalls = useCallback(() => {
        // Get all tool calls for this session
        const allCalls = ToolCallTrackerService.getAllToolCallsForSession(sessionId, scopedAgentId);
        // Convert to the format we want for state
        const newExecutingToolCalls = new Map();
        for (const [todoId] of allCalls) {
            // Get all tool calls (both executing and completed)
            const allToolCalls = ToolCallTrackerService.getAllToolCalls(sessionId, todoId, scopedAgentId);
            newExecutingToolCalls.set(todoId, allToolCalls);
        }
        setExecutingToolCalls(newExecutingToolCalls);
    }, [scopedAgentId, sessionId]);
    // Set up subscription to tool call updates
    useEffect(() => {
        // Unsubscribe from previous subscription if it exists
        if (unsubscribeRef.current) {
            unsubscribeRef.current();
            unsubscribeRef.current = null;
        }
        // Subscribe to updates
        const unsubscribe = ToolCallTrackerService.subscribeToUpdates(sessionId, updateExecutingToolCalls, scopedAgentId);
        unsubscribeRef.current = unsubscribe;
        // Do an initial update to get current state
        updateExecutingToolCalls();
        // Clean up subscription on unmount
        return () => {
            if (unsubscribeRef.current) {
                unsubscribeRef.current();
                unsubscribeRef.current = null;
            }
        };
    }, [scopedAgentId, sessionId, updateExecutingToolCalls]);
    const contextValue = useMemo(() => ({
        getExecutingToolCalls,
        subscribe: (callback) => {
            const unsubscribe = ToolCallTrackerService.subscribeToUpdates(sessionId, () => {
                updateExecutingToolCalls();
                callback();
            }, scopedAgentId);
            return unsubscribe;
        },
    }), [getExecutingToolCalls, scopedAgentId, sessionId, updateExecutingToolCalls]);
    return (_jsx(ToolCallContext.Provider, { value: contextValue, children: children }));
};
//# sourceMappingURL=ToolCallProvider.js.map