import { useUserStore } from "@/store";
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Components from "./imports";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: "HomeView",
        component: Components.HomeViewMain,
        children: [
            {
                path: "home",
                component: Components.HomeMain,
            },
            {
                path: "menu",
                component: Components.MenuMain,
            },
            {
                path: "hookah",
                component: Components.HookahMain,
                children: [
                    { path: "table", component: Components.hookah.TableTab },
                    { path: "heaviness", component: Components.hookah.HeavinessTab },
                    { path: "taste", component: Components.hookah.TasteTab },
                    { path: "summary", component: Components.hookah.SummaryTab },
                ],
            },
            {
                path: "orders",
                component: Components.OrdersMain,
            },
            {
                path: "user",
                component: Components.UserMain,
            },
        ],
    },
    {
        path: "/login",
        component: () => import("@/components/Authorization/AuthorizationMain.vue"),
        props: { type: "login" },
    },
    {
        path: "/register",
        component: () => import("@/components/Authorization/AuthorizationMain.vue"),
        props: { type: "register" },
    },
    {
        path: "/intro",
        component: () => import("@/components/Intro/IntroMain.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

router.beforeEach((to) => {
    if (to.path === "/") {
        return "/home";
    }
    if (["/login", "/register"].includes(to.path) && useUserStore().isLoggedIn) {
        return "/home";
    }
    if (to.path !== "/intro" && !useUserStore().introWatched) {
        return "/intro";
    }
});

export default router;
