import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ILBPoolCreateOptionsModel } from '@fleio-api/openstack/load-balancing/model/lb-pool-create-options.model';
import { ILBHealthMonitorCreateOptionsModel } from '@fleio-api/openstack/load-balancing/model/lb-health-monitor-create-options.model';
import { LBHealthMonitorsApiService } from '@fleio-api/openstack/load-balancing/health-monitors/lb-health-monitors-api.service';

@Injectable({
  providedIn: 'root'
})
export class LBHealthMonitorCreateOptionsResolver  {
  constructor(private lbHealthMonitorsApiService: LBHealthMonitorsApiService) {
  }

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