import { useQuery } from 'react-query';

import useGeneralError from 'src/hooks/useGeneralError';
import { getArticle } from 'src/services/libraries';
import { useLokaliseTranslation } from 'src/utils/i18n';

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

export const useGetLibrary = (id: number | string, options?: object) => {
  const { t } = useLokaliseTranslation('libraries');
  const showGeneralError = useGeneralError();

  return useQuery(librariesKeys.detail(id), () => getArticle(id), {
    onError: err => showGeneralError(err, t('article.error')),
    select: response => response.data,
    ...options,
  });
};

export default useGetLibrary;
