import { useQuery } from 'react-query';

import { EMPLOYEE_LIFECYCLE_STALE_TIME } from '../constants';
import { employeeLifecycleKeys } from '../queries';
import { getJourneyTask } from '../services';

export const useGetJourneyTask = (taskId: number | null) => {
  const { data, ...query } = useQuery({
    queryKey: employeeLifecycleKeys.journeys.stepTask(taskId!),
    queryFn: () => {
      if (!taskId) throw new Error('Task ID is required');
      return getJourneyTask(taskId);
    },
    enabled: !!taskId,
    select: response => response.data,
    staleTime: EMPLOYEE_LIFECYCLE_STALE_TIME,
  });

  return { task: data, ...query };
};
