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 news post to get its title
    const post = await getBySlug('neuigkeiten', params.slug, {
        where: { _status: { equals: 'published' } },
        depth: 1,
    }, fetch);

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

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