import { buildChangeFeedIterator } from "./buildChangeFeedIterator.js";
import { ErrorResponse } from "../../request/index.js";
/**
 * @hidden
 * Provides iterator for change feed.
 *
 * Use `Items.getChangeFeedIterator()` to get an instance of the iterator.
 */
export class ChangeFeedIteratorBuilder {
    cfOptions;
    clientContext;
    container;
    partitionKeyRangeCache;
    iterator;
    isInitialized;
    /**
     * @internal
     */
    constructor(cfOptions, clientContext, container, partitionKeyRangeCache) {
        this.cfOptions = cfOptions;
        this.clientContext = clientContext;
        this.container = container;
        this.partitionKeyRangeCache = partitionKeyRangeCache;
        this.isInitialized = false;
    }
    /**
     * Change feed is an infinite feed. hasMoreResults is always true.
     */
    get hasMoreResults() {
        return true;
    }
    /**
     * Gets an async iterator which will yield change feed results.
     */
    async *getAsyncIterator() {
        await this.initializeIterator();
        do {
            const result = await this.iterator.readNext();
            yield result;
        } while (this.hasMoreResults);
    }
    /**
     * Returns the result of change feed from Azure Cosmos DB.
     */
    async readNext() {
        await this.initializeIterator();
        return this.iterator.readNext();
    }
    async initializeIterator() {
        if (!this.isInitialized) {
            try {
                const iterator = await buildChangeFeedIterator(this.cfOptions, this.clientContext, this.container, this.partitionKeyRangeCache);
                this.isInitialized = true;
                this.iterator = iterator;
            }
            catch (err) {
                throw new ErrorResponse(err.message);
            }
        }
    }
}
//# sourceMappingURL=ChangeFeedIteratorBuilder.js.map