import { z } from "zod";

export function zodGenerateDefaultSchema<Schema extends z.ZodFirstPartySchemaTypes>(schema: Schema): z.TypeOf<Schema> {
		switch (schema._def.typeName) {
			case z.ZodFirstPartyTypeKind.ZodDefault:
				return schema._def.defaultValue();
			case z.ZodFirstPartyTypeKind.ZodObject: {
				// The switch wasn't able to infer this but the cast should
				// be safe.
				return Object.fromEntries(
					Object.entries(
						(schema as z.SomeZodObject).shape
					).map(([key, value]) => [key, zodGenerateDefaultSchema(value)])
				);
			}
			case z.ZodFirstPartyTypeKind.ZodString:
				return "";
			case z.ZodFirstPartyTypeKind.ZodNull:
				return null;
			case z.ZodFirstPartyTypeKind.ZodNullable:
				return null;
			case z.ZodFirstPartyTypeKind.ZodUndefined:
				return undefined;
			case z.ZodFirstPartyTypeKind.ZodNumber:
				return 0;
			case z.ZodFirstPartyTypeKind.ZodBoolean:
				return false;
			case z.ZodFirstPartyTypeKind.ZodOptional:
				return undefined;
			case z.ZodFirstPartyTypeKind.ZodNativeEnum:
				return schema._def.values[0];
			case z.ZodFirstPartyTypeKind.ZodArray:
				return [];
			default:
				throw new Error(`Unsupported type ${schema._def.typeName}`);
		}
}