import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { Observable, of } from 'rxjs';
import { IListQueryParams } from '@fleio-api/base-model/list-query-params';
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 RbacPoliciesListResolver  {
  constructor(private rbacPoliciesApiService: RbacPoliciesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<IRBACPolicyModel>> | Promise<FleioObjectsList<IRBACPolicyModel>> |
    FleioObjectsList<IRBACPolicyModel> {
    return this.rbacPoliciesApiService.list(
      route.queryParams as IListQueryParams
    ).pipe(catchError(() => of(null)));
  }
}
