/* Server-side mapping from the editable Strapi phase component to page copy. */ import type { CmsLegacyStage } from "./cms-types"; import { list, toImage } from "./cms.server"; import type { LegacyGalleryImage, LegacyStageCopy } from "./copy"; const textOr = (value: string | null | undefined, fallback: string): string => value?.trim() ? value : fallback; export function resolveLegacyStage( source: CmsLegacyStage | null | undefined, fallback: LegacyStageCopy, ): LegacyStageCopy { const metrics = list(source?.metrics) .filter((metric) => metric.value.trim() !== "" && metric.label.trim() !== "") .map(({ value, label }) => ({ value, label })); const focusAreas = list(source?.focusAreas) .map((area) => area.name.trim()) .filter(Boolean); const images: LegacyGalleryImage[] = list(source?.images) .slice(0, 5) .flatMap((media) => { const image = toImage(media, "large"); return image ? [{ src: image.src, alt: image.alt, ...(media.caption ? { caption: media.caption } : {}), }] : []; }); return { ...fallback, name: textOr(source?.name, fallback.name), years: textOr(source?.years, fallback.years), summary: textOr(source?.summary, fallback.summary), description: textOr(source?.description, fallback.description), metrics: metrics.length > 0 ? metrics : fallback.metrics, focusHeading: textOr(source?.focusHeading, fallback.focusHeading), focusAreas: focusAreas.length > 0 ? focusAreas : fallback.focusAreas, imageHeading: textOr(source?.galleryHeading, fallback.imageHeading), images, }; }