import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IListQueryParams } from '../../base-model/list-query-params';
import { RevenueReportsApiService } from './revenue-reports-api.service';
import { IRevenueReportModel } from '../model/revenue-report.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';

@Injectable({
  providedIn: 'root'
})
export class RevenueReportsResolver  {
  constructor(private revenueReportsApiService: RevenueReportsApiService) {
  }

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