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

@Injectable({
  providedIn: 'root'
})
export class HeatResourceTypeListResolver  {
  constructor(private heatResourceTypesApiService: HeatResourceTypesApiService) {
  }

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