import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';
import { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';

@Injectable({
  providedIn: 'root'
})
export class LBPoolResolver  {
  constructor(private lbPoolsApiService: LBPoolsApiService) {
  }

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