import { useQuery } from 'react-query';

import { getCourseSurvey } from 'src/services/courses';
import { type RequestError, type RequestSuccess } from 'src/types/services';

import { coursesKeys } from '../queries';
import { type Course } from '../types';
import { shouldGetSurvey } from '../utils';

export type TSuccess = RequestSuccess<typeof getCourseSurvey>;
export type TError = RequestError;
export type TData = TSuccess['data'];

const useGetCourseSurvey = (course: Course | undefined) => {
  return useQuery<TSuccess, TError, TData>(
    coursesKeys.detail.survey(course?.id ?? ''),
    () => getCourseSurvey(course?.id ?? ''),
    {
      select: response => response.data,
      enabled: shouldGetSurvey(course),
    },
  );
};

export default useGetCourseSurvey;
