import { encodeAnimation, decodeAnimation } from './wasm-webp/index.js';

self.onmessage = async (e) => {
    let action = e.data.action || 'encode';
    try {
        if (action === 'decode') {
            let decoded = await decodeAnimation(e.data.data, true);
            let transferables = decoded.map(f => f.data.buffer);
            self.postMessage({ success: true, data: decoded, action }, transferables);
        } else {
            let { targetWidth, targetHeight, webpFrames } = e.data;
            let webpUint8 = await encodeAnimation(targetWidth, targetHeight, true, webpFrames);
            self.postMessage({ success: true, data: webpUint8, action }, [webpUint8.buffer]);
        }
    } catch (err) {
        self.postMessage({ success: false, error: err.toString(), action });
    }
};
