import { useQuery } from 'react-query';

import useGeneralError from 'src/hooks/useGeneralError';
import { getDocument } from 'src/services/documents';
import { Document } from 'src/types/documents';
import { useLokaliseTranslation } from 'src/utils/i18n';

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

type UseGetDocumentProps = {
  documentId?: string;
};

const useGetDocument = ({ documentId }: UseGetDocumentProps) => {
  const showGeneralError = useGeneralError();
  const { t } = useLokaliseTranslation('documents');

  return useQuery<Document>({
    queryKey: documentsKeys.document.detail(documentId || ''),
    queryFn: () => getDocument(Number(documentId)).then(res => res.data),
    onError: err =>
      showGeneralError(err, t('error_loading_pending_document_item')),
    enabled: !!documentId,
  });
};

export default useGetDocument;
