import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  Resolve,
  RouterStateSnapshot
} from '@angular/router';
import { Observable, of } from 'rxjs';
import { User } from 'src/modules/user/model/user.model';
import { UserService } from 'src/modules/user/service/user.service';

@Injectable()
export class DetailResolver implements Resolve<User> {
  /**
   * Constructor
   */
  constructor(private userService: UserService) {}

  /**
   * Resolver
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<User> | Promise<User> | any {
    const id = route.params.id;
    if ('0' === id) {
      return of({});
    }
    return this.userService.getUser(route.params.id);
  }
}
