<script lang="ts">
    import { notifications, openModal, removeNotification } from "../../store"
    import Icon from "./Icon.svelte"
    import { mdiWindowClose } from "@mdi/js"
    export let force = false

    export function animateIt(node: HTMLLIElement) {
        node.classList.add("activate")
        return {}
    }
</script>

{#each $notifications as n (n)}
    {#if n.blockUI}
        <div class="blocker"></div>
    {/if}
{/each}

<ul
    class="container"
    class:inModal={force}
>
    {#each $notifications as n (n)}
        {#if !$openModal || ($openModal && force)}
            <!-- svelte-ignore a11y-no-noninteractive-element-to-interactive-role -->
            <li
                class={`notification ${n.class}`}
                use:animateIt
                on:click={() => {
                    removeNotification(n)
                }}
                role="button"
                tabindex={0}
                on:keydown={(event) => {
                    if (event.key === 'Enter' || event.key === ' ') {
                        removeNotification(n)
                    }
                }}
            >
                <button
                    class="close"
                    aria-label="Close notification"
                    on:click={() => {
                        removeNotification(n)
                    }}
                >
                    <Icon path={mdiWindowClose} />
                </button>
                {@html n.html}
            </li>
        {/if}
    {/each}
</ul>

<style lang="less">
    .container {
        position: fixed;
        top: 80px;
        right: 20px;
        width: 300px;
        opacity: 0.9;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        gap: 0.6rem;
        align-items: flex-end;
        .notification:nth-child(1) {
            margin-top: 0px;
        }
        @media (max-width: 768px) {
            width: auto;
            left: 20px;
        }
    }

    .notification {
        padding: 20px;
        border-radius: 5px;
        z-index: 9999;
        margin-top: 12px;
        position: relative;
        max-width: 300px;
        width: fit-content;

        &.info {
            background-color: #464646;
            color: white;
        }

        &.success {
            background-color: rgb(143, 202, 143);
            color: black;
        }

        &.warning {
            background-color: rgb(219, 187, 126);
            color: black;
        }

        &.error {
            background-color: darkred;
            color: white;
        }
        &.activate {
            animation: fadeinUp 0.5s; /* 'forwards' behält den Endzustand der Animation bei */
        }
        @keyframes fadeinUp {
            from {
                position: absolute; /* Geändert zu 'absolute' für bessere Kontrolle */
                transform: translateY(100%); /* Initial außerhalb des Sichtbereichs */
                opacity: 0; /* Set initial opacity to 0 */
                width: fit-content;
            }
            to {
                position: absolute; /* Geändert zu 'absolute' für bessere Kontrolle */
                opacity: 1;
                transform: translateY(0); /* Endposition */
                width: fit-content;
            }
        }
        .close {
            position: absolute;
            top: 5px;
            right: 5px;
            cursor: pointer;
            opacity: 0.5;
        }
    }

    .blocker {
        position: absolute;
        background: #525151b8;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 2000;
    }
    .inModal {
        top: initial;
        top: calc(50% - 50vh + 100px) !important;
        right: calc(50% - 50vw) !important;
    }
</style>
