import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ISfaTypeModel } from '@fleio-api/core/model/sfa-type.model';
import { SfaTypesApiService } from '@fleio-api/core/sfa-types-api.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';

@Injectable({
  providedIn: 'root'
})
export class SfaTypesResolver  {
  constructor(private sfaTypesApiService: SfaTypesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<ISfaTypeModel>> | Promise<FleioObjectsList<ISfaTypeModel>> |
    FleioObjectsList<ISfaTypeModel> {
    return this.sfaTypesApiService.list().pipe(catchError(() => of(null)));
  }
}
