
"use client";

import React, { createContext, useContext, useCallback } from 'react';
import { useGroupsCore } from '@/hooks/use-groups';
import type { ChatRecipient } from '@/lib/types';


type GroupsContextType = ReturnType<typeof useGroupsCore>;

const GroupsContext = createContext<GroupsContextType | null>(null);

export const useGroups = () => {
    const context = useContext(GroupsContext);
    if (!context) {
        throw new Error('useGroups must be used within a GroupsProvider');
    }
    return context;
};

export const GroupsProvider = ({ children }: { children: React.ReactNode }) => {
    // This hook is no longer used to set the recipient directly,
    // so we can pass a dummy function.
    const dummySetRecipient = useCallback((recipient: ChatRecipient | null | ((prev: ChatRecipient | null) => ChatRecipient | null)) => {}, []);
    const groupsData = useGroupsCore({ setRecipient: dummySetRecipient });
    
    return (
        <GroupsContext.Provider value={groupsData}>
            {children}
        </GroupsContext.Provider>
    );
};
