import { z } from "zod"; import { get_empty_datum_for, Presentation } from "./presentation.svelte.ts"; 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; export function parse_clipboard_value( presentation: Presentation, input: string, ): Datum { if (presentation.t === "Dropdown") { if ( presentation.c.allow_custom || presentation.c.options.some(({ value }) => value === input) ) { return { t: "Text", c: input }; } else { return get_empty_datum_for(presentation); } } else if (presentation.t === "Text") { return { t: "Text", c: input }; } else if (presentation.t === "Timestamp") { // TODO: implement console.warn("parsing timestamps from clipboard is not yet supported"); return get_empty_datum_for(presentation); } else if (presentation.t === "Uuid") { // TODO: implement console.warn("parsing uuids from clipboard is not yet supported"); return get_empty_datum_for(presentation); } type _ = Assert; throw new Error("should be unreachable"); }