import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { map, take } from 'rxjs/operators';

import { UserService } from '@services';

export const adminGuard: CanActivateFn = () => {
    const router = inject(Router);
    const userService = inject(UserService);
    return userService.currentUser.pipe(take(1)).pipe(map(data => {
        if (data.role !== 'Admin') {
            router.navigate(['/']);
            return false;
        } else {
            return true;
        }
    }));
};
