import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
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 ContainerListResolver  {
  constructor(private containersApiService: ContainersApiService) {
  }

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