import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { FleioObjectsList } from '../fleio-objects-list';
import { INotificationModel } from './model/notification.model';
import { UserNotificationsApiService } from './user-notifications-api.service';
import { Observable, of } from 'rxjs';
import { IListQueryParams } from '../base-model/list-query-params';
import { catchError } from 'rxjs/operators';


@Injectable({
  providedIn: 'root'
})
export class UserNotificationsListResolver  {
  constructor(private userNotificationsApiService: UserNotificationsApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<INotificationModel>> | Promise<FleioObjectsList<INotificationModel>> |
    FleioObjectsList<INotificationModel> {
    const queryParams = JSON.parse(JSON.stringify(route.queryParams));
    if (queryParams && !queryParams.hasOwnProperty('name')) {
      queryParams.name = 'frontend';
    }
    return this.userNotificationsApiService.list(
      queryParams as IListQueryParams
    ).pipe(catchError(() => of(null)));
  }
}
