import type { I18nConfig } from '../i18n/index.js' import type { SystemPageRole } from './types.js' import { buildLocalizedPath, getLocalizedSlugs } from '../i18n/index.js' /** * The shape we need from an assigned system-page document: its localized slug * field, as returned by Payload when the parent is read with `locale: 'all'`. * * The plugin doesn't know the client's Pages type, so it depends only on this * minimal contract. */ type AssignedPage = { slug?: null | Record } type GetSystemPagePathArgs = { config: I18nConfig /** * Slug that represents the site root. Defaults to 'home'. When the assigned * page's slug in the target locale equals this, the path collapses to the * locale root (/pl, /en). */ homeSlug?: string /** Target locale to build the path for. */ locale: string /** * The resolved system-page assignment from SiteSettings — the related * document object (not just an ID), read with `locale: 'all'` so its slug * is a locale→value map. Pass null/undefined if the role is unassigned. */ page: AssignedPage | null | undefined } /** * Resolves a system-page assignment to a locale-aware path. * * Bridges the Pages module (which document plays a role) and the i18n module * (how that document's localized slug becomes a URL). This is what powers * "visit /pl → serve the homepage": read SiteSettings.homepage, pass the * related document here, get /pl/strona-glowna (or /pl if it's the home slug). * * Returns undefined when the role is unassigned or the assigned page has no * slug in the target locale — the caller decides the fallback (e.g. 404, * redirect to default locale). * * @example * const settings = await payload.findGlobal({ slug: 'site-settings', locale: 'all', depth: 1 }) * getSystemPagePath({ page: settings.homepage, locale: 'pl', config }) * // → '/pl' (home slug collapses to root) * getSystemPagePath({ page: settings.privacyPolicy, locale: 'en', config }) * // → '/en/privacy-policy' */ export function getSystemPagePath({ config, homeSlug = 'home', locale, page, }: GetSystemPagePathArgs): string | undefined { if (!page?.slug) { return undefined } const slugs = getLocalizedSlugs({ config, slugField: page.slug }) return buildLocalizedPath({ config, homeSlug, locale, slugs }) }