import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { createContext, useContext, useState, useCallback, useEffect, useMemo, } from 'react';
import { useOpenAIProviderInfo } from '../hooks/useOpenAIProviderInfo.js';
const OpenAIProviderContext = createContext(undefined);
export const OpenAIProviderContextProvider = ({ config, children }) => {
    const providerInfo = useOpenAIProviderInfo(config);
    const [remoteTokenStats, setRemoteTokenStats] = useState({
        promptTokenCount: 0,
        candidatesTokenCount: 0,
        totalTokenCount: 0,
        lastUpdated: null,
    });
    const updateRemoteTokenStats = useCallback((stats) => {
        setRemoteTokenStats((prev) => ({
            promptTokenCount: stats.promptTokenCount ?? prev.promptTokenCount,
            candidatesTokenCount: stats.candidatesTokenCount ?? prev.candidatesTokenCount,
            totalTokenCount: stats.totalTokenCount ?? prev.totalTokenCount,
            lastUpdated: new Date(),
        }));
    }, []);
    // Reset stats when provider changes
    useEffect(() => {
        if (!providerInfo.provider) {
            setRemoteTokenStats({
                promptTokenCount: 0,
                candidatesTokenCount: 0,
                totalTokenCount: 0,
                lastUpdated: null,
            });
        }
    }, [providerInfo.provider]);
    const value = useMemo(() => ({
        isOpenAIActive: providerInfo.provider !== null,
        isResponsesAPI: providerInfo.isResponsesAPI,
        currentModel: providerInfo.currentModel,
        conversationCache: providerInfo.conversationCache,
        remoteTokenStats,
        updateRemoteTokenStats,
        getCachedConversation: providerInfo.getCachedConversation,
    }), [
        providerInfo.provider,
        providerInfo.isResponsesAPI,
        providerInfo.currentModel,
        providerInfo.conversationCache,
        remoteTokenStats,
        updateRemoteTokenStats,
        providerInfo.getCachedConversation,
    ]);
    return (_jsx(OpenAIProviderContext.Provider, { value: value, children: children }));
};
export const useOpenAIProviderContext = () => {
    const context = useContext(OpenAIProviderContext);
    if (context === undefined) {
        throw new Error('useOpenAIProviderContext must be used within an OpenAIProviderContextProvider');
    }
    return context;
};
//# sourceMappingURL=OpenAIProviderContext.js.map