/**
 * Internal subprocess entry point for workspace preparation.
 * Spawned via fork() by prepare.ts — not a public API.
 *
 * IPC protocol:
 *   Parent → Child:  { type: 'start', options: BuildCodebaseOptions }
 *   Parent → Child:  { type: 'abort' }
 *   Child → Parent:  { type: 'progress', event: ProgressEvent }
 *   Child → Parent:  { type: 'complete', resultPath: string }
 *   Child → Parent:  { type: 'aborted' }
 *   Child → Parent:  { type: 'error', message: string }
 */
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { buildCodebaseIndex } from '../code/build.js';
import { writeSectionedCodeIndex } from '../code/index-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 index = await buildCodebaseIndex(msg.options, (event) => {
            send({ type: 'progress', event });
        }, abortController.signal);
        const resultPath = join(tmpdir(), `occ-prepare-${process.pid}-${Date.now()}`);
        await writeSectionedCodeIndex(resultPath, index, {}, 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=prepare-runner.js.map