/**
 * Internal subprocess entry point for workspace bundles.
 * Spawned via fork() by bundleWorkspaceIsolated — not a public API.
 */
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { bundleWorkspace } from './bundle.js';
import { writeSectionedWorkspaceBundle } from './bundle-io.js';
function send(msg) {
    if (process.send) {
        process.send(msg);
    }
}
let abortController;
process.on('message', async (msg) => {
    if (msg.type === 'abort') {
        abortController?.abort();
        return;
    }
    if (msg.type !== 'start')
        return;
    try {
        abortController = new AbortController();
        const bundle = await bundleWorkspace(msg.rootDir, {
            ...msg.options,
            offThread: false,
            cooperative: undefined,
            signal: abortController.signal,
            onProgress: (event) => {
                send({ type: 'progress', event });
            },
        });
        const resultPath = join(tmpdir(), `occ-bundle-${process.pid}-${Date.now()}`);
        await writeSectionedWorkspaceBundle(resultPath, bundle, abortController.signal);
        send({ type: 'complete', resultPath });
    }
    catch (err) {
        const error = err instanceof Error ? err : new Error(String(err));
        if (error.name === 'AbortError') {
            send({ type: 'aborted' });
        }
        else {
            send({ type: 'error', message: error.message });
        }
    }
    if (process.connected) {
        process.disconnect?.();
    }
});
//# sourceMappingURL=bundle-runner.js.map