import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IContainerModel } from '@fleio-api/openstack/object-store/model/container.model';
import { ContainersApiService } from '@fleio-api/openstack/object-store/containers/containers-api.service';

@Injectable({
  providedIn: 'root'
})
export class ContainerResolver  {
  constructor(private containersApiService: ContainersApiService) {
  }

  // noinspection JSUnusedLocalSymbols
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IContainerModel> | Promise<IContainerModel> | IContainerModel {
    this.containersApiService.setAdditionalQueryParams(route.queryParams);
    return this.containersApiService.get(route.params.id).pipe(catchError(() => of(null)));
  }
}
