import { getOrigin } from "../listen/index.js";
import { dirname, join } from "node:path";
import { ok } from "../is.js";
async function getDefaultNodeWorker(url, options) {
    const { Worker } = await import("node:worker_threads");
    const worker = new Worker(url, {
        stderr: true,
        stdout: true,
        ...options,
    });
    // stdout is not available in bun
    if (worker.stdout) {
        worker.stdout.pipe(process.stdout);
        worker.stderr.pipe(process.stderr);
    }
    return worker;
}
export function getWorkerURLForImportURL(relative, url) {
    const instance = new URL(url, getOrigin());
    const { pathname } = instance;
    // Note, this isn't true for all urls
    const directory = dirname(pathname);
    instance.pathname = join(directory, relative);
    return instance;
}
export function getNodeWorkerForImportURL(relative, url, options) {
    return getNodeWorker(getWorkerURLForImportURL(relative, url), options);
}
/**
 * @param url
 * @param options used when new pool created for url, not used if exists
 */
export async function getNodeWorker(url, options) {
    const instance = new URL(url);
    const { protocol } = instance;
    ok(protocol === "file:", "Only file import worker supported... for now, please open an issue");
    // console.log("Get worker for", instance.pathname);
    return getDefaultNodeWorker(instance.pathname, {
        ...options
    });
}
//# sourceMappingURL=worker.js.map