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';

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

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