// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//import init, { decode_i420_simd } from "./pkg/viewer_rust_decoder.js";
/**
 * Provides a worker that can process RawImages on a background thread.
 *
 * The input image data must be **copied** to the worker, because image messages may be used
 * concurrently by other panels and features of the app. However, the resulting decoded data is
 * [transferred](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects)
 * back to the main thread.
 */
export class WorkerImageDecoder {
    //#wasmInitialized = false;
    #worker; // have more workers
    //#reusableBuffer: SharedArrayBuffer | null = null;
    constructor() {
        this.#worker = new Worker(
        // foxglove-depcheck-used: babel-plugin-transform-import-meta
        new URL("./WorkerImageDecoder.worker", import.meta.url), { type: "module" });
    }
    /**
     * Copies `image` to the worker, and transfers the decoded result back to the main thread.
     */
    decode(image, options) {
        return new Promise((resolve, reject) => {
            // Create or resize buffer if needed
            //if (!this.#reusableBuffer || this.#reusableBuffer.byteLength !== requiredSize) {
            //  console.log("Resizing buffer", this.#reusableBuffer?.byteLength);
            //  this.#reusableBuffer = new SharedArrayBuffer(requiredSize);
            //}
            this.#worker.onmessage = (event) => {
                if (event.data.error) {
                    reject(new Error(event.data.error));
                }
                else {
                    resolve(new ImageData(event.data.result, image.width, image.height));
                }
            };
            // TODO: Try using shared buffer for input data as well
            this.#worker.postMessage({ image, options }, [
                image.data.buffer,
            ]);
        });
    }
    //public async decodeOnMain(image: RosImage | RawImage, options: Partial<RawImageOptions>): Promise<ImageData | ImageBitmap> {
    //    const requiredSize = image.width * image.height * 4;
    //
    //    // Create or resize buffer if needed
    //    if (!this.#reusableBuffer || this.#reusableBuffer.length < requiredSize) {
    //      console.log('Resizing buffer', this.#reusableBuffer?.length);
    //      this.#reusableBuffer = new Uint8ClampedArray(requiredSize);
    //    }
    //
    //    return decodeRawImage(image, options, this.#reusableBuffer).then(() => {
    //      return new ImageData(this.#reusableBuffer!, image.width, image.height);
    //    });
    //}
    //public async decodeRust(
    //  image: RosImage | RawImage,
    //  options: Partial<RawImageOptions>,
    //): Promise<ImageData> {
    //  if (!this.#wasmInitialized) {
    //    console.log("Initializing wasm decoder");
    //    await init();
    //    this.#wasmInitialized = true;
    //  }
    //
    //  // TODO: Potentially use async rs function to improve performance
    //  const decodedBuffer: Uint8Array = decode_i420_simd(
    //    image.data as Uint8Array,
    //    image.width,
    //    image.height,
    //    0,
    //  );
    //
    //  return new ImageData(new Uint8ClampedArray(decodedBuffer), image.width, image.height);
    //}
    terminate() {
        this.#worker.terminate();
    }
}
//# sourceMappingURL=WorkerImageDecoder.js.map