import { z } from "zod" import { Lang } from "@/constants/languages" import { JoinLoyaltyContactTypenameEnum, LoyaltyBlocksTypenameEnum, LoyaltyComponentEnum, SidebarTypenameEnum, } from "@/types/components/loyalty/enums" import { Embeds } from "@/types/requests/embeds" import { EdgesWithTotalCount } from "@/types/requests/utils/edges" import { RTEDocument } from "@/types/rte/node" const loyaltyPageBlockCardGrid = z.object({ __typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid), card_grid: z.object({ title: z.string().optional(), subtitle: z.string().optional(), cards: z.array( z.object({ title: z.string().optional(), subtitle: z.string().optional(), referenceConnection: z.object({ edges: z.array( z.object({ node: z.object({ system: z.object({ uid: z.string(), }), url: z.string(), title: z.string(), __typename: z.string(), }), }) ), totalCount: z.number(), }), open_in_new_tab: z.boolean(), cta_text: z.string().optional(), }) ), }), }) const loyaltyPageDynamicContent = z.object({ __typename: z.literal( LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent ), dynamic_content: z.object({ title: z.string().optional(), subtitle: z.string().optional(), component: z.nativeEnum(LoyaltyComponentEnum), link: z.object({ text: z.string().optional(), pageConnection: z.object({ edges: z.array( z.object({ node: z.object({ system: z.object({ uid: z.string(), locale: z.nativeEnum(Lang), }), url: z.string(), title: z.string(), }), }) ), totalCount: z.number(), }), }), }), }) const loyaltyPageShortcuts = z.object({ __typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts), shortcuts: z.object({ title: z.string().optional(), preamble: z.string().optional(), shortcuts: z.array( z.object({ linkConnection: z.object({ edges: z.array( z.object({ node: z.object({ system: z.object({ uid: z.string(), locale: z.nativeEnum(Lang), }), url: z.string(), web: z .object({ original_url: z.string().optional(), }) .optional(), title: z.string(), }), }) ), totalCount: z.number(), }), text: z.string().optional(), open_in_new_tab: z.boolean(), }) ), }), }) const loyaltyPageBlockTextContent = z.object({ __typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent), content: z.object({ content: z.object({ embedded_itemsConnection: z.object({ edges: z.array(z.any()), totalCount: z.number(), }), json: z.any(), }), }), }) const loyaltyPageBlockItem = z.discriminatedUnion("__typename", [ loyaltyPageBlockCardGrid, loyaltyPageDynamicContent, loyaltyPageBlockTextContent, loyaltyPageShortcuts, ]) const loyaltyPageSidebarTextContent = z.object({ __typename: z.literal(SidebarTypenameEnum.LoyaltyPageSidebarContent), content: z.object({ content: z.object({ embedded_itemsConnection: z.object({ edges: z.array(z.any()), totalCount: z.number(), }), json: z.any(), }), }), }) const loyaltyPageJoinLoyaltyContact = z.object({ __typename: z.literal( SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact ), join_loyalty_contact: z.object({ title: z.string().optional(), preamble: z.string().optional(), contact: z.array( z.object({ __typename: z.literal( JoinLoyaltyContactTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact ), contact: z.object({ display_text: z.string().optional(), contact_field: z.string(), }), }) ), }), }) const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [ loyaltyPageSidebarTextContent, loyaltyPageJoinLoyaltyContact, ]) export const validateLoyaltyPageSchema = z.object({ all_loyalty_page: z.object({ items: z.array( z.object({ title: z.string(), blocks: z.array(loyaltyPageBlockItem).nullable(), sidebar: z.array(loyaltyPageSidebarItem).nullable(), }) ), }), }) // Block types type CardGridRaw = z.infer export type CardGridCard = Omit< CardGridRaw["card_grid"]["cards"][number], "referenceConnection" > & { link: | { href: string title: string } | undefined } export type CardGrid = Omit & { card_grid: Omit & { cards: CardGridCard[] } } type DynamicContentRaw = z.infer export type DynamicContent = Omit & { dynamic_content: Omit & { link: | { href: string title: string text?: string } | undefined } } type BlockContentRaw = z.infer export interface RteBlockContent extends BlockContentRaw { content: { content: { json: RTEDocument embedded_itemsConnection: EdgesWithTotalCount } } } type ShortcutsRaw = z.infer export type Shortcuts = Omit & { shortcuts: Omit & { shortcuts: { text?: string openInNewTab: boolean url: string title: string }[] } } export type Block = CardGrid | RteBlockContent | DynamicContent | Shortcuts // Sidebar block types type SidebarContentRaw = z.infer export type RteSidebarContent = Omit & { content: { content: { json: RTEDocument embedded_itemsConnection: EdgesWithTotalCount } } } export type JoinLoyaltyContact = z.infer export type Sidebar = JoinLoyaltyContact | RteSidebarContent export type LoyaltyPageDataRaw = z.infer type LoyaltyPageRaw = LoyaltyPageDataRaw["all_loyalty_page"]["items"][0] export type LoyaltyPage = Omit & { blocks: Block[] sidebar: Sidebar[] }