import { createRootRouteWithContext, Outlet, redirect } from "@tanstack/react-router";
import type { RouterContext } from "@/router";

export const Route = createRootRouteWithContext<RouterContext>()({
  beforeLoad: async ({ context, location }) => {
    if (!context.auth.isAuthenticated && location.pathname !== "/login") {
      throw redirect({ to: "/login" });
    }
    if (context.auth.isAuthenticated && location.pathname === "/login") {
      throw redirect({ to: "/" });
    }
  },
  component: Outlet,
});
