<script lang="ts">
    import type { PageData } from './$types';
    import SeasonLayout from '$lib/components/recipes/SeasonLayout.svelte';
    import CompactCard from '$lib/components/recipes/CompactCard.svelte';
    let { data } = $props<{ data: PageData }>();

    const isEnglish = $derived(data.lang === 'en');
    const months = $derived(isEnglish
        ? ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        : ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]);
    const siteTitle = $derived(isEnglish ? 'Bocken Recipes' : 'Bocken Rezepte');
    const currentMonth = $derived(months[data.month - 1]);

    import { rand_array } from '$lib/js/randomize';

    // Search state
    let matchedRecipeIds = $state(new Set());
    let hasActiveSearch = $state(false);

    // Handle search results from Search component
    function handleSearchResults(ids: Set<string>, categories: Set<string>) {
        matchedRecipeIds = ids;
        hasActiveSearch = ids.size < data.season.length;
    }

    // Filter recipes based on search
    const filteredRecipes: any[] = $derived.by(() => {
        if (!hasActiveSearch) {
            return data.season;
        }
        return data.season.filter((r: any) => matchedRecipeIds.has(r._id));
    });
</script>

<svelte:head>
    <title>{currentMonth} - {siteTitle}</title>
</svelte:head>

<SeasonLayout active_index={data.month -1} {months} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} onSearchResults={handleSearchResults}>
	{#snippet recipesSlot()}
		<div class="recipe-grid">
			{#each rand_array(filteredRecipes) as recipe (recipe._id)}
				<CompactCard {recipe} icon_override={true} isFavorite={recipe.isFavorite} showFavoriteIndicator={!!data.session?.user} routePrefix="/{data.recipeLang}" />
			{/each}
		</div>
	{/snippet}
</SeasonLayout>
