import { hashObject } from "../../utils/hashObject.js";
import { createParallelQueryResult } from "../parallelQueryResult.js";
import { processDistinctQueryAndUpdateRangeMap } from "../PartitionRangeUtils.js";
/** @hidden */
export class OrderedDistinctEndpointComponent {
    executionContext;
    hashedLastResult;
    constructor(executionContext, hashedLastResult) {
        this.executionContext = executionContext;
        this.hashedLastResult = hashedLastResult;
    }
    hasMoreResults() {
        return this.executionContext.hasMoreResults();
    }
    async fetchMore(diagnosticNode) {
        const buffer = [];
        const response = await this.executionContext.fetchMore(diagnosticNode);
        if (!response ||
            !response.result ||
            !Array.isArray(response.result.buffer) ||
            response.result.buffer.length === 0) {
            return { result: response.result, headers: response.headers };
        }
        const parallelResult = response.result;
        const dataToProcess = parallelResult.buffer;
        const partitionKeyRangeMap = parallelResult.partitionKeyRangeMap;
        const updatedContinuationRanges = parallelResult.updatedContinuationRanges;
        const orderByItems = parallelResult.orderByItems;
        // Process each item and maintain hashedLastResult for distinct filtering
        for (const item of dataToProcess) {
            if (item) {
                const hashedResult = await hashObject(item);
                if (hashedResult !== this.hashedLastResult) {
                    buffer.push(item);
                    this.hashedLastResult = hashedResult;
                }
            }
        }
        // Process distinct query logic and update partition key range map with hashedLastResult
        const updatedPartitionKeyRangeMap = await processDistinctQueryAndUpdateRangeMap(dataToProcess, partitionKeyRangeMap, hashObject);
        // Return in the new structure format using the utility function
        const result = createParallelQueryResult(buffer, updatedPartitionKeyRangeMap, updatedContinuationRanges, orderByItems);
        return {
            result,
            headers: response.headers,
        };
    }
}
//# sourceMappingURL=OrderedDistinctEndpointComponent.js.map