import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

import { AuthService } from '../service/auth.service';

@Injectable({
	providedIn: 'root',
})
export class AuthGuard implements CanActivate {
	constructor(
		private authService: AuthService,
		private router: Router
	) {
	}

	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
		if (this.authService.isAuthenticated) {
			return true;
		}
		this.router.navigate(['/authentication/signin'], { queryParams: { r: encodeURIComponent(state.url) } });
		return false;
	}
}
