import { inject } from '@angular/core';
import { CanActivateFn } from '@angular/router';
import { map } from 'rxjs';
import { AuthService } from './auth.service';

export const roleGuard: CanActivateFn = (route) => {
  if (!route.data?.['roles']) {
    return true;
  }
  const routeRoles: string[] = route.data['roles'];
  const authService = inject(AuthService);
  return authService.roles$.pipe(
    map((userRoles) => routeRoles.some((role) => userRoles?.includes(role)))
  );
};
