import { extname } from 'node:path';
import { z } from 'astro/zod';

const faviconTypeMap = {
	'.ico': 'image/x-icon',
	'.gif': 'image/gif',
	'.jpeg': 'image/jpeg',
	'.jpg': 'image/jpeg',
	'.png': 'image/png',
	'.svg': 'image/svg+xml',
};

export const FaviconSchema = () =>
	z
		.string()
		.default('/favicon.svg')
		.transform((favicon, ctx) => {
			// favicon can be absolute or relative url
			const { pathname } = new URL(favicon, 'https://example.com');
			const ext = extname(pathname).toLowerCase();

			if (!isFaviconExt(ext)) {
				ctx.issues.push({
					code: 'custom',
					message: 'favicon must be a .ico, .gif, .jpg, .png, or .svg file',
					input: favicon,
				});

				return z.NEVER;
			}

			return {
				href: favicon,
				type: faviconTypeMap[ext],
			};
		});

function isFaviconExt(ext: string): ext is keyof typeof faviconTypeMap {
	return ext in faviconTypeMap;
}
