import * as prismicH from 'prismic-helpers-beta' ; import { PrismicDocument , KeyTextField , RichTextField } from '@prismicio/types' ; import { Label } from '@weco/common/model/labels' ; import { WithSeries } from '../types/articles' ; import linkResolver from '../link-resolver' ; import { CommonPrismicFields , Image , isFilledLinkToDocumentWithData , WithArticleFormat , } from '../types' ; type Meta = { title : string ; type : 'website' | 'article' | 'book' | 'profile' | 'video' | 'music' ; url : string ; description ? : string ; promoText ? : string ; image ? : Image ; } ; type Doc = PrismicDocument < CommonPrismicFields > ; export function transformMeta ( doc ) { const promo = transformPromo ( doc ) ; return { title : transformRichTextFieldToString ( doc . data . title ) ?? '' , type : 'website' , description : doc . data . metadataDescription || undefined , promoText : transformRichTextFieldToString ( promo ?. caption ?? [ ] ) || undefined , image : promo ?. image , url : linkResolver ( doc ) || '' , } ; } export function transformPromo ( doc ) { /**
     * this is a little bit annoying as we modelled this at a stage where Prismic was suggesting
     * "use slices for all the things!". Unfortunately it definitely wasn't made for this, and
     * we should have robably just had `.image` and `.description`.
     * We could reimport into these fields, but it would have to be the whole Prismic corpus,
     * and we aren't confident enough that it imports correctly.
     *
     * This method flattens out the `SliceZone` into just a Promo
     */ return doc . data ?. promo ?. [ 0 ] ?. primary ?? undefined ; } export function transformLabels ( doc ) { const typeLabels = { seasons : [ { text : 'Season' } ] , } ; const labels = typeLabels [ doc . type ] ; return labels ?? [ ] ; } export function transformSeries ( document ) { return document . data . series . map ( ( { series } ) => series ) . filter ( isFilledLinkToDocumentWithData ) ; } export function transformFormat ( document ) { const { format } = document . data ; if ( isFilledLinkToDocumentWithData ( format ) && format . data ) { return format ; } } export function transformKeyTextField ( field ) { return field ?? undefined ; } export function transformRichTextField ( field ) { return field && field . length > 0 ? field : undefined ; } export function transformRichTextFieldToString ( field ) { return field && field . length > 0 ? prismicH . asText ( field ) : undefined ; }