/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
/**
 * An iterator that supports having items pushed onto it for later iteration.
 */
export class StackyIterator {
    constructor(list) {
        this.stack = [];
        this.index = 0;
        this.list = list;
    }
    [Symbol.iterator]() {
        return this;
    }
    next() {
        if (this.done) {
            return { value: undefined, done: true };
        }
        return { value: this.pop() };
    }
    get done() {
        return this.index >= this.list.length && this.stack.length === 0;
    }
    push(item) {
        this.stack.push(item);
    }
    pop() {
        if (this.stack.length > 0) {
            return this.stack.pop();
        }
        if (this.index >= this.list.length) {
            return undefined;
        }
        return this.list[this.index++];
    }
    peek() {
        if (this.stack.length > 0) {
            return this.stack[this.stack.length - 1];
        }
        return this.list[this.index];
    }
}
//# sourceMappingURL=stackyIterator.js.map