import type { ReactNode } from 'react';
import { Navigate, useLocation } from 'react-router';
import { useAuth } from './AuthProvider';

export function RequirePermission({ anyOf, children }: { anyOf: readonly string[]; children: ReactNode }) {
  const auth = useAuth();
  const location = useLocation();
  return auth.hasPermission(...anyOf)
    ? children
    : <Navigate to="/forbidden" replace state={{ from: location.pathname }} />;
}
