import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { FleioObjectsList } from '../fleio-objects-list';
import { IListQueryParams } from '../base-model/list-query-params';
import { catchError } from 'rxjs/operators';
import { IPeriodicTaskLogModel } from '@fleio-api/logger/model/periodic-task-log.model';
import { PeriodicTaskLogApiService } from '@fleio-api/logger/periodic-task-log-api.service';

@Injectable({
  providedIn: 'root'
})
export class PeriodicTaskLogListResolver  {
  constructor(private periodicTaskLogApiService: PeriodicTaskLogApiService) {
  }

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