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 { IOrganizationType } from '../organization-type.model';
import { OrganizationTypeService } from '../service/organization-type.service';

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

export default organizationTypeResolve;
