import type { Field, RelationshipField } from 'payload' import type { AdminText } from '../admin-i18n/index.js' import type { ContentOption } from './types.js' import { archiveFieldName } from './types.js' /** * Builds one relationship field per content collection, pointing at the * client's Pages collection. * * Sits alongside the system-page roles (homepage, privacy policy) because it's * the same idea: a page referenced by function rather than by slug. The * assignment is locale-agnostic — one page document — while the resulting path * is locale-aware, since the page's slug is localized. Assign the "Artykuły" * page once and you get /pl/artykuly and /en/articles from its own slugs. */ export function buildArchiveFields(content: ContentOption, pagesSlug: string): Field[] { return content.collections.map((collection): RelationshipField => { const name = collectionName(collection.label, collection.slug) return { name: archiveFieldName(collection.slug), type: 'relationship', admin: { description: { en: 'Page that lists the entries; its address (URL) starts the address of each entry.', pl: 'Strona z listą wpisów; jej adres (URL) rozpoczyna adres każdego wpisu.', }, }, label: { en: `${name.en} archive`, pl: `Archiwum: ${name.pl}` }, maxDepth: 1, relationTo: pagesSlug, } }) } /** * The collection's name in each admin language: the project's `label` (a * string, or `{ en, pl }`), else the slug made readable (`blog-posts` → "Blog posts"). */ function collectionName(label: AdminText | string | undefined, slug: string): AdminText { if (label && typeof label === 'object') {return { en: label.en, pl: label.pl || label.en }} if (typeof label === 'string' && label.trim()) {return { en: label, pl: label }} const words = slug.replace(/[-_]+/g, ' ').replace(/([a-z])([A-Z])/g, '$1 $2').toLowerCase().trim() const readable = words.charAt(0).toUpperCase() + words.slice(1) return { en: readable, pl: readable } }