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 { IInputDataUploadLog } from '../input-data-upload-log.model';
import { InputDataUploadLogService } from '../service/input-data-upload-log.service';

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

export default inputDataUploadLogResolve;
