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

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

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