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 { IDashboard } from '../dashboard-component.model';
import { DashboardComponentService } from '../service/dashboard-component.service';

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

export default dashboardComponentResolve;
