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

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

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<ILBListenerCreateOptionsModel> | Promise<ILBListenerCreateOptionsModel> | ILBListenerCreateOptionsModel {
    this.lbListenersApiService.setAdditionalQueryParams(route.queryParams);
    return this.lbListenersApiService.createOptions()
      .pipe(catchError(() => of(null))) as unknown as ILBListenerCreateOptionsModel;
  }
}
