import { z } from "zod" import { discriminatedUnionArray } from "@/lib/discriminatedUnion" import { accordionRefsSchema, accordionSchema, } from "../schemas/blocks/accordion" import { contentRefsSchema, contentSchema } from "../schemas/blocks/content" import { tempImageVaultAssetSchema } from "../schemas/imageVault" import { linkRefsUnionSchema, linkUnionSchema, transformPageLink, } from "../schemas/pageLinks" import { systemSchema } from "../schemas/system" import type { ImageVaultAsset } from "@/types/components/imageVault" import { TrackingChannelEnum, type TrackingSDKPageData, } from "@/types/components/tracking" import { Country } from "@/types/enums/country" import { DestinationCountryPageEnum } from "@/types/enums/destinationCountryPage" export const destinationCountryPageContent = z .object({ __typename: z.literal( DestinationCountryPageEnum.ContentStack.blocks.Content ), }) .merge(contentSchema) export const destinationCountryPageAccordion = z .object({ __typename: z.literal( DestinationCountryPageEnum.ContentStack.blocks.Accordion ), }) .merge(accordionSchema) export const blocksSchema = z.discriminatedUnion("__typename", [ destinationCountryPageAccordion, destinationCountryPageContent, ]) export const destinationCountryPageSchema = z .object({ destination_country_page: z.object({ title: z.string(), destination_settings: z.object({ country: z.nativeEnum(Country), }), heading: z.string(), preamble: z.string(), experiences: z .object({ destination_experiences: z.array(z.string()), }) .transform(({ destination_experiences }) => destination_experiences), images: z .array(z.object({ image: tempImageVaultAssetSchema })) .transform((images) => images .map((image) => image.image) .filter((image): image is ImageVaultAsset => !!image) ), has_sidepeek: z.boolean().default(false), sidepeek_button_text: z.string().default(""), sidepeek_content: z.object({ heading: z.string(), content: z.object({ json: z.any(), embedded_itemsConnection: z.object({ edges: z.array( z.object({ node: linkUnionSchema.transform((data) => { const link = transformPageLink(data) if (link) { return link } return data }), }) ), }), }), }), blocks: discriminatedUnionArray(blocksSchema.options).nullable(), system: systemSchema.merge( z.object({ created_at: z.string(), updated_at: z.string(), }) ), }), trackingProps: z.object({ url: z.string(), }), }) .transform((data) => { const countryPageData = data.destination_country_page const system = countryPageData.system const trackingUrl = data.trackingProps.url const tracking: TrackingSDKPageData = { pageId: system.uid, domainLanguage: system.locale, publishDate: system.updated_at, createDate: system.created_at, channel: TrackingChannelEnum["destination-page"], pageType: "staticcontentpage", pageName: trackingUrl, siteSections: trackingUrl, siteVersion: "new-web", } return { destinationCountryPage: countryPageData, tracking, } }) /** REFS */ const destinationCountryPageContentRefs = z .object({ __typename: z.literal( DestinationCountryPageEnum.ContentStack.blocks.Content ), }) .merge(contentRefsSchema) const destinationCountryPageAccordionRefs = z .object({ __typename: z.literal( DestinationCountryPageEnum.ContentStack.blocks.Accordion ), }) .merge(accordionRefsSchema) const blocksRefsSchema = z.discriminatedUnion("__typename", [ destinationCountryPageAccordionRefs, destinationCountryPageContentRefs, ]) export const destinationCountryPageRefsSchema = z.object({ destination_country_page: z.object({ sidepeek_content: z.object({ content: z.object({ embedded_itemsConnection: z.object({ edges: z.array( z.object({ node: linkRefsUnionSchema, }) ), }), }), }), blocks: discriminatedUnionArray(blocksRefsSchema.options).nullable(), system: systemSchema, }), })