/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { subtractChangeAtomIds, } from "../../core/index.js";
import { brand, RangeMap, } from "../../util/index.js";
export function getFromChangeAtomIdMap(map, id) {
    return map.get([id.revision, id.localId]);
}
export function rangeQueryChangeAtomIdMap(map, id, count) {
    const pair = map.getPairOrNextHigher([id.revision, id.localId]);
    if (pair === undefined) {
        return { value: undefined, length: count };
    }
    const [[revision, localId], value] = pair;
    const lengthBefore = subtractChangeAtomIds({ revision, localId }, id);
    if (lengthBefore === 0) {
        return { value, length: 1 };
    }
    return { value: undefined, length: Math.min(lengthBefore, count) };
}
export function setInChangeAtomIdMap(map, id, value) {
    return map.set([id.revision, id.localId], value);
}
export function newCrossFieldRangeTable() {
    return new RangeMap(offsetCrossFieldKey, subtractCrossFieldKeys);
}
function offsetCrossFieldKey(key, offset) {
    return {
        ...key,
        localId: brand(key.localId + offset),
    };
}
function subtractCrossFieldKeys(a, b) {
    const cmpTarget = a.target - b.target;
    if (cmpTarget !== 0) {
        return cmpTarget * Number.POSITIVE_INFINITY;
    }
    return subtractChangeAtomIds(a, b);
}
//# sourceMappingURL=modularChangeTypes.js.map