import { useQuery, UseQueryOptions } from 'react-query';

import { getLibraryArticle } from 'src/services/libraryService';
import { RequestError } from 'src/types/services';

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

export type TSuccess = Awaited<ReturnType<typeof getLibraryArticle>>;
export type TError = RequestError;
export type TData = TSuccess['data'];

export const useGetLibraryArticle = (
  libraryId: number,
  options: UseQueryOptions<TSuccess, TError, TData> = {},
) => {
  return useQuery<TSuccess, TError, TData>(
    librariesKeys.library(libraryId),
    () => getLibraryArticle(libraryId),
    {
      ...options,
      enabled: !!libraryId,
      select: r => r.data,
    },
  );
};

export default useGetLibraryArticle;
