import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { RbacPoliciesApiService } from '@fleio-api/openstack/rbac-policy/rbac-policies-api.service';
import { IRBACPolicyModel } from '@fleio-api/openstack/model/rbac-policy.model';

@Injectable({
  providedIn: 'root'
})
export class RbacPolicyResolver  {
  constructor(private rbacPoliciesApiService: RbacPoliciesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IRBACPolicyModel> | Promise<IRBACPolicyModel> | IRBACPolicyModel {
    return this.rbacPoliciesApiService.get(route.params.id).pipe(catchError(() => of(null)));
  }
}
