<script lang="ts">
    import { currentLang } from "../../store"
    import { getArticles } from "../../api"

    import Article from "../widgets/Article.svelte"

    export let tags: string[] = null
    export let pages: string[] = null
    export let path: string = null

    let articleEntries: CollectionEntry[] = []

    $: if ($currentLang) {
        let filter = {
            "article.general.locale": $currentLang,
        }

        if (tags && tags?.length) {
            filter["article.assignments.tags"] = { $in: tags }
        }

        if (pages && pages?.length) {
            filter["article.assignments.pages"] = { $in: pages }
        }

        if (path) {
            filter["article.assignments.pages"] = { $in: [path] }
        }

        getArticles("articles", { filter }).then((response) => {
            articleEntries = response
        })
    }
</script>

{#each articleEntries || [] as entry}
    <Article entry="{entry}" />
{/each}
