import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';
import { ILBRuleModel } from '@fleio-api/openstack/load-balancing/model/lb-rule.model';

@Injectable({
  providedIn: 'root'
})
export class LBRuleResolver  {
  constructor(private lbRulesApiService: LBRulesApiService) {
  }

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