import { statisticsQueryKeys } from '../statistics-query-keys';
import { type PostGetList, type PostGetStat, statisticsApi } from '../statistics-api';
import { queryClient } from 'src/utils/queryClient';

export const fetchList = async (token: string) => {
  try {
    const data: PostGetList = await queryClient.fetchQuery({
      queryKey: statisticsQueryKeys.getList(token),
      queryFn: async () => {
        const response = await statisticsApi.getList(token);
        return response.data;
      },
      gcTime: 0,
      staleTime: 0
    });

    return data;
  } catch (error) {
    console.error('Failed to fetch statistics list:', error);
  }
};

export const fetchStatistic = async (token: string, url1: string, url2: string) => {
  try {
    const data: PostGetStat = await queryClient.fetchQuery({
      queryKey: statisticsQueryKeys.getStatistic(token, url1, url2),
      queryFn: async () => {
        const response = await statisticsApi.getStatistic(token, url1, url2);
        return response.data;
      },
      gcTime: 0,
      staleTime: 0
    });

    return data;
  } catch (error) {
    console.error('Failed to fetch statistic:', error);
  }
};
