import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { IClientModel } from '../model/client.model';
import { ClientsApiService } from './clients-api.service';
import { catchError } from 'rxjs/operators';
import { AuthService } from '@shared/auth/auth.service';
import {
  IUserNotificationSettingsResponseModel
} from '@fleio-api/notifications/model/user-notification-settings-response.model';

@Injectable({
  providedIn: 'root'
})
export class UserClientNotificationsSettingsResolver  {
  constructor(
    private clientsApiService: ClientsApiService,
    private authService: AuthService,
  ) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IUserNotificationSettingsResponseModel> |
    Promise<IUserNotificationSettingsResponseModel> | IUserNotificationSettingsResponseModel {
    return this.clientsApiService.getUserNotificationsSettings(
      route.params.id, this.authService.userData.user.id, true,
    ).pipe(catchError(() => of(null)));
  }
}
