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 { IOutputData } from '../output-data.model';
import { OutputDataService } from '../service/output-data.service';

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

export default outputDataResolve;
