import { useQuery } from '@tanstack/react-query';

import { chatQueryKeys } from '../chat-query-keys';
import { chatApi, type PostGetGroupChatWithReturn } from '../chat-api';

import type { BaseAxiosError } from '../../../../types';

export const usePostGetGroupChatQuery = (
  token: string,
  group_token: string,
  no_of_messages: number,
  previous_than_message_id: number,
  enabled: boolean
) => {
  return useQuery<PostGetGroupChatWithReturn, BaseAxiosError>({
    queryKey: chatQueryKeys.getGroupChat(
      token,
      group_token,
      no_of_messages,
      previous_than_message_id
    ),
    queryFn: async () => {
      const response = await chatApi.getGroupChat(
        token,
        group_token,
        no_of_messages,
        previous_than_message_id
      );

      return response.data;
    },
    enabled
  });
};
