import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router';
import { firstValueFrom } from 'rxjs';
import { EntityPermissionsService } from '@services/entity-permissions/entity-permissions.service';

@Injectable({ providedIn: 'root' })
export class PermissionsGuard implements CanActivate {
    constructor(private readonly _entityPermissionsService: EntityPermissionsService) {}

    async canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
        const targetObject: string = route.data.permissionsObject;
        await firstValueFrom(this._entityPermissionsService.loadConfigFor(targetObject));
        await firstValueFrom(this._entityPermissionsService.loadFor(targetObject));
        return true;
    }
}
