import { hashObject } from "../../utils/hashObject.js";
import { createParallelQueryResult } from "../parallelQueryResult.js";
import { getInitialHeader } from "../headerUtils.js";
/** @hidden */
export class UnorderedDistinctEndpointComponent {
    executionContext;
    hashedResults;
    constructor(executionContext) {
        this.executionContext = executionContext;
        this.hashedResults = new Set();
    }
    hasMoreResults() {
        const result = this.executionContext.hasMoreResults();
        return result;
    }
    async fetchMore(diagnosticNode) {
        const buffer = [];
        const response = await this.executionContext.fetchMore(diagnosticNode);
        if (!response) {
            const result = createParallelQueryResult([], new Map(), {}, undefined);
            return { result, headers: getInitialHeader() };
        }
        if (response.result === undefined ||
            !Array.isArray(response.result.buffer) ||
            response.result.buffer.length === 0) {
            const result = createParallelQueryResult([], new Map(), {}, undefined);
            return { result, headers: response.headers };
        }
        const parallelResult = response.result;
        const dataToProcess = parallelResult.buffer;
        for (const item of dataToProcess) {
            if (item) {
                const hashedResult = await hashObject(item);
                if (!this.hashedResults.has(hashedResult)) {
                    buffer.push(item);
                    this.hashedResults.add(hashedResult);
                }
            }
        }
        const result = createParallelQueryResult(buffer, new Map(), undefined, undefined);
        return { result, headers: response.headers };
    }
}
//# sourceMappingURL=UnorderedDistinctEndpointComponent.js.map