"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParallelQueryExecutionContext = void 0;
const parallelQueryExecutionContextBase_js_1 = require("./parallelQueryExecutionContextBase.js");
const TargetPartitionRangeManager_js_1 = require("./queryFilteringStrategy/TargetPartitionRangeManager.js");
const ParallelQueryProcessingStrategy_js_1 = require("./queryProcessingStrategy/ParallelQueryProcessingStrategy.js");
/**
 * Provides the ParallelQueryExecutionContext.
 * This class is capable of handling parallelized queries and derives from ParallelQueryExecutionContextBase.
 * @hidden
 */
class ParallelQueryExecutionContext extends parallelQueryExecutionContextBase_js_1.ParallelQueryExecutionContextBase {
    constructor(clientContext, collectionLink, query, options, partitionedQueryExecutionInfo, correlatedActivityId) {
        const rangeManager = TargetPartitionRangeManager_js_1.TargetPartitionRangeManager.createForParallelQuery({
            queryInfo: partitionedQueryExecutionInfo,
        });
        // Create parallel query processing strategy
        const processingStrategy = new ParallelQueryProcessingStrategy_js_1.ParallelQueryProcessingStrategy();
        // Create comparator for document producers
        const comparator = ParallelQueryExecutionContext.createDocumentProducerComparator();
        // Calling on base class constructor
        super(clientContext, collectionLink, query, options, partitionedQueryExecutionInfo, correlatedActivityId, rangeManager, processingStrategy, comparator);
    }
    /**
     * Fetches all buffered items from producer for parallel processing.
     */
    async fetchFromProducer(producer) {
        return producer.fetchBufferedItems();
    }
    /**
     * Determines if buffered producers should continue to be processed for parallel queries.
     * For parallel queries, we process all buffered producers.
     * @param _isUnfilledQueueEmpty - Whether the unfilled queue is empty (ignored for parallel queries)
     * @hidden
     */
    shouldProcessBufferedProducers(_isUnfilledQueueEmpty) {
        return true; // Process all buffered items in parallel queries
    }
    /**
     * Creates a comparator function for sorting document producers in parallel queries.
     * Sorts by partition key range minInclusive values, with empty string first,
     * then lexicographically. Uses EPK ranges as secondary sort when minInclusive values are identical.
     */
    static createDocumentProducerComparator() {
        return (docProd1, docProd2) => {
            const aMinInclusive = docProd1.targetPartitionKeyRange.minInclusive;
            const bMinInclusive = docProd2.targetPartitionKeyRange.minInclusive;
            // Sort empty string first, then lexicographically (original logic)
            if (aMinInclusive === bMinInclusive) {
                // If minInclusive values are the same, check minEPK ranges if they exist
                const aMinEpk = docProd1.startEpk;
                const bMinEpk = docProd2.startEpk;
                if (aMinEpk && bMinEpk) {
                    return aMinEpk < bMinEpk ? -1 : 1;
                }
                return 0;
            }
            if (aMinInclusive === "")
                return -1;
            if (bMinInclusive === "")
                return 1;
            return aMinInclusive < bMinInclusive ? -1 : 1;
        };
    }
}
exports.ParallelQueryExecutionContext = ParallelQueryExecutionContext;
//# sourceMappingURL=parallelQueryExecutionContext.js.map