import { z } from "zod";
import {
	AmqSongLinkType,
	AmqSongLinkTypeMap,
	AmqSongLinkTypeMapReverse,
} from "$lib/types/amq";

const SEP = ",";

const songTypesCodec = z.codec(z.string(), z.array(AmqSongLinkType), {
	decode: (str) =>
		str
			? decodeURIComponent(str)
					.split(SEP)
					.map((s) => AmqSongLinkTypeMap[s as keyof typeof AmqSongLinkTypeMap])
			: [],
	encode: (arr) =>
		arr
			? encodeURIComponent(
					arr.map((a) => AmqSongLinkTypeMapReverse[a]).join(SEP),
				)
			: "",
});

export const SearchParamsSchema = z.object({
	limit: z.coerce.number().int().default(20),
	song: z.string().optional().default(""),
	artist: z.string().optional().default(""),
	anime: z.string().optional().default(""),
	gpm: z.coerce.number().int().optional().default(0),
	gpx: z.coerce.number().int().optional().default(100),
	type: songTypesCodec.default([]),
});
