import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ITaxRuleCreateOptionsModel } from '@fleio-api/billing/model/tax-rule-create-options.model';
import { TaxRulesApiService } from '@fleio-api/billing/tax-rules/tax-rules-api.service';

@Injectable({
  providedIn: 'root'
})
export class TaxRuleCreateOptionsResolver  {
  constructor(private taxRulesApiService: TaxRulesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<ITaxRuleCreateOptionsModel> | Promise<ITaxRuleCreateOptionsModel> | ITaxRuleCreateOptionsModel {
    const createOpt = this.taxRulesApiService.createOptions() as Observable<ITaxRuleCreateOptionsModel>;
    return createOpt.pipe(catchError(() => of(null)));
  }
}
