import type { LayoutLoad } from './$types';
import { getBySlug } from '$lib/utils/payload';
import type { BreadcrumbItem } from '$lib/types/breadcrumbs';

export const load: LayoutLoad = async ({ parent, params, fetch }) => {
    // Get parent data (includes breadcrumbs from root layout)
    const { breadcrumbs } = await parent();
    
    // Fetch the article to get its title
    const artikel = await getBySlug('artikel', params.slug, {
        depth: 2,
    }, fetch);

    // Replace the last breadcrumb label with the article title
    const updatedCrumbs = breadcrumbs?.crumbs || [];
    if (artikel && updatedCrumbs.length > 0) {
        updatedCrumbs[updatedCrumbs.length - 1] = {
            ...updatedCrumbs[updatedCrumbs.length - 1],
            label: artikel.title
        };
    }

    return {
        breadcrumbs: { crumbs: updatedCrumbs }
    };
};
