// Web Worker that runs the genuine Sketch-to-3D cleanup pixel pipeline off the
// main thread. Receives a transferable RGBA buffer + settings, returns the
// processed RGBA buffer (dims may change when trim padding is enabled).
import { processImageData } from './imageProcessing';

self.onmessage = (e) => {
  const { id, width, height, buffer, settings } = e.data || {};
  try {
    const src = new ImageData(new Uint8ClampedArray(buffer), width, height);
    const result = processImageData(src, settings);
    self.postMessage(
      { id, width: result.width, height: result.height, buffer: result.data.buffer },
      [result.data.buffer]
    );
  } catch (err) {
    self.postMessage({ id, error: String(err?.message || err) });
  }
};