import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IHypervisorModel } from '@fleio-api/openstack/model/hypervisor.model';
import { HypervisorApiService } from '@fleio-api/openstack/hypervisor/hypervisor-api.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IListQueryParams } from '@fleio-api/base-model/list-query-params';

@Injectable({
  providedIn: 'root'
})
export class HypervisorsListResolver  {
  constructor(private hypervisorApiService: HypervisorApiService) {
  }

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