import { z } from "zod";

type Assert<_T extends true> = void;

export const all_datum_tags = [
  "Text",
  "Timestamp",
  "Uuid",
] as const;

// Type checking to ensure that all valid enum tags are included.
type _ = Assert<
  Datum["t"] extends (typeof all_datum_tags)[number] ? true : false
>;

const datum_text_schema = z.object({
  t: z.literal("Text"),
  c: z.string().nullish().transform((x) => x ?? undefined),
});

const datum_timestamp_schema = z.object({
  t: z.literal("Timestamp"),
  c: z.coerce.date().nullish().transform((x) => x ?? undefined),
});

const datum_uuid_schema = z.object({
  t: z.literal("Uuid"),
  c: z.string().nullish().transform((x) => x ?? undefined),
});

export const datum_schema = z.union([
  datum_text_schema,
  datum_timestamp_schema,
  datum_uuid_schema,
]);

export type Datum = z.infer<typeof datum_schema>;
