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

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

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