import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IVolumeSnapshotModel } from '@fleio-api/openstack/model/volume-snapshot.model';
import { VolumeSnapshotsApiService } from '@fleio-api/openstack/volume/volume-snapshots-api.service';

@Injectable({
  providedIn: 'root'
})
export class VolumeSnapshotResolver  {
  constructor(private volumeSnapshotsApiService: VolumeSnapshotsApiService) {
  }

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