import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { DbaasInstancesApiService } from '@fleio-api/openstack/dbaas/dbaas-instances-api.service';

@Injectable({
  providedIn: 'root'
})
export class DbaasInstanceResolver  {
  constructor(private dbaasInstancesApiService: DbaasInstancesApiService) {
  }

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