import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
import { LBListenersApiService } from '@fleio-api/openstack/load-balancing/listeners/lb-listeners-api.service';

@Injectable({
  providedIn: 'root'
})
export class LBListenerResolver  {
  constructor(private lbListenersApiService: LBListenersApiService) {
  }

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