import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { LBMembersApiService } from '@fleio-api/openstack/load-balancing/members/lb-members-api.service';
import { ILBMemberModel } from '@fleio-api/openstack/load-balancing/model/lb-member.model';

@Injectable({
  providedIn: 'root'
})
export class LBMemberResolver  {
  constructor(private lbMembersApiService: LBMembersApiService) {
  }

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