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

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

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