import { useQuery } from 'react-query';

import { useAuth } from 'src/contexts/JWTContext';
import { getAgent } from 'src/pages/dashboard/serviceManagement/services';

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

const useAgent = () => {
  const { user } = useAuth();

  const {
    data: agent,
    isLoading: isLoadingAgent,
    error: errorFetchingAgent,
  } = useQuery(
    serviceManagementKeys.agents.detail(user!.id),
    () => getAgent(user!.id),
    {
      select: response => response.data,
      refetchOnMount: false,
    },
  );

  const isCoordinator = !!agent?.isCoordinator;

  return {
    agent,
    isLoadingAgent,
    errorFetchingAgent,
    isCoordinator,
  };
};

export default useAgent;
