import { useQuery } from 'react-query';

import { getCourseCategories } from 'src/services/courses';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { coursesKeys } from '../queries';
import {
  CategoryStatus,
  CourseCategories,
  type CourseCategory,
  CourseCategorySortingOrder,
  type GetCourseCategoriesParams,
} from '../types';

export const useGetCategories = () => {
  const { t } = useLokaliseTranslation('learning');

  const params: GetCourseCategoriesParams = {
    page: 1,
    limit: 100,
    sortingOption: CourseCategorySortingOrder.NAME_ASC,
    status: CategoryStatus.ACTIVE,
    includeCourses: false,
  };

  return useQuery({
    queryKey: coursesKeys.categories(),
    queryFn: () => getCourseCategories(params),
    select: response => {
      const categoryAll: CourseCategory = {
        id: Number(CourseCategories.ALL),
        name: t('path.tabs.all'),
        status: CategoryStatus.ACTIVE,
      };

      const categories = response?.data?.items ?? [];

      return [categoryAll, ...categories];
    },
  });
};
