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

@Injectable({
  providedIn: 'root'
})
export class VolumeTypesListResolver  {
  constructor(private volumeTypesApiService: VolumeTypesApiService) {
  }

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