import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import {
  TicketStaffNotificationsSettingsApiService
} from '@fleio-api/plugins/tickets/ticket-staff-notifications-settings-api.service';

@Injectable({
  providedIn: 'root'
})
export class TicketStaffNotificationsSettingsResolver  {
  constructor(private ticketStaffNotificationsSettingsApiService: TicketStaffNotificationsSettingsApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<any>> |
    Promise<FleioObjectsList<any>> |
    FleioObjectsList<any> {
    return this.ticketStaffNotificationsSettingsApiService.getCurrentUserSettings().pipe(
      catchError((err, caught) => {
        if (err.status === 403) {
          // prevent navigation
          throw err;
        }
        return of(null);
      })
    );
  }
}
