import { z } from "zod/mini" import { EmptyDocumentLinkLegacyValueSchema, EmptyImageLinkLegacyValueSchema, FilledDocumentLinkLegacyValueSchema, FilledFileLinkLegacyValueSchema, FilledImageLinkLegacyValueSchema, EmptyMediaLinkLegacyValueSchema, FilledExternalLinkLegacyValueSchema, EmptyExternalLinkLegacyValueSchema, AnyLinkLegacySchema, LinkType, EmptyFileLinkLegacyValueSchema, } from "./legacy/link" // Image const FilledImageLinkContentValueSchema = z.extend(FilledImageLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.Image), }) const EmptyImageLinkContentValueSchema = z.extend(EmptyImageLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.Image), }) // File const FilledFileLinkContentValueSchema = z.extend(FilledFileLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.File), }) const EmptyFileLinkContentValueSchema = z.extend(EmptyFileLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.File), }) // Media const EmptyMediaLinkContentValueSchema = z.extend(EmptyMediaLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.Media), }) // Document const FilledDocumentLinkContentValueSchema = z.extend(FilledDocumentLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.Document), }) const EmptyDocumentLinkContentValueSchema = z.extend(EmptyDocumentLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.Document), }) // External const FilledExternalLinkContentValueSchema = z.extend(FilledExternalLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.External), }) const EmptyExternalLinkContentValueSchema = z.extend(EmptyExternalLinkLegacyValueSchema, { __TYPE__: z.literal(LinkType.External), }) // Any const AnyLinkContentValueSchema = z.extend(AnyLinkLegacySchema, { __TYPE__: z.literal(LinkType.Any), }) // All // Used by content/richText export const FilledLinkContentValueSchema: z.ZodMiniType = z.union([ FilledImageLinkContentValueSchema, FilledFileLinkContentValueSchema, FilledDocumentLinkContentValueSchema, FilledExternalLinkContentValueSchema, ]) // Used by content/legacy/link type FilledImageLinkContentValue = z.infer type FilledFileLinkContentValue = z.infer type FilledDocumentLinkContentValue = z.infer type FilledExternalLinkContentValue = z.infer export type FilledLinkContentValue = | FilledImageLinkContentValue | FilledFileLinkContentValue | FilledDocumentLinkContentValue | FilledExternalLinkContentValue const EmptyLinkContentValueSchema: z.ZodMiniType = z.union([ EmptyImageLinkContentValueSchema, EmptyFileLinkContentValueSchema, EmptyMediaLinkContentValueSchema, EmptyDocumentLinkContentValueSchema, EmptyExternalLinkContentValueSchema, AnyLinkContentValueSchema, ]) // Used by content/codec/link type EmptyImageLinkContentValue = z.infer type EmptyFileLinkContentValue = z.infer type EmptyMediaLinkContentValue = z.infer type EmptyDocumentLinkContentValue = z.infer type EmptyExternalLinkContentValue = z.infer type AnyLinkContentValue = z.infer export type EmptyLinkContentValue = | EmptyImageLinkContentValue | EmptyFileLinkContentValue | EmptyMediaLinkContentValue | EmptyDocumentLinkContentValue | EmptyExternalLinkContentValue | AnyLinkContentValue const LinkContentValueSchema: z.ZodMiniType = z.union([ FilledLinkContentValueSchema, EmptyLinkContentValueSchema, ]) // Used by content/codec/link export type LinkContentValue = FilledLinkContentValue | EmptyLinkContentValue // Content export const LinkContentType = "LinkContent" as const const getLinkContentSchema = (value: z.ZodMiniType) => z.object({ __TYPE__: z.literal(LinkContentType), key: z.transform((value) => z.uuidv4().safeParse(value).data || crypto.randomUUID()), // We cannot use z.extend here because we're working with unions. // z.intersection wouldn't work also for the same reason. value: z.pipe( z.pipe( z.pipe( z.record(z.string(), z.unknown()), z.transform( ({ text, variant, ...value }): { text?: unknown variant?: unknown value: Record } => ({ text, variant, value }), ), ), z.object({ text: z.optional(z.string()), variant: z.optional(z.string()), value: value as z.ZodMiniType>, }), ), z.transform(({ text, variant, value }) => ({ ...(text ? { text } : {}), ...(variant ? { variant } : {}), ...value, })), ), }) export const FilledLinkContentSchema = getLinkContentSchema(FilledLinkContentValueSchema) export type FilledLinkContent = z.infer export const EmptyLinkContentSchema = getLinkContentSchema(EmptyLinkContentValueSchema) export type EmptyLinkContent = z.infer export const LinkContentSchema = getLinkContentSchema(LinkContentValueSchema) export type LinkContent = z.infer