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

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

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