import { defineComponent, ref, Suspense, type VNode } from "vue";
import { type RouteRecordRaw, RouterLink, RouterView, useRoute } from "vue-router";
import GEMain from "@/generative-energy/GEMain.tsx";
import DjEmail from "@/DjEmail.tsx";
import GEDeutsch from "@/generative-energy/GEDeutsch.tsx";
import GEDeutschArticle from "@/generative-energy/GEDeutschArticle.tsx";
import GECalculator from "@/generative-energy/GECalculator.tsx";
import DjDonate from "@/DjDonate.tsx";
import { setupTooltip } from "@/DjTooltip.tsx";

export const routes: RouteRecordRaw[] = [
    {
        path: "/",
        name: "GEMain",
        component: GEMain,
    },
    {
        path: "/calculator",
        name: "GECalculator",
        component: GECalculator,
    },
    {
        path: "/raypeat-deutsch",
        name: "GEDeutsch",
        component: GEDeutsch,
    },
    {
        path: "/raypeat-deutsch/article/:articleName",
        name: "GEDeutschArticle",
        component: GEDeutschArticle,
        props: ({ params }) => {
            if ("articleName" in params) {
                return { articleName: params.articleName };
            } else {
                return false;
            }
        },
    },
];

export default defineComponent({
    name: "ge-root",
    setup() {
        const route = useRoute();
        const carrier = ref<HTMLDivElement | null>(null);
        setupTooltip({ carrier });
        return () => (
            <>
                <div ref={carrier} class="tooltip-carrier" />
                <main>
                    <RouterLink class={"home-btn" + (route.name === "GEMain" ? " hide" : "")} to={{ name: "GEMain" }}>
                        Generative Energy Home
                    </RouterLink>
                    <RouterView>
                        {{
                            default: ({ Component }: { Component: VNode }) => (Component &&
                                (
                                    <Suspense>
                                        {{
                                            default: () => Component,
                                            fallback: () => <div>Page loading...</div>,
                                        }}
                                    </Suspense>
                                )),
                        }}
                    </RouterView>
                    <footer>
                        <div class="bottom">
                            <div>
                                <a href="/">djledda.net</a> {new Date().getFullYear()} - <DjEmail>{() => "Contact"}</DjEmail>
                            </div>
                            <DjDonate />
                        </div>
                    </footer>
                </main>
            </>
        );
    },
});
