<!--
    Component: FontVirtualList
    - Renders a virtualized list of fonts
    - Handles font registration with the manager
-->
<script lang="ts">
import {
    Skeleton,
    VirtualList,
} from '$shared/ui';
import type { ComponentProps } from 'svelte';
import { fade } from 'svelte/transition';
import { getFontUrl } from '../../lib';
import {
    type FontConfigRequest,
    type UnifiedFont,
    appliedFontsManager,
    unifiedFontStore,
} from '../../model';

interface Props extends
    Omit<
        ComponentProps<typeof VirtualList<UnifiedFont>>,
        'items' | 'total' | 'isLoading' | 'onVisibleItemsChange' | 'onNearBottom'
    >
{
    /**
     * Callback for when visible items change
     */
    onVisibleItemsChange?: (items: UnifiedFont[]) => void;
    /**
     * Weight of the font
     */
    /**
     * Weight of the font
     */
    weight: number;
}

let {
    children,
    onVisibleItemsChange,
    weight,
    ...rest
}: Props = $props();

const isLoading = $derived(
    unifiedFontStore.isFetching || unifiedFontStore.isLoading,
);

function handleInternalVisibleChange(visibleItems: UnifiedFont[]) {
    const configs: FontConfigRequest[] = [];

    visibleItems.forEach(item => {
        const url = getFontUrl(item, weight);

        if (url) {
            configs.push({
                id: item.id,
                name: item.name,
                weight,
                url,
                isVariable: item.features?.isVariable,
            });
        }
    });
    // Auto-register fonts with the manager
    appliedFontsManager.touch(configs);

    // Forward the call to any external listener
    // onVisibleItemsChange?.(visibleItems);
}

/**
 * Load more fonts by moving to the next page
 */
function loadMore() {
    if (
        !unifiedFontStore.pagination.hasMore
        || unifiedFontStore.isFetching
    ) {
        return;
    }
    unifiedFontStore.nextPage();
}

/**
 * Handle scroll near bottom - auto-load next page
 *
 * Triggered by VirtualList when the user scrolls within 5 items of the end
 * of the loaded items. Only fetches if there are more pages available.
 */
function handleNearBottom(_lastVisibleIndex: number) {
    const { hasMore } = unifiedFontStore.pagination;

    // VirtualList already checks if we're near the bottom of loaded items
    if (hasMore && !unifiedFontStore.isFetching) {
        loadMore();
    }
}
</script>

{#key isLoading}
    <div class="relative w-full h-full" transition:fade={{ duration: 300 }}>
        {#if isLoading}
            <div class="flex flex-col gap-3 sm:gap-4 p-3 sm:p-4">
                {#each Array(5) as _, i}
                    <div class="flex flex-col gap-1.5 sm:gap-2 p-3 sm:p-4 border rounded-lg sm:rounded-xl border-border-subtle bg-background-40">
                        <div class="flex items-center justify-between mb-3 sm:mb-4">
                            <Skeleton class="h-6 sm:h-8 w-1/3" />
                            <Skeleton class="h-6 sm:h-8 w-6 sm:w-8 rounded-full" />
                        </div>
                        <Skeleton class="h-24 sm:h-32 w-full" />
                    </div>
                {/each}
            </div>
        {:else}
            <VirtualList
                items={unifiedFontStore.fonts}
                total={unifiedFontStore.pagination.total}
                onVisibleItemsChange={handleInternalVisibleChange}
                onNearBottom={handleNearBottom}
                {...rest}
            >
                {#snippet children(scope)}
                    {@render children(scope)}
                {/snippet}
            </VirtualList>
        {/if}
    </div>
{/key}
