import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { UsersApiService } from '@fleio-api/client-user/user/users-api.service';
import { IUserCreateOptionsModel } from '@fleio-api/client-user/model/user-create-options.model';

@Injectable({
  providedIn: 'root'
})
export class UserCreateOptionsResolver  {
  constructor(private usersApiService: UsersApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IUserCreateOptionsModel> | Promise<IUserCreateOptionsModel> | IUserCreateOptionsModel {
    const createOpt = this.usersApiService.createOptions() as Observable<IUserCreateOptionsModel>;
    return createOpt.pipe(catchError(() => of(null)));
  }
}
