export class ScriptedInput {
    keyQueue = [];
    keyWaiters = [];
    lineQueue = [];
    lineWaiters = [];
    /**
     * Optionally pre-load the input with a sequence of keys. Strings are
     * converted to `{ key }` events; `KeyEvent` objects are used as-is.
     */
    constructor(initial) {
        if (initial) {
            for (const item of initial) {
                this.feedKey(typeof item === "string" ? { key: item } : item);
            }
        }
    }
    feedKey(key) {
        const waiter = this.keyWaiters.shift();
        if (waiter) {
            waiter(key);
        }
        else {
            this.keyQueue.push(key);
        }
    }
    feedLine(line) {
        const waiter = this.lineWaiters.shift();
        if (waiter) {
            waiter(line);
        }
        else {
            this.lineQueue.push(line);
        }
    }
    nextKey() {
        const queued = this.keyQueue.shift();
        if (queued !== undefined)
            return Promise.resolve(queued);
        return new Promise((resolve) => {
            this.keyWaiters.push(resolve);
        });
    }
    nextLine(_prompt) {
        const queued = this.lineQueue.shift();
        if (queued !== undefined)
            return Promise.resolve(queued);
        return new Promise((resolve) => {
            this.lineWaiters.push(resolve);
        });
    }
    destroy() {
        this.keyQueue = [];
        this.keyWaiters = [];
        this.lineQueue = [];
        this.lineWaiters = [];
    }
}
