/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { BTree } from "@tylerbu/sorted-btree-es6";
import { brand } from "./brand.js";
export function newTupleBTree(entries) {
    return brand(new BTree(entries, compareTuples));
}
// 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;
}
export function mergeTupleBTrees(tree1, tree2, preferLeft = true) {
    if (tree1 === undefined) {
        return tree2 !== undefined ? brand(tree2.clone()) : newTupleBTree();
    }
    const result = brand(tree1.clone());
    if (tree2 === undefined) {
        return result;
    }
    for (const [key, value] of tree2.entries()) {
        result.set(key, value, !preferLeft);
    }
    return result;
}
//# sourceMappingURL=bTreeUtils.js.map