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

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

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<IGatewayModel>> | Promise<FleioObjectsList<IGatewayModel>> |
    FleioObjectsList<IGatewayModel> {
    return this.gatewaysApiService.list(
      route.queryParams as IListQueryParams
    ).pipe(catchError(() => of(null)));
  }
}
