import { availableParallelism, FixedThreadPool } from "poolifier";
import { getConfig } from "../config/index.js";
import { isLike, ok } from "../is.js";
import { getWorkerURLForImportURL } from "./worker.js";
const getDefaultPool = function getDefaultPool(availableParallelism, url, options) {
    return new FixedThreadPool(1, url, options);
};
const pools = new WeakMap();
export function getWorkerPoolForImportURL(relative, url, options) {
    return getWorkerPool(getWorkerURLForImportURL(relative, url), options);
}
/**
 * @param url
 * @param options used when new pool created for url, not used if exists
 */
export function getWorkerPool(url, options) {
    const instance = new URL(url);
    const { protocol } = instance;
    ok(protocol === "file:", "Only file import worker supported... for now, please open an issue");
    const config = getConfig();
    const fn = config.getWorkerPool ?? getDefaultPool;
    const cache = pools.get(fn) ?? {};
    const urlString = instance.toString();
    const existing = cache?.[urlString];
    if (isLike(existing)) {
        return existing;
    }
    const pool = fn(availableParallelism(), instance.pathname, {
        ...options
    });
    cache[urlString] = pool;
    pools.set(fn, cache);
    return pool;
}
//# sourceMappingURL=pool.js.map