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 { ILBAssociateFloatingIpOptions } from '@fleio-api/openstack/load-balancing/model/lb-associate-floating-ip-options.model';

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

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