let pyodideWorker;
const callbacks = {};
let id = 0;
function setup() {
    pyodideWorker = new Worker('/pyodide/webworker.js');
    pyodideWorker.onmessage = (event) => {
        const { id, ...data } = event.data;
        const { resolve, reject, script, context } = callbacks[id];
        delete callbacks[id];
        if ('error' in data) {
            reject(new Error(`${data.error.message || data.error}\n\tat script: ${script}\n\n\twith context: ${JSON.stringify(context)}`));
        }
        else
            resolve(data.results);
    };
}
export function runPython(script, { context, global }) {
    if (!pyodideWorker)
        setup();
    id = (id + 1) % Number.MAX_SAFE_INTEGER;
    return new Promise((resolve, reject) => {
        callbacks[id] = { resolve, reject, script, context };
        pyodideWorker?.postMessage({
            context,
            script,
            id,
            global,
        });
    });
}
//# sourceMappingURL=python.js.map