// 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 * as Comlink from "comlink";
import { ConfigStore } from "../utils/config-store";
function initWorker(callback) {
    const workerWrap = Comlink.wrap(new Worker(new URL("./deserialization.worker", import.meta.url), { type: "module", name: `message-decoder` }));
    workerWrap.initCallback(Comlink.proxy(callback));
    return workerWrap;
}
const neuralCameraIntrinsics = JSON.parse(localStorage.getItem("neuralCameraIntrinsics") ?? "{}");
export function createMessageHandler(callback) {
    const workerPool = new Map();
    return {
        handle: (({ schema, buffer, topic }, errorCallback) => {
            let worker = workerPool.get(topic);
            if (!worker) {
                const newWorker = initWorker(callback);
                worker = newWorker;
                workerPool.set(topic, newWorker);
            }
            const proxiedCallback = errorCallback ? Comlink.proxy(errorCallback) : undefined;
            worker.handleMessageToDecode({ schema, buffer, topic, neuralCameraIntrinsics, stereoDepthMaxValue: Number(localStorage.getItem("stereoDepthMaxValue") ?? "8000") }, ConfigStore.getInstance(), proxiedCallback);
        }),
    };
}
//# sourceMappingURL=message-handler.js.map