import { createInterface } from 'node:readline';
export class NodeStdinLineReader {
    readline;
    buffered = [];
    waiting = [];
    closed = false;
    constructor(input = process.stdin) {
        this.readline = createInterface({ input });
        this.readline.on('line', (line) => this.enqueue(line));
        this.readline.on('close', () => this.finish());
    }
    async read() {
        const buffered = this.buffered.shift();
        if (buffered !== undefined)
            return buffered;
        if (this.closed)
            return null;
        return new Promise((resolve) => {
            this.waiting.push(resolve);
        });
    }
    enqueue(line) {
        const resolver = this.waiting.shift();
        if (resolver) {
            resolver(line);
            return;
        }
        this.buffered.push(line);
    }
    finish() {
        this.closed = true;
        while (this.waiting.length > 0) {
            const resolver = this.waiting.shift();
            if (resolver)
                resolver(null);
        }
    }
}
//# sourceMappingURL=lineReader.stdin.gateway.js.map