/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { loadWorker } from "worker:./png-decoder.js";
let workerLoader;
let poolLoader;
const DEFAULT_POOL_SIZE = 1;
function loadWorkerModule() {
    if (workerLoader == null) {
        console.debug(`Loading PNG worker module`);
        workerLoader = loadWorker();
    }
    return workerLoader;
}
function getPoolSize() {
    var _a;
    if (typeof window !== 'undefined') {
        const concurrency = (_a = window.navigator.hardwareConcurrency) !== null && _a !== void 0 ? _a : 8;
        return Math.ceil(concurrency / 4);
    }
    else {
        return DEFAULT_POOL_SIZE;
    }
}
async function getPool() {
    if (poolLoader == null) {
        poolLoader = loadWorkerModule().then(async ({ spawnPool }) => {
            const size = getPoolSize();
            console.debug(`Spawning PNG worker pool [size=${size}]`);
            return spawnPool({ size });
        });
    }
    return poolLoader;
}
export const decodePng = async (bytes) => {
    const pool = await getPool();
    return pool.queue((decode) => decode(bytes));
};
// Prefetch the worker and initialize the pool in browsers only.
if (typeof window !== 'undefined') {
    void getPool();
}
//# sourceMappingURL=png-decoder-pool.js.map
