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 { FleioObjectsList } from '../../fleio-objects-list';
import { PredefinedReplyCategoryApiService } from '@fleio-api/plugins/tickets/predefined-reply-category-api.service';
import { IPredefinedReplyCategoryModel } from '@fleio-api/plugins/tickets/model/predefined-reply-category.model';

@Injectable({
  providedIn: 'root'
})
export class PredefinedReplyCategoryListResolver  {
  constructor(private predefinedReplyCategoryApiService: PredefinedReplyCategoryApiService) {
  }

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