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 { IContentPage } from '../content-page.model';
import { ContentPageService } from '../service/content-page.service';

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

export default contentPageResolve;
