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 { IUnitType } from '../unit-type.model';
import { UnitTypeService } from '../service/unit-type.service';

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

export default unitTypeResolve;
