//#region src/validation/render-validation.ts
const MAX_CODE_SIZE = 5e4;
const MAX_DATA_SIZE = 1e4;
const DANGEROUS_PATTERNS = [
	{
		pattern: /\beval\s*\(/i,
		message: "eval() is not allowed"
	},
	{
		pattern: /\bFunction\s*\(/i,
		message: "Function constructor is not allowed"
	},
	{
		pattern: /dangerouslySetInnerHTML/i,
		message: "dangerouslySetInnerHTML is not allowed"
	},
	{
		pattern: /<script\b/i,
		message: "Script tags are not allowed"
	},
	{
		pattern: /\bon\w+\s*=/i,
		message: "Inline event handlers (onClick=, onLoad=, etc.) are not allowed"
	},
	{
		pattern: /document\.write/i,
		message: "document.write is not allowed"
	},
	{
		pattern: /window\.location/i,
		message: "window.location is not allowed"
	},
	{
		pattern: /\.innerHTML\s*=/i,
		message: "innerHTML manipulation is not allowed"
	}
];
const ALLOWED_IMPORTS = ["lucide-react", "react"];
/**
* Validates component render code and data
*/
function validateRender(render) {
	const errors = [];
	if (!render.component || typeof render.component !== "string") return {
		isValid: false,
		errors: [{
			field: "render.component",
			message: "Component must be a non-empty string"
		}]
	};
	if (!render.mockData || typeof render.mockData !== "object") return {
		isValid: false,
		errors: [{
			field: "render.mockData",
			message: "MockData must be an object"
		}]
	};
	try {
		const serialized = JSON.stringify(render);
		const parsed = JSON.parse(serialized);
		if (JSON.stringify(parsed) !== serialized) return {
			isValid: false,
			errors: [{
				field: "render",
				message: "Render data contains values that cannot be safely serialized to JSON"
			}]
		};
	} catch (e) {
		return {
			isValid: false,
			errors: [{
				field: "render",
				message: `Render data contains invalid characters that cannot be stored in the database: ${e instanceof Error ? e.message : "Unknown JSON serialization error"}`
			}]
		};
	}
	if (render.component.length > MAX_CODE_SIZE) errors.push({
		field: "render.component",
		message: `Component size exceeds maximum allowed (${MAX_CODE_SIZE} characters)`
	});
	if (JSON.stringify(render.mockData).length > MAX_DATA_SIZE) errors.push({
		field: "render.mockData",
		message: `MockData size exceeds maximum allowed (${MAX_DATA_SIZE} characters)`
	});
	for (const { pattern, message } of DANGEROUS_PATTERNS) if (pattern.test(render.component)) errors.push({
		field: "render.component",
		message: `Component contains potentially dangerous pattern: ${message}`
	});
	const importMatches = render.component.matchAll(/import\s+.*?\s+from\s+['"]([^'"]+)['"]/g);
	for (const match of importMatches) {
		const importPath = match[1];
		if (!ALLOWED_IMPORTS.includes(importPath) && !importPath.startsWith(".")) errors.push({
			field: "render.component",
			message: `Import from "${importPath}" is not allowed. Only imports from ${ALLOWED_IMPORTS.join(", ")} are permitted`
		});
	}
	if (!/function\s+\w+\s*\(/.test(render.component)) errors.push({
		field: "render.component",
		message: "Component must contain a function declaration"
	});
	if (!/return\s*\(?\s*</.test(render.component)) errors.push({
		field: "render.component",
		message: "Component function must have a return statement with JSX"
	});
	return {
		isValid: errors.length === 0,
		errors
	};
}

//#endregion
export { validateRender };