import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { IPermissionsModel } from '../../base-model/IPermissionsModel';
import { catchError } from 'rxjs/operators';
import { JournalsApiService } from '@fleio-api/billing/journal/journal-api.service';

@Injectable({
  providedIn: 'root'
})
export class JournalPermissionsResolver implements Resolve<IPermissionsModel> {
  constructor(private journalApi: JournalsApiService) {
  }

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