import { useMutation } from 'react-query';

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

import { queryClient } from 'src/config/react-query';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { librariesKeys } from '../queries';
import { updateArticlesPositions } from '../services';

export const useBulkUpdatePosition = (parentId?: number) => {
  const { t } = useLokaliseTranslation('libraries');
  const { enqueueSnackbar } = useSnackbar();

  const queryKey = parentId
    ? librariesKeys.article.childrenTree(parentId)
    : librariesKeys.article.rootList();

  return useMutation({
    mutationFn: updateArticlesPositions,
    onSuccess: () => {
      enqueueSnackbar({
        title: t('library.move.success'),
        variant: 'success',
      });
    },
    onSettled: () => queryClient.invalidateQueries(queryKey),
  });
};
