import { useQuery } from 'react-query';

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

export const useGetJourneyStep = (
  journeyId: number | undefined,
  stepId: number | undefined,
) => {
  const { data, ...query } = useQuery({
    queryKey: employeeLifecycleKeys.journeys.step(journeyId!, stepId!),
    queryFn: () => {
      if (!journeyId || !stepId) {
        throw new Error('Journey ID and step ID are required');
      }
      return getJourneyStep(journeyId, stepId);
    },
    select: response => response.data,
    enabled: !!journeyId && !!stepId,
    staleTime: EMPLOYEE_LIFECYCLE_STALE_TIME,
  });

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