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 { ILoadBalancerModel } from '@fleio-api/openstack/load-balancing/model/load-balancer.model';
import { LoadBalancersApiService } from '@fleio-api/openstack/load-balancing/load-balancers/load-balancers-api.service';

@Injectable({
  providedIn: 'root'
})
export class LoadBalancerListResolver  {
  constructor(private loadBalancersApiService: LoadBalancersApiService) {
  }

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