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 { IVariableCategory } from '../variable-category.model';
import { VariableCategoryService } from '../service/variable-category.service';

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

export default variableCategoryResolve;
