import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ClusterTemplatesApiService } from '@fleio-api/openstack/cluster-template/cluster-templates-api.service';
import { IClusterTemplateCreateOptionsModel } from '@fleio-api/openstack/model/cluster-template-create-options.model';

@Injectable({
  providedIn: 'root'
})
export class ClusterTemplateCreateOptionsResolver  {
  constructor(private clusterTemplatesApiService: ClusterTemplatesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IClusterTemplateCreateOptionsModel> | Promise<IClusterTemplateCreateOptionsModel> |
    IClusterTemplateCreateOptionsModel {
    const params: {[key: string]: any} = {}
    if (route.params.clusterTemplateId) {
      params['cluster_template_id'] = route.params.clusterTemplateId;
    }
    return this.clusterTemplatesApiService.createOptions(params)
      .pipe(catchError(() => of(null))) as unknown as IClusterTemplateCreateOptionsModel;
  }
}
