"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeTupleBTrees = exports.newTupleBTree = void 0;
const sorted_btree_es6_1 = require("@tylerbu/sorted-btree-es6");
const brand_js_1 = require("./brand.js");
function newTupleBTree(entries) {
    return (0, brand_js_1.brand)(new sorted_btree_es6_1.BTree(entries, compareTuples));
}
exports.newTupleBTree = newTupleBTree;
// This assumes that the arrays are the same length.
function compareTuples(arrayA, arrayB) {
    for (let i = 0; i < arrayA.length; i++) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const a = arrayA[i];
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const b = arrayB[i];
        // Less-than and greater-than always return false if either value is undefined,
        // so we handle undefined separately, treating it as less than all other values.
        if (a === undefined && b !== undefined) {
            return -1;
        }
        else if (b === undefined && a !== undefined) {
            return 1;
        }
        else if (a < b) {
            return -1;
        }
        else if (a > b) {
            return 1;
        }
    }
    return 0;
}
function mergeTupleBTrees(tree1, tree2, preferLeft = true) {
    if (tree1 === undefined) {
        return tree2 !== undefined ? (0, brand_js_1.brand)(tree2.clone()) : newTupleBTree();
    }
    const result = (0, brand_js_1.brand)(tree1.clone());
    if (tree2 === undefined) {
        return result;
    }
    for (const [key, value] of tree2.entries()) {
        result.set(key, value, !preferLeft);
    }
    return result;
}
exports.mergeTupleBTrees = mergeTupleBTrees;
//# sourceMappingURL=bTreeUtils.js.map