import {createRootRoute, redirect} from "@tanstack/react-router";
import RootPage from "../pages/RootPage/RootPage.tsx";
import {SearchParams} from "../shared/lib/general.ts";

export const Route = createRootRoute({
    component: RootPage,
    beforeLoad: async ({location, search}) => {
        const isAuthorized = JSON.parse(localStorage.getItem('authState') || '{}')['isAuthorized'];
        const searchParams: SearchParams = search;
        if (searchParams.accessToken) {
            return;
        }
        if (!isAuthorized && location.pathname !== '/login') {
            throw redirect({
                to: '/login',
            })
        }
    }
})