import { Navigate } from 'react-router-dom';

import {
  useGetManagerTypes,
  useVacations,
} from 'src/hooks/queryHooks/vacations';

const TimeOffManagerGuard = ({ children }: { children: React.ReactNode }) => {
  const { manager } = useVacations();

  const { isManager, isAdmin } = useGetManagerTypes();

  const hasManagerDelegation = manager?.hasRequests;
  const canViewManagerView = isManager || isAdmin || hasManagerDelegation;

  if (!canViewManagerView) {
    return <Navigate to="/" />;
  }

  return <>{children}</>;
};

export default TimeOffManagerGuard;
