import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ILBPolicyCreateOptionsModel } from '@fleio-api/openstack/load-balancing/model/lb-policy-create-options.model';
import { LBPoliciesApiService } from '@fleio-api/openstack/load-balancing/policies/lb-policies-api.service';

@Injectable({
  providedIn: 'root'
})
export class LBPolicyCreateOptionsResolver  {
  constructor(private lbPoliciesApiService: LBPoliciesApiService) {
  }

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