import { useQuery } from 'react-query';

import { useAuth } from 'src/contexts/JWTContext';
import { orgChartKeys } from 'src/pages/dashboard/orgChart/queries';
import { getUserSubordinates } from 'src/services/orgChart';

export const useSubordinates = () => {
  const { user } = useAuth();

  const { data: subordinates } = useQuery(
    orgChartKeys.detailSubordinates(user && user.id ? user.id : ''),
    () => (user?.id ? getUserSubordinates(user.id) : undefined),
    {
      select: r => r?.data,
      enabled: !!user?.id,
    },
  );

  const hasSubordinates =
    Array.isArray(subordinates) && subordinates.length > 0;

  return {
    subordinates,
    hasSubordinates,
  };
};
