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

import { type AxiosResponse } from 'axios';

import { getPath } from 'src/pages/dashboard/learning/paths/services';
import { type RequestError, type RequestSuccess } from 'src/types/services';

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

export type TSuccess = RequestSuccess<typeof getPath>;
export type TError = RequestError;
export type TData = TSuccess['data'];
export type TParams = {
  pathId?: number;
  options?: UseQueryOptions<AxiosResponse<TData>, TError, TData>;
};

const useGetPath = ({ pathId, options }: TParams) => {
  return useQuery({
    queryKey: pathsKeys.detail.path(pathId),
    queryFn: () => {
      if (!pathId) throw new Error('[useGetPath]: Path ID is required');
      return getPath(pathId);
    },
    select: response => response.data,
    enabled: !!pathId,
    ...options,
  });
};

export default useGetPath;
