<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>
    {#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;
        #content-section {
            padding: 0px var(--horizontal-default-margin);
            max-width: var(--small-max-width);
            width: 100%;
            display: flex;
            flex-direction: row;
            gap: 1.5rem;
            align-items: flex-start;
            justify-content: flex-start;
            position: relative;
            #content-link-section {
                display: flex;
                flex-direction: row;
                gap: 1.5rem;
                align-items: flex-start;
                justify-content: flex-start;
                flex: 1;
                position: relative;
                .content {
                    display: flex;
                    flex-direction: column;
                    gap: 1.125rem;
                    align-items: flex-start;
                    justify-content: flex-start;
                    width: 8rem;
                    position: relative;
                }
            }
            @media @mobile {
                flex-direction: column;
                align-items: flex-start;
                #content-link-section {
                    flex-direction: column;
                }
            }
        }
        #newsletter-section {
            display: flex;
            flex-direction: column;
            gap: 18px;
            min-width: 300px;
            form {
                button {
                    width: fit-content;
                }
            }
        }
        #icons-section {
            padding: 0px var(--horizontal-default-margin);
            max-width: var(--small-max-width);
            width: 100%;
            margin: 0 0 0 -0.0625rem;
            display: flex;
            gap: 0.625rem;
            align-items: flex-start;
            justify-content: flex-start;
            flex: 1;
            position: relative;
            overflow: hidden;
            .payments,
            .social {
                display: flex;
                gap: 0.75rem;
                figure {
                    height: 1.2rem;
                    img {
                        height: 100%;
                        width: auto;
                    }
                }
            }
            .line {
                padding: 0rem 1.5rem 0rem 1.5rem;
                display: flex;
                flex-direction: row;
                gap: 1.5rem;
                align-items: center;
                justify-content: flex-start;
                flex: 1;
                position: relative;
                overflow: hidden;
                .line-1,
                .line-2 {
                    border-style: solid;
                    border-color: var(--text-invers-100);
                    border-width: 0.0625rem 0 0 0;
                    flex: 1;
                    height: 0rem;
                    position: relative;
                }
            }
            @media @mobile {
                flex-direction: column-reverse;
                align-items: center;
                gap: 2rem;
                .line {
                    width: 100%;
                    padding: 0px;
                }
            }
        }

        #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>
