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 { ISecurityGroup } from '../security-group.model';
import { SecurityGroupService } from '../service/security-group.service';

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

export default securityGroupResolve;
