import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
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 LoadBalancerResolver  {
  constructor(private loadBalancersApiService: LoadBalancersApiService) {
  }

  // noinspection JSUnusedLocalSymbols
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<ILoadBalancerModel> | Promise<ILoadBalancerModel> | ILoadBalancerModel {
    this.loadBalancersApiService.setAdditionalQueryParams(route.queryParams);
    const loadBalancerId = route.params.id || route.params.loadBalancerId;
    return this.loadBalancersApiService.get(loadBalancerId).pipe(catchError(() => of(null)));
  }
}
