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

@Injectable({
  providedIn: 'root'
})
export class ActivityLogResolver  {
  constructor(private activityLogApiService: ActivityLogApiService) {
  }

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