---
import {type CollectionEntry, getCollection} from "astro:content";
import {astroI18n, createGetStaticPaths} from "astro-i18n";
import PageLayout from "../layouts/PageLayout.astro";
import LanguageWarning from "../components/LanguageWarning.astro";
import "@styles/table.css";

export const getStaticPaths = createGetStaticPaths(async () => {
    let posts = await getCollection("pages", value => value.id.split("/")[0] === astroI18n.locale);

    const germanPosts = await getCollection("pages", entry => entry.id.split("/")[0] === astroI18n.fallbackLocale);

    germanPosts.forEach(value => {
        if (posts.find(post => post.id.split("/")[1] === value.id.split("/")[1])) {
            return;
        } else {
            posts.push(value);
        }
    });

    function fixLink(page: CollectionEntry<"pages">): string {
        if (astroI18n.locale != astroI18n.primaryLocale && Object.keys(page.data.slugs ?? {}).includes(astroI18n.locale)) {
            return page.data.slugs[astroI18n.locale];
        }

        if (astroI18n.locales.includes(page.slug.split("/")[0])) {
            return page.slug.split("/").slice(1).join("/");
        } else {
            return page.slug;
        }
    }

    return posts.map((page) => ({
        props: {
            page,
            german: astroI18n.locale !== astroI18n.primaryLocale && page.id.split("/")[0] !== astroI18n.locale,
        },
        params: {
            slug: fixLink(page),
        },
    }));
});

const {page, german} = Astro.props as { page: CollectionEntry<"pages">, german: boolean };
const {Content} = await page.render();
---

<PageLayout title={page.data.title}>
    <article>
        {german && (
            <LanguageWarning/>
        )}
        <h1 class="text-left">{page.data.title}</h1>
        <Content/>
    </article>
</PageLayout>

<style is:global>
    article {
        > * {
            all: revert;
        }

        code {
            @apply dark:text-neutral-400 text-neutral-800;
        }

        pre.astro-code {
            @apply w-fit p-4 rounded-md border-2 border-gray-600 my-4;
        }

        a {
            @apply text-neutral-800 dark:text-neutral-400 hover:underline;
        }
    }
</style>
