import { getChatRoom, IChatRoomPayload } from '@livelike/javascript';
import { useEffect, useState } from 'react';
import { useAnalytics } from './useAnalytics';

export type UseChatRoomArg = {
  roomId: string;
};

export function useChatRoom({ roomId }: UseChatRoomArg) {
  const [chatRoom, setChatRoom] = useState<IChatRoomPayload | null>(null);
  const { trackEvent } = useAnalytics();
  useEffect(() => {
    getChatRoom({ roomId }).then((_chatroom) => {
      setChatRoom(_chatroom);
      trackEvent('Chat Room Entered', { roomId, room: _chatroom });
    });
    return () => {
      trackEvent('Chat Room Exited', { roomId });
    };
  }, []);

  return { chatRoom };
}
