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 { IEmailMessageLogModel } from '@fleio-api/logger/model/email-message-log.model';
import { EmailMessageLogApiService } from '@fleio-api/logger/emai-message-log-api.service';

@Injectable({
  providedIn: 'root'
})
export class EmailMessageLogListResolver  {
  constructor(private emailMessageLogApiService: EmailMessageLogApiService) {
  }

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