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 { IHeatTemplateVersionModel } from '@fleio-api/openstack/orchestration/model/heat-template-version.model';
import { HeatTemplateVersionsApiService } from '@fleio-api/openstack/orchestration/heat-template-versions/heat-template-versions-api.service';

@Injectable({
  providedIn: 'root'
})
export class HeatTemplateVersionListResolver  {
  constructor(private heatTemplateVersionsApiService: HeatTemplateVersionsApiService) {
  }

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