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();

    const person = await getBySlug('persoenlichkeiten', params.slug, {}, fetch);

    // Replace the last breadcrumb label with the person's name
    const updatedCrumbs = breadcrumbs?.crumbs || [];
    if (person && updatedCrumbs.length > 0) {
        updatedCrumbs[updatedCrumbs.length - 1] = {
            ...updatedCrumbs[updatedCrumbs.length - 1],
            label: person.name || params.slug
        };
    }

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