/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import process from 'node:process';
import { TerminalBufferWorker } from './render-worker.js';
let buffer;
const main = () => {
    process.on('message', async (message) => {
        switch (message.type) {
            case 'init': {
                const columns = (process.stdout.columns || message.columns);
                const rows = (process.stdout.rows || message.rows);
                buffer = new TerminalBufferWorker(columns, rows, {
                    debugRainbowEnabled: message.debugRainbowEnabled,
                    isAlternateBufferEnabled: message.isAlternateBufferEnabled,
                    stickyHeadersInBackbuffer: message.stickyHeadersInBackbuffer,
                    animatedScroll: message.animatedScroll,
                    animationInterval: message.animationInterval,
                    backbufferUpdateDelay: message.backbufferUpdateDelay,
                    maxScrollbackLength: message.maxScrollbackLength,
                    forceScrollToBottomOnBackbufferRefresh: message.forceScrollToBottomOnBackbufferRefresh,
                });
                break;
            }
            case 'updateOptions': {
                if (buffer) {
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
                    buffer.updateOptions(message.options);
                }
                break;
            }
            case 'edits': {
                if (buffer) {
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
                    buffer.update(message.tree, message.updates, message.cursorPosition);
                }
                break;
            }
            case 'fullRender': {
                if (buffer) {
                    void buffer.fullRender();
                }
                break;
            }
            case 'render': {
                if (buffer) {
                    await buffer.render();
                    process.send?.({ type: 'renderDone' });
                }
                break;
            }
            case 'done': {
                if (buffer) {
                    buffer.done();
                    process.send?.({ type: 'doneConfirmed' });
                }
                break;
            }
            case 'getLinesUpdated': {
                if (buffer) {
                    process.send?.({
                        type: 'linesUpdated',
                        count: buffer.getLinesUpdated(),
                    });
                }
                break;
            }
            case 'resetLinesUpdated': {
                if (buffer) {
                    buffer.resetLinesUpdated();
                }
                break;
            }
            case 'clear': {
                if (buffer) {
                    buffer.clear();
                    process.send?.({ type: 'clearDone' });
                }
                break;
            }
            case 'resize': {
                if (buffer) {
                    buffer.resize(message.columns, message.rows);
                }
                break;
            }
            case 'dumpCurrentFrame': {
                if (buffer) {
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
                    buffer.dumpCurrentFrame(message.filename);
                }
                break;
            }
            case 'startRecording': {
                if (buffer) {
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
                    buffer.startRecording(message.filename);
                }
                break;
            }
            case 'stopRecording': {
                if (buffer) {
                    buffer.stopRecording();
                }
                break;
            }
            case 'waitForIdle': {
                if (buffer) {
                    await buffer.waitForIdle();
                    process.send?.({ type: 'idle' });
                }
                break;
            }
            default: {
                break;
            }
        }
    });
    process.stdout.on('resize', () => {
        if (buffer && process.stdout.columns && process.stdout.rows) {
            buffer.resize(process.stdout.columns, process.stdout.rows);
        }
    });
};
if (process.env['INK_WORKER'] === 'true') {
    main();
}
//# sourceMappingURL=worker-entry.js.map