import { isNode as e } from "../constants/vite.constants.mjs";
import { workerNodeURL as t } from "./worker.registry.mjs";
import { WorkerWrapper as n } from "./WorkerWrapper.mjs";
//#region libs/shared/common/src/webworker/workerFactory.ts
var r = class r {
	constructor() {}
	static resolveWorkerURL = async (e) => {
		let { getWorkerViteURL: t } = await import("./viteWorkerURL.constant.mjs");
		return (await t())[e];
	};
	static async createBrowserWorker(e) {
		let t = await r.resolveWorkerURL(e);
		return new Worker(t, { type: "module" });
	}
	static async createNodeWorker(e) {
		let { Worker: n } = await import("worker_threads"), r = await import("path"), { existsSync: i } = await import("fs"), a = t[e].replace(".js", ".cjs"), o, s = r.join(__dirname, a), c = s.replace(".cjs", ".js"), l = null;
		if (i(s) ? l = s : i(c) && (l = c), l) o = l;
		else {
			let e = __dirname;
			for (let t = 0; t < 12; t += 1) {
				let t = r.join(e, "dist", "libs", "shared", "common", "webworker", a);
				if (i(t)) {
					o = t;
					break;
				}
				let n = r.dirname(e);
				if (n === e) break;
				e = n;
			}
		}
		if (!o) throw Error(`Worker bundle not found (${a}). Run "nx run shared-common:postbuild:node-workers".`);
		return new n(o);
	}
	static async createWorker(t) {
		return e ? r.createNodeWorker(t) : r.createBrowserWorker(t);
	}
	static async getWebWorker(e) {
		return new n(await r.createWorker(e));
	}
};
//#endregion
export { r as WorkerFactory };
