import { init, default as encode } from '@discourse/jxl/encode.js';

self.onmessage = async (e: MessageEvent<{ imageData: ImageData; quality: number }>) => {
	try {
		await init({ locateFile: (path: string) => `/${path}` });
		const encoded = await encode(e.data.imageData, { effort: 4, quality: e.data.quality });
		self.postMessage({ encoded, format: 'jxl' }, { transfer: [encoded as Transferable] });
	} catch (err) {
		self.postMessage({ error: (err as Error).message, format: 'jxl' });
	}
};
