import { z } from 'zod';

/**
 * Persisted reader preferences.
 *
 * Validated on read, not just on write: several of these values are written
 * straight onto foliate's renderer as custom-element attributes, where a bad
 * value silently wedges the layout instead of throwing. Anything that fails
 * parsing falls back to the defaults.
 */
export const readerFlowSchema = z.enum(['paginated', 'scrolled']);

export const readerSettingsSchema = z.object({
	fontFamily: z.string().min(1).max(200),
	/** px, applied to <html> so the book's own rem/em headings scale with it. */
	fontSize: z.number().int().min(12).max(32),
	fontWeight: z.number().int().min(300).max(700),
	lineHeight: z.number().min(1).max(2.5),
	/** em */
	letterSpacing: z.number().min(-0.05).max(0.2),
	/** px, applied to all four renderer margins. */
	margin: z.number().int().min(0).max(120),
	/** % of the viewport, the space between columns. */
	gap: z.number().int().min(0).max(15),
	/**
	 * px, the maximum width of a single column. foliate caps the whole reading
	 * area at this times the column count, so on a wide screen it is what decides
	 * how much of the window the book actually uses.
	 */
	maxInlineSize: z.number().int().min(400).max(2400),
	/** 1 for a single page, 2 for a spread. Reflowable books only. */
	maxColumnCount: z.number().int().min(1).max(2),
	flow: readerFlowSchema,
	justify: z.boolean(),
	hyphenate: z.boolean()
});

export type ReaderSettings = z.infer<typeof readerSettingsSchema>;
export type ReaderFlow = z.infer<typeof readerFlowSchema>;
