import { useQuery } from 'react-query';

import { getAudience } from 'src/pages/dashboard/Learning/Paths/services';
import { sToMs } from 'src/utils/timeUtils';

import { pathsKeys } from '../queries';

const useGetAudience = (pathId: number | null) => {
  return useQuery({
    queryKey: pathsKeys.audience(pathId!),
    queryFn: () => {
      if (!pathId) throw new Error('Path ID is required');
      return getAudience(pathId);
    },
    enabled: !!pathId,
    select: res => res.data,
    staleTime: sToMs(60),
    onError: () => {},
  });
};

export default useGetAudience;
