import { WeaviateDeserializationError } from '../../errors.js';
const ITERATOR_CACHE_SIZE = 100;
export class Iterator {
    query;
    cache = [];
    last = undefined;
    constructor(query) {
        this.query = query;
        this.query = query;
    }
    [Symbol.asyncIterator]() {
        return {
            next: async () => {
                const objects = await this.query(ITERATOR_CACHE_SIZE, this.last);
                this.cache = objects;
                if (this.cache.length == 0) {
                    return {
                        done: true,
                        value: undefined,
                    };
                }
                const obj = this.cache.shift();
                if (obj === undefined) {
                    throw new WeaviateDeserializationError('Object iterator returned an object that is undefined');
                }
                this.last = obj?.uuid;
                if (this.last === undefined) {
                    throw new WeaviateDeserializationError('Object iterator returned an object without a UUID');
                }
                return {
                    done: false,
                    value: obj,
                };
            },
        };
    }
}
