import { init, default as encode } from '@jsquash/webp/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, { quality: e.data.quality });
		self.postMessage({ encoded, format: 'webp' }, { transfer: [encoded as Transferable] });
	} catch (err) {
		self.postMessage({ error: (err as Error).message, format: 'webp' });
	}
};
