import { useQuery } from 'react-query';

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

export const useArticleIcons = (articleId?: number) => {
  const { data: icons, ...query } = useQuery({
    queryKey: librariesKeys.article.icons(articleId),
    queryFn: () => {
      if (!articleId)
        throw new Error('[useArticleIcons] Article ID is required');
      return getArticleIcons(articleId);
    },
    select: response => response?.data ?? [],
    enabled: !!articleId,
  });

  return { icons: icons ?? [], ...query };
};
