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

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

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