import { useMutation } from 'react-query';

import { downloadFile } from 'src/utils/files';

import { getSessionICS } from '../services';
import { type GetSessionICSParams } from '../types';

export const useGetSessionICS = () => {
  return useMutation({
    mutationFn: async ({ id, title }: GetSessionICSParams) => {
      if (!id) throw new Error('[useGetSessionICS] Session ID is required');
      const response = await getSessionICS(id);
      downloadFile(response.data, `${title}.ics`);
    },
  });
};
