import { useMutation } from 'react-query';

import useSnackbar from '@material-hu/components/design-system/Snackbar';

import { useLokaliseTranslation } from 'src/utils/i18n';
import { LogEvents, logEvent } from 'src/utils/logging';

import { createCategory } from '../services';
import { type CreateCategoryParams } from '../types';

const useCreateCategory = () => {
  const { t } = useLokaliseTranslation('learning');
  const { enqueueSnackbar } = useSnackbar();

  return useMutation({
    mutationFn: (params: CreateCategoryParams) => createCategory(params),
    onSuccess: ({ data: { id } }) => {
      logEvent(LogEvents.NEW_CATEGORY_CREATED, { categoryId: id });

      enqueueSnackbar({
        title: t('settings.categories.snackbar.create_success'),
        variant: 'success',
      });
    },
    onError: () => {
      enqueueSnackbar({
        title: t('settings.categories.snackbar.create_error'),
        variant: 'error',
      });
    },
  });
};

export default useCreateCategory;
