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

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

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

export const usePostGetChatWithQuery = (
  token: string,
  uid: number,
  no_of_messages: number,
  previous_than_message_id: number,
  enabled: boolean
) => {
  return useQuery<PostGetChatWithReturn, BaseAxiosError>({
    queryKey: chatQueryKeys.getChatWith(token, uid, no_of_messages, previous_than_message_id),
    queryFn: async () => {
      const response = await chatApi.getChatWith(
        token,
        uid,
        no_of_messages,
        previous_than_message_id
      );

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