import { z } from "zod"

import {
  teaserCardBlockRefsSchema,
  teaserCardBlockSchema,
  transformTeaserCardBlock,
} from "../blocks/cards/teaserCard"
import { transformCardBlockRefs } from "../blocks/cardsGrid"

import { SidebarEnums } from "@/types/enums/sidebar"

export const teaserCardsSchema = z.object({
  typename: z
    .literal(SidebarEnums.blocks.TeaserCard)
    .optional()
    .default(SidebarEnums.blocks.TeaserCard),
  teaser_card: z
    .object({
      theme: z.enum(["featured", "default"]).nullable().default("default"),
      teaser_cardConnection: z.object({
        edges: z.array(
          z.object({
            node: teaserCardBlockSchema,
          })
        ),
      }),
    })
    .transform((data) => {
      return {
        ...transformTeaserCardBlock(data.teaser_cardConnection.edges[0].node),
        theme: data.theme,
      }
    }),
})

export const teaserCardRefschema = z.object({
  teaser_card: z
    .object({
      teaser_cardConnection: z.object({
        edges: z.array(
          z.object({
            node: teaserCardBlockRefsSchema,
          })
        ),
      }),
    })
    .transform((data) => {
      let card = null
      if (data.teaser_cardConnection.edges.length) {
        card = transformCardBlockRefs(data.teaser_cardConnection.edges[0].node)
      }
      return card
    }),
})
