import { useMutation } from 'react-query';

import { updatePartialSession } from '../services';
import { type UpdateLearningSession } from '../types';

import { useNewSessionStepper } from './useNewSessionStepper';

export const useSaveSessionStep = (sessionId?: number) => {
  const { next } = useNewSessionStepper();

  return useMutation({
    mutationFn: (data: UpdateLearningSession) => {
      if (!sessionId) throw new Error('Session ID is required');
      return updatePartialSession(sessionId, data);
    },
    onSuccess: () => next.handle(),
  });
};
