import {z} from "zod"
import {
	ChromePdfRenderOptions,
	HttpLoginCredentials,
	WaitFor,
	WaitForHtmlQuerySelector,
	WaitForJavaScript,
	WaitForNetworkIdle0,
	WaitForNetworkIdle2,
	WaitForNetworkIdleN,
	WaitForPageLoad, WaitForRenderDelay,
	WaitForType
} from "../../public/render";
import {cssMediaTypeSchema, fitToPaperModesSchema, marginConfigSchema, useMarginsSchema} from "./typeSchema";
import {pdfPaperOrientationSchema, pdfPaperSizeSchema} from "./paperSchema";
import {htmlAffixSchema, textAffixSchema} from "./affixSchema";

export const waitForTypeSchema : z.ZodType<WaitForType> = z.nativeEnum(WaitForType)

export const waitForRenderDelaySchema: z.ZodType<WaitForRenderDelay> = z.object({
	type: z.literal(WaitForType.RenderDelay),
	delay: z
		.number()
		.optional()
})

export const waitForJavaScriptSchema: z.ZodType<WaitForJavaScript> = z.object({
	type: z.literal(WaitForType.JavaScript),
	maxWaitTime: z
		.number()
		.optional()
})

export const waitForNetworkIdleNSchema : z.ZodType<WaitForNetworkIdleN> = z.object({
	type: z.literal(WaitForType.NetworkIdleN),
	networkIdleDuration: z.number().optional(),
	maxNumAllowedInflight: z.number().optional(),
	maxWaitTime: z.number().optional()
})

export const waitForNetworkIdle0Schema : z.ZodType<WaitForNetworkIdle0> = z.object({
	type: z.literal(WaitForType.NetworkIdle0),
	maxWaitTime: z
		.number()
		.optional()
})

export const waitForNetworkIdle2Schema : z.ZodType<WaitForNetworkIdle2> = z.object({
	type: z.literal(WaitForType.NetworkIdle2),
	maxWaitTime: z
		.number()
		.optional()
})

export const waitForPageLoadSchema : z.ZodType<WaitForPageLoad> = z.object({
	type: z.literal(WaitForType.PageLoad)
})

export const waitForHtmlQuerySelectorSchema : z.ZodType<WaitForHtmlQuerySelector> = z.object({
	type: z.literal(WaitForType.HtmlElement),
	htmlQueryStr: z.string(),
	maxWaitTime: z
		.number()
		.optional()
})

export const waitForSchema : z.ZodType<WaitFor> = z.union([
	waitForRenderDelaySchema,
	waitForPageLoadSchema,
	waitForJavaScriptSchema,
	waitForNetworkIdleNSchema,
	waitForNetworkIdle0Schema,
	waitForNetworkIdle2Schema,
	waitForHtmlQuerySelectorSchema
])

export const httpLoginCredentialsSchema : z.ZodType<HttpLoginCredentials> = z.object({
	custom_cookies: z.map(z.string(),z.string()).optional(),
	enable_cookies: z.boolean().optional(),
	network_password: z.string().optional(),
	network_username: z.string().optional()
})

export const chromePdfRenderOptionsSchema : z.ZodType<ChromePdfRenderOptions> = z.object({
	createPdfFormsFromHtml: z.boolean().optional(),
	customCssUrl: z.string().optional(),
	enableJavaScript: z.boolean().optional(),
	fitToPaperMode: fitToPaperModesSchema.optional(),
	grayScale: z.boolean().optional(),
	margin: marginConfigSchema.optional(),
	paperOrientation: pdfPaperOrientationSchema.optional(),
	paperSize: pdfPaperSizeSchema.optional(),
	printHtmlBackgrounds: z.boolean().optional(),
	timeout: z.number().positive().optional(),
	waitFor: waitForSchema.optional(),
	title: z.string().optional(),
	inputEncoding: z.string().optional(),
	cssMediaType: cssMediaTypeSchema.optional(),
	javascript: z.string().optional(),
	firstPageNumber: z.number().optional(),
	htmlHeader:htmlAffixSchema.optional(),
	htmlFooter:htmlAffixSchema.optional(),
	textHeader:textAffixSchema.optional(),
	textFooter:textAffixSchema.optional(),
	useMarginsOnHeaderAndFooter: useMarginsSchema.optional()
})

