/** * RFC-0045 step 8b: the referenced entries a structured-data preset reads (`ReferenceValueSource`), * for `entryStructuredData`. For each reference field a source names, the member the view shows: * the source's `groupMembers` (gated by the view), then `pickMember` in the entry's language (the * default language's under `default-with-notice`), then `resolveFields` with the member's * same-site original, so a translation member's non-localized `street` comes from its original. * One `groupMembers` call for every field; a field without a member reads as nothing. */ import type { ContentEntry, Site } from "@cloudhal/core-contracts"; import { getGroupMembers } from "@cloudhal/core-ts"; import { pickMember, resolveFields } from "@cloudhal/core-ts/fields"; import { itemUrl } from "@cloudhal/core-ts/locale-paths"; import type { ReferencedValues } from "@cloudhal/core-ts/structured-data"; import type { SqlDriver } from "@cloudhal/storage"; import type { GraphSource } from "./source.ts"; type MembersSource = Partial>; /** The reference fields the entry's preset reads, with each one's group id and target. */ function namedReferences( site: Site, entry: ContentEntry, resolved: ReturnType, ) { const preset = site.collections?.[entry.collection]?.structuredData; const out = new Map(); for (const source of Object.values(preset?.properties ?? {})) { if (source?.kind !== "reference" || out.has(source.field)) continue; const field = resolved[source.field]; if (field?.def.type !== "reference" || typeof field.value !== "string") continue; out.set(source.field, { groupId: field.value, collections: field.def.collections }); } return out; } /** A member's same-site original among the members the view shows (RFC-0041 ยง5). */ function originalAmong(members: readonly ContentEntry[], member: ContentEntry) { const g = member.translation?.group; if (!g || g === member.id || member.translation?.source?.siteId !== undefined) return undefined; return members.find((m) => m.id === g); } /** The `references` of `StructuredDataContext`: one entry per field with a member the view shows. */ export async function referencedValues(input: { readonly source: MembersSource; readonly site: Site; readonly entry: ContentEntry; readonly resolved: ReturnType; readonly origin: string; }): Promise | undefined> { const { source, site, entry, resolved, origin } = input; const named = namedReferences(site, entry, resolved); if (named.size === 0 || !source.groupMembers) return undefined; const groups = [...new Set([...named.values()].map((n) => n.groupId))]; const members = await source.groupMembers(groups); const out = new Map(); for (const [field, { groupId, collections }] of named) { const all = members.get(groupId) ?? []; const picked = pickMember(groupId, all, { locale: entry.locale, defaultLocale: site.defaultLocale, fallback: site.localization?.fallback, collections, }); if (!picked) continue; const m = picked.member; out.set(field, { member: m, fields: resolveFields(site, m, originalAmong(all, m)), url: itemUrl(site, origin, m), }); } return out; } /** * The previews' `groupMembers` (MCP `preview_structured_data`, the API's structured-data route): * the working copy's published members, what the site would show once the entry is published. * Time gates are not applied: a preview shows the mapping, not a moment. */ export function previewGroupMembers( sql: SqlDriver, siteId: string, ): (groupIds: readonly string[]) => Promise> { return async (groupIds) => { const read = await getGroupMembers(sql, siteId, groupIds); return new Map( [...read].map(([g, members]) => [g, members.filter((m) => m.status === "published")]), ); }; }