import { PageContentTypeEnum } from "@scandic-hotels/trpc/enums/contentType" import { RTETypeEnum } from "@scandic-hotels/trpc/types/RTEenums" import { getDestinationCityPageDescription, getDestinationCountryPageDescription, getDestinationFilterSeoMetaDescription, } from "@/utils/metadata/description/destinationPage" import { truncateTextAfterLastPeriod } from "../truncate" import { getHotelPageDescription } from "./hotelPage" import type { RawMetadataSchema } from "@scandic-hotels/trpc/routers/contentstack/metadata/output" export async function getDescription(data: RawMetadataSchema) { const metadata = data.web?.seo_metadata const isDestinationPage = [ PageContentTypeEnum.destinationCityPage, PageContentTypeEnum.destinationCountryPage, ].includes(data.system.content_type_uid as PageContentTypeEnum) if (isDestinationPage) { const destinationFilterSeoMetaDescription = getDestinationFilterSeoMetaDescription(data) if (destinationFilterSeoMetaDescription) { return destinationFilterSeoMetaDescription } } if (metadata?.description) { return metadata.description } let description: string | null = null switch (data.system.content_type_uid) { case PageContentTypeEnum.hotelPage: description = await getHotelPageDescription(data) break case PageContentTypeEnum.destinationCityPage: description = await getDestinationCityPageDescription(data) break case PageContentTypeEnum.destinationCountryPage: description = await getDestinationCountryPageDescription(data) break default: break } if (description) { return description } // Fallback descriptions from contentstack content if (data.preamble) { return truncateTextAfterLastPeriod(data.preamble) } if (data.header?.preamble) { return truncateTextAfterLastPeriod(data.header.preamble) } if (data.blocks?.length) { const jsonData = data.blocks[0].content?.content?.json // Finding the first paragraph with text const firstParagraph = jsonData?.children?.find( (child) => child.type === RTETypeEnum.p && child.children[0].text ) if (firstParagraph?.children?.length) { return firstParagraph.children[0].text ? truncateTextAfterLastPeriod(firstParagraph.children[0].text) : "" } } return "" }