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 { ILBPoolCreateOptionsModel } from '@fleio-api/openstack/load-balancing/model/lb-pool-create-options.model';

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

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