import {CanActivateFn, Router} from '@angular/router';
import {inject} from '@angular/core';
import {LoginService} from '../services/login/login.service';
import {catchError, map} from 'rxjs';

export const isLoggedInGuard: CanActivateFn = (route, state) => {

  const loginService: LoginService = inject(LoginService);
  const router: Router = inject(Router);

  if (loginService.user() === undefined) {
    return loginService.getUsers().pipe(
      map(_ => {
        return true;
      }),
      catchError(_ => router.navigate(['login']))
    )
  }

  if (loginService.user() === null) {
    router.navigate(['login']).then();
  }

  return true;
};
