import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IProjectModel } from '@fleio-api/openstack/project/model/project.model';
import { ProjectsApiService } from '@fleio-api/openstack/project/projects-api.service';

@Injectable({
  providedIn: 'root'
})
export class ProjectResolver  {
  constructor(private projectsApiService: ProjectsApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IProjectModel> | Promise<IProjectModel> | IProjectModel {
    return this.projectsApiService.get(route.params.id).pipe(catchError(() => of(null)));
  }
}
