import { z } from "zod/mini" import { GroupContentType } from "../group" import type { WidgetLegacy } from "../legacy/widget" import { SlicesContentType } from "../slices" import { UIDContentType } from "../uid" import { WidgetContentSchema } from "../widget" import type { WidgetContent } from "../widget" import { GroupLegacyCodec } from "./group" import type { LegacyCodec, LegacyContentCtx } from "./legacyContentCtx" import { NestableLegacyCodec } from "./nestable" import { SlicesLegacyCodec } from "./slices" import { UIDLegacyCodec } from "./uid" const UnsupportedWidgetLegacySchema = z.never() export const WidgetLegacyCodec = ( ctx: LegacyContentCtx, ): LegacyCodec => ({ name: "WidgetLegacy", is(input): input is WidgetContent { return WidgetContentSchema.safeParse(input).success }, toContent(input) { if (!ctx.fieldType) { return UnsupportedWidgetLegacySchema.safeParse( input, ) as z.core.util.SafeParseError } switch (ctx.fieldType) { case "UID": return UIDLegacyCodec(ctx).toContent(input) case "Group": return GroupLegacyCodec(ctx).toContent(input) case "Choice": case "Slices": return SlicesLegacyCodec(ctx).toContent(input) default: return NestableLegacyCodec(ctx).toContent(input) } }, fromContent(input) { switch (input.__TYPE__) { case UIDContentType: return UIDLegacyCodec(ctx).fromContent(input) case GroupContentType: return GroupLegacyCodec(ctx).fromContent(input) case SlicesContentType: return SlicesLegacyCodec(ctx).fromContent(input) default: return NestableLegacyCodec(ctx).fromContent(input) } }, })