function isRunLikeCommand(commandName) {
    return commandName === 'run' || commandName === 'watch';
}
export function installImmediateSigintExit(commandName, runtime = process) {
    if (!isRunLikeCommand(commandName)) {
        return () => { };
    }
    const stdin = runtime.stdin;
    const hadRawMode = stdin.isRaw === true;
    let enabledRawMode = false;
    let cleanedUp = false;
    if (!stdin.isTTY) {
        return () => { };
    }
    if (typeof stdin.setRawMode === 'function' && !hadRawMode) {
        stdin.setRawMode(true);
        enabledRawMode = true;
    }
    const onData = (chunk) => {
        const text = typeof chunk === 'string' ? chunk : chunk.toString('utf-8');
        if (text.includes('\u0003')) {
            runtime.emit('SIGINT');
        }
    };
    stdin.on('data', onData);
    stdin.resume();
    const cleanup = () => {
        if (cleanedUp) {
            return;
        }
        cleanedUp = true;
        stdin.removeListener('data', onData);
        stdin.pause();
        if (enabledRawMode && typeof stdin.setRawMode === 'function') {
            stdin.setRawMode(false);
        }
    };
    runtime.once('exit', cleanup);
    return cleanup;
}
//# sourceMappingURL=immediateSigintExit.js.map