import { useQuery } from 'react-query';

import { getCourseCategories } from 'src/pages/dashboard/Learning/Courses/services';
import { type RequestError, type RequestSuccess } from 'src/types/services';

import { type Category } from '../../Settings/Categories/types';
import { coursesKeys } from '../queries';
import { CourseCategorySortingOrder } from '../types';

export type TSuccess = RequestSuccess<typeof getCourseCategories>;
export type TError = RequestError;
export type TData = TSuccess;

// TODO: Migrate this hook to the new infinite query hook
// useGetInfiniteCourseCategories.ts
const useGetCourseCategories = () => {
  return useQuery<TSuccess, TError, Category[]>(
    coursesKeys.categories.list(),
    () =>
      getCourseCategories({
        page: 0,
        limit: 300,
        order: CourseCategorySortingOrder.NAME_ASC,
      }),
    {
      select: (response: TSuccess) => response.data.items,
    },
  );
};

export default useGetCourseCategories;
