import { useQuery } from 'react-query';

import { getEventGuests } from 'src/services/events';
import { Event } from 'src/types/events';
import { downloadFile } from 'src/utils/files';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { eventsKeys } from '../queries';

export const useDownloadGuestList = (event: Event) => {
  const { t } = useLokaliseTranslation('events');
  const { isLoading: isLoadingGuests, refetch } = useQuery(
    eventsKeys.eventGuests(event?.id),
    () => getEventGuests(event?.id),
    {
      select: r => r.data,
      enabled: false,
      onSuccess(data) {
        const fileName = `${event?.name}_${t('INVITATIONS')}.csv`;
        downloadFile(data, fileName);
      },
    },
  );

  return { isLoadingGuests, downloadGuestList: refetch };
};
