import { useMutation } from 'react-query';

import { assignUsersToPath } from 'src/pages/dashboard/Learning/Paths/services';

import { type SegmentationPayload } from '../../common/types';

import { useNewPathStepper } from './useNewPathStepper';

const useAssignUsersToPathMutation = (pathId: number | null) => {
  const { next } = useNewPathStepper();

  return useMutation({
    mutationFn: (data: SegmentationPayload) => {
      if (!pathId) throw new Error('Path ID is required');
      return assignUsersToPath(pathId, data);
    },
    onSuccess: () => {
      next.handle();
    },
  });
};

export default useAssignUsersToPathMutation;
