<script>
import { onMount } from 'svelte';
import { getScrollBreadcrumbsStore } from '../../model';
import BreadcrumbHeader from './BreadcrumbHeader.svelte';

const scrollBreadcrumbsStore = getScrollBreadcrumbsStore();

const sections = [
    { index: 100, title: 'Introduction' },
    { index: 101, title: 'Typography' },
    { index: 102, title: 'Spacing' },
];

/** @type {HTMLDivElement} */
let container;

onMount(() => {
    for (const section of sections) {
        const el = /** @type {HTMLElement} */ (container.querySelector(`[data-story-index="${section.index}"]`));
        scrollBreadcrumbsStore.add({ index: section.index, title: section.title, element: el }, 96);
    }

    /*
     * Scroll past the sections so IntersectionObserver marks them as
     * scrolled-past, making scrolledPastItems non-empty and the header visible.
     */
    setTimeout(() => {
        window.scrollTo({ top: 2000, behavior: 'instant' });
    }, 100);

    return () => {
        for (const { index } of sections) {
            scrollBreadcrumbsStore.remove(index);
        }
        window.scrollTo({ top: 0, behavior: 'instant' });
    };
});
</script>

<div bind:this={container} style="height: 2400px; padding-top: 900px;">
    {#each sections as section}
        <div
            data-story-index={section.index}
            style="height: 400px; padding: 2rem; background: #f5f5f5; margin-bottom: 1rem;"
        >
            {section.title} — scroll up to see the breadcrumb header
        </div>
    {/each}
</div>

<BreadcrumbHeader />
