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 { HeatStacksApiService } from '@fleio-api/openstack/orchestration/heat-stacks/heat-stacks-api.service';
import { IHeatStackModel } from '@fleio-api/openstack/orchestration/model/heat-stack.model';

@Injectable({
  providedIn: 'root'
})
export class HeatStackListResolver  {
  constructor(private heatStacksApiService: HeatStacksApiService) {
  }

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