import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IRoleModel } from '@fleio-api/client-user/roles/model/role.model';
import { RolesApiService } from '@fleio-api/client-user/roles/roles-api.service';

@Injectable({
  providedIn: 'root'
})
export class RoleResolver  {
  constructor(private rolesApiService: RolesApiService) {
  }

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