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

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

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