import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IActiveTosResponseModel } from '@fleio-api/core/model/active-tos-response.model';
import { TosAgreementsApiService } from '@fleio-api/core/tos-agreements-api.service';

@Injectable({
  providedIn: 'root'
})
export class ActiveTosResolver  {
  constructor(private tosApiService: TosAgreementsApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IActiveTosResponseModel> | Promise<IActiveTosResponseModel> | IActiveTosResponseModel {
    return this.tosApiService.getActiveTOS().pipe(catchError(() => of(null)));
  }
}
