<script lang="ts">
    import { getDBEntries } from "../../api"
    import { spaLink } from "../actions"
    import { navigationCache } from "../store"
    import CrinkledSection from "./CrinkledSection.svelte"

    let navigationEntries: NavigationEntry[] = []

    function elementsToCache(elements: NavigationElement[]) {
        elements.forEach((el) => {
            if (!el.external) {
                if (!$navigationCache[el.page]) $navigationCache[el.page] = el
                if (el.elements?.length > 0) elementsToCache(el.elements)
            }
        })
    }

    getDBEntries("navigation").then((navs) => {
        navigationEntries = navs
        navigationEntries.filter((nav) => "elements" in nav).forEach((nav) => elementsToCache(nav.elements))
    })
</script>

<CrinkledSection brightBackground={false}>
    {#snippet contentSnippet()}
        <footer class="footer">
            <section id="legal-section">
                <div class="wrapper">
                    <small class="">© 2025 | KontextWerk | Alle Rechte vorbehalten.</small>
                    <nav class="nav-points">
                        <ul>
                            {#each navigationEntries.length ? navigationEntries[0].elements : [] as link}
                                <li>
                                    <a
                                        class="footer-nav-point-bottom"
                                        use:spaLink
                                        href={link.page}
                                    >
                                        <small>{link.name}</small>
                                    </a>
                                </li>
                            {/each}
                        </ul>
                    </nav>
                </div>
            </section>
        </footer>
    {/snippet}
</CrinkledSection>

<style lang="less">
    @import "../../lib/assets/css/variables.less";
    .footer {
        &,
        & * {
            box-sizing: border-box;
        }
        background: var(--neutral-white);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        position: relative;
        width: 100%;
        gap: 1.5rem;
        #legal-section {
            display: flex;
            justify-content: center;
            width: 100%;
            background: var(--bg-100);
            gap: 1.2rem;
            .wrapper {
                max-width: var(--small-max-width);

                width: 100%;
                a,
                small {
                    color: var(--text-100);
                }

                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: space-between;
                flex-shrink: 0;
                position: relative;
                gap: 1.2rem;
                padding: 1.5rem var(--horizontal-default-margin);
                @media @mobile {
                    flex-direction: column;
                    align-items: flex-start;
                }

                .nav-points {
                    ul {
                        display: flex;
                        flex-wrap: wrap;
                        gap: 0.6rem;
                        align-items: center;
                        a {
                            font-weight: 400;
                            font-family: Outfit;
                        }
                    }
                }
            }
        }
    }
</style>
