import { inject } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { of, EMPTY, Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

import { IVariableClassType } from '../variable-class-type.model';
import { VariableClassTypeService } from '../service/variable-class-type.service';

const variableClassTypeResolve = (route: ActivatedRouteSnapshot): Observable<null | IVariableClassType> => {
  const id = route.params['id'];
  if (id) {
    return inject(VariableClassTypeService)
      .find(id)
      .pipe(
        mergeMap((variableClassType: HttpResponse<IVariableClassType>) => {
          if (variableClassType.body) {
			const ut = of(variableClassType.body);
            return of(variableClassType.body);
          } else {
            inject(Router).navigate(['404']);
            return EMPTY;
          }
        }),
      );
  }
  return of(null);
};

export default variableClassTypeResolve;
