import { notFound } from 'next/navigation' import type { ReactNode } from 'react' import { getConsentTexts, getAnalyticsConfig, getSystemPagePath, } from '@intecion/ipal-kit' import { ConsentProvider, CookieButton, Analytics, } from '@intecion/ipal-kit/client' import { i18nConfig } from '@/i18n.config' import { getCachedPayload, getConfiguredLocales } from '@/lib/content' import { getSettings } from '@/lib/payload' import { Header, type NavItem } from '@/components/Header' import { Footer, type FooterNavItem } from '@/components/Footer' import { CustomCookieBanner } from '@/components/CustomCookieBanner' import '../styles.css' import type { Company, Navigation, Page } from '@/payload-types' interface LayoutProps { children: ReactNode params: Promise<{ locale: string }> } function resolveNavItemHref(item: { page?: Page | number | string | null; url?: string | null }): string { if (item.url) return item.url if (item.page && typeof item.page === 'object' && 'slug' in item.page && item.page.slug) { return item.page.slug === 'home' ? '/' : `/${item.page.slug}` } return '#' } export default async function LocaleLayout({ children, params }: LayoutProps) { const { locale } = await params const locales = await getConfiguredLocales() if (!locales.includes(locale)) notFound() const payload = await getCachedPayload() const settings = await getSettings(locale) const privacyPage = (settings as { privacyPolicy?: unknown })?.privacyPolicy const cookiePage = (settings as { cookiePolicy?: unknown })?.cookiePolicy const homePage = (settings as { homepage?: unknown })?.homepage let company: Company | null = null let navigation: Navigation | null = null try { const [compDoc, navDoc] = await Promise.all([ payload.findGlobal({ slug: 'company', locale: locale as never }), payload.findGlobal({ slug: 'navigation', locale: locale as never, depth: 2 }), ]) company = compDoc navigation = navDoc } catch { // fallback if globals not yet created } const privacyHref = privacyPage ? getSystemPagePath({ config: i18nConfig, locale, page: privacyPage }) : undefined const cookieHref = cookiePage ? getSystemPagePath({ config: i18nConfig, locale, page: cookiePage }) : undefined const homeHref = homePage ? getSystemPagePath({ config: i18nConfig, locale, page: homePage }) || '/' : '/' const [texts, analytics] = await Promise.all([ getConsentTexts({ config: i18nConfig, locale, payload, privacyPolicy: privacyPage && typeof privacyPage === 'object' ? { page: privacyPage, label: 'Polityka prywatności' } : undefined, }), getAnalyticsConfig(payload), ]) const headerItems: NavItem[] = navigation?.headerItems?.map((item) => ({ id: item.id || undefined, label: item.label, href: resolveNavItemHref(item), })) || [] const footerItems: FooterNavItem[] = navigation?.footerItems?.map((item) => ({ id: item.id || undefined, label: item.label, href: resolveNavItemHref(item), })) || [] return (