import { CanActivateFn, Router } from "@angular/router";
import { AuthService } from "../services/auth.service";
import { inject } from "@angular/core";
import { HelpersService } from "../services/helpers.service";

export const authGuard: CanActivateFn = (route, state) => {
  const auth = inject(AuthService);
  const router = inject(Router);
  const helpers = inject(HelpersService);

  if (!auth.isAuthenticated()) {
    auth.removeToken();
    router.navigate(["/auth"], {
      queryParams: {
        url: state.url,
      },
    });
    helpers.toast("error", "Silakan login terlebih dahulu");
    return false;
  }

  return true;
};
