import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { FeaturesService } from '@services/features.service';

@Injectable({ providedIn: 'root' })
export class FeaturesGuard implements CanActivate {
    constructor(private readonly _router: Router, private readonly _featuresService: FeaturesService) {}

    async canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
        const features: string[] = route.data.features || [];

        if (features.find(feature => !this._featuresService.isEnabled(feature))) {
            await this._router.navigate(['']);
            return false;
        }

        return true;
    }
}
