import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import { Observable } from 'rxjs';
import {Store} from "@ngxs/store";
import {UserState} from "../states/user.state";

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor(private router: Router, private store: Store) {
  }
  // @ts-ignore
  canActivate(){
    const token = this.store.selectSnapshot(UserState.getToken);
    const userroles = this.store.selectSnapshot(UserState.getRoles);
    if (token == null) {
      this.router.navigate(['/login']);
    }
    else if (userroles?.includes('ADMIN')) {
      return true;
    }else {
      this.router.navigate(['/home']);
    }
  }
  }



