import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { PredefinedReplyCategoryApiService } from '@fleio-api/plugins/tickets/predefined-reply-category-api.service';
import { IPredefinedReplyModel } from '@fleio-api/plugins/tickets/model/predefined-reply.model';

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

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IPredefinedReplyModel> | Promise<IPredefinedReplyModel> | IPredefinedReplyModel {
    return this.predefinedReplyCategoryApiService.getPredefinedReply(
      route.params.id,
      route.params.replyId,
    ).pipe(catchError(() => of(null)));
  }
}
