import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IGatewayModel } from '../model/gateway.model';
import { GatewaysApiService } from '@fleio-api/billing/gateways/gateways-api.service';

@Injectable({
  providedIn: 'root'
})
export class GatewayResolver  {
  constructor(private gatewaysApiService: GatewaysApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IGatewayModel> | Promise<IGatewayModel> | IGatewayModel {
    return this.gatewaysApiService.get(route.params.id).pipe(catchError(() => of(null)));
  }
}
