import { z } from "zod"

import * as pageLinks from "@/server/routers/contentstack/schemas/pageLinks"

export const linkConnectionRefsSchema = z
  .object({
    linkConnection: z.object({
      edges: z.array(
        z.object({
          node: z.discriminatedUnion("__typename", [
            pageLinks.accountPageRefSchema,
            pageLinks.contentPageRefSchema,
            pageLinks.loyaltyPageRefSchema,
          ]),
        })
      ),
    }),
  })
  .transform((data) => {
    if (!data.linkConnection.edges.length) {
      return null
    }

    return data.linkConnection.edges[0].node.system
  })
