import { PCDLoader } from 'three/examples/jsm/loaders/PCDLoader.js';
const loader = new PCDLoader();
globalThis.onmessage = async (event) => {
    const { data, id } = event.data;
    if (!(data instanceof Uint8Array)) {
        postMessage({ id, error: 'Invalid data format' });
        return;
    }
    try {
        const pcd = loader.parse(data.buffer);
        if (pcd.geometry) {
            /**
             * Positions is _usually_ defined. However, we have experienced parsing PCDs from Viam APIs that
             * result in the Three.js parser not attaching this attribute, throwing errors downstream.
             */
            const positions = pcd.geometry.attributes.position?.array ??
                new Float32Array(0);
            const colorsFloat = pcd.geometry.attributes.color?.array ?? null;
            const colors = colorsFloat ? new Uint8Array(colorsFloat.length) : null;
            if (colors) {
                for (let i = 0, l = colorsFloat.length; i < l; i++) {
                    colors[i] = Math.round(colorsFloat[i] * 255);
                }
            }
            postMessage({ positions, colors, id }, colors ? [positions.buffer, colors.buffer] : [positions.buffer]);
        }
        else {
            postMessage({ id, error: 'Failed to extract geometry' });
        }
    }
    catch (error) {
        postMessage({ id, error: error.message });
    }
};
