import { initializeBrowser, closeBrowser } from '$lib/server/browser';

// Initialize browser when server starts
export async function init() {
	try {
		await initializeBrowser();
	} catch (error) {
		console.error('Failed to initialize browser:', error);
		process.exit(1);
	}

	// Graceful shutdown
	process.on('SIGTERM', async () => {
		console.log('SIGTERM received, shutting down gracefully...');
		await closeBrowser();
		process.exit(0);
	});

	process.on('SIGINT', async () => {
		console.log('SIGINT received, shutting down gracefully...');
		await closeBrowser();
		process.exit(0);
	});
}

// Run initialization immediately
init().catch(console.error);
