import type { LinkValue, ResolveLinkHrefOptions } from './resolveLinkHref.js'
import { resolveLinkHref } from './resolveLinkHref.js'
/**
* Everything an / needs for a panel link — the project keeps the
* element and the classes (Button variants), the plugin decides the rest:
* href, new tab (files always open in one), rel, and whether it's internal
* (render next/link) or not (plain ).
*/
export type ResolvedLink = {
/** `link.appearance` from the panel (primary / secondary / link) — map it to your Button classes. */
appearance?: string
href: string
/** Internal page → next/link; external URL / file / mailto → . */
internal: boolean
label: string
newTab: boolean
/** Spread on the element: target + rel for a new tab, nothing otherwise. */
targetProps: { rel: string; target: '_blank' } | Record
}
/** A panel link → ResolvedLink, or null when it leads nowhere in this locale (no page, no file, unsafe URL). */
export function resolveLink(link: LinkValue | null | undefined, options: ResolveLinkHrefOptions): null | ResolvedLink {
const href = resolveLinkHref(link, options)
if (!link || !href) {return null}
const newTab = Boolean(link.newTab) || link.type === 'file'
return {
...(link.appearance ? { appearance: link.appearance } : {}),
href,
internal: (link.type ?? 'internal') === 'internal',
label: link.label ?? '',
newTab,
targetProps: newTab ? { rel: 'noopener noreferrer', target: '_blank' } : {},
}
}
export type NavItem = {
/** Nested items (dropdown), already resolved. */
children: NavItem[]
} & ResolvedLink
export type ResolveNavOptions = {
/** Field with the nested items inside an item. Default 'subItems'. */
childrenField?: string
/** Field with the link inside an item. Default 'link'. */
linkField?: string
} & ResolveLinkHrefOptions
/**
* Menu items from a Header/Footer global → plain, serializable items (safe to
* pass to a client MobileNav). Items whose link leads nowhere in this locale
* — a deleted or untranslated page — are dropped instead of rendering "#".
*
* const items = resolveNav(header.navItems, { locale, config: i18nConfig, homeSlug })
* // [{ href, label, newTab, internal, targetProps, children: [...] }]
*
* Any depth; the item shape is configurable (linkField, childrenField).
*/
export function resolveNav(items: unknown, options: ResolveNavOptions): NavItem[] {
const { childrenField = 'subItems', linkField = 'link', ...linkOptions } = options
if (!Array.isArray(items)) {return []}
return items.flatMap((item) => {
if (!item || typeof item !== 'object') {return []}
const record = item as Record
const link = resolveLink(record[linkField] as LinkValue, linkOptions)
if (!link || !link.label) {return []}
return [{ ...link, children: resolveNav(record[childrenField], options) }]
})
}
/** Homepage slug from SiteSettings (`homepage` relationship, populated) — for links that collapse to /{locale}. */
export function homeSlugFrom(settings: unknown, field = 'homepage'): string | undefined {
const home = settings && typeof settings === 'object' ? (settings as Record)[field] : undefined
const slug = home && typeof home === 'object' ? (home as { slug?: unknown }).slug : undefined
return typeof slug === 'string' && slug ? slug : undefined
}