import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { IMessageTemplateModel } from './model/message-template.model';
import { MessageTemplatesApiService } from './message-templates-api.service';

@Injectable({
  providedIn: 'root'
})
export class MessageTemplateResolver  {
  constructor(private messageTemplatesApiService: MessageTemplatesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IMessageTemplateModel[]> | Promise<IMessageTemplateModel[]> | IMessageTemplateModel[] {
    return this.messageTemplatesApiService.getEditableMessageTemplate(
      route.params.id
    ).pipe(catchError(() => of(null))).pipe(map((response: any) => response.objects));
  }
}
