"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.newCrossFieldRangeTable = exports.setInChangeAtomIdMap = exports.rangeQueryChangeAtomIdMap = exports.getFromChangeAtomIdMap = void 0;
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../../util/index.js");
function getFromChangeAtomIdMap(map, id) {
    return map.get([id.revision, id.localId]);
}
exports.getFromChangeAtomIdMap = getFromChangeAtomIdMap;
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 = (0, index_js_1.subtractChangeAtomIds)({ revision, localId }, id);
    if (lengthBefore === 0) {
        return { value, length: 1 };
    }
    return { value: undefined, length: Math.min(lengthBefore, count) };
}
exports.rangeQueryChangeAtomIdMap = rangeQueryChangeAtomIdMap;
function setInChangeAtomIdMap(map, id, value) {
    return map.set([id.revision, id.localId], value);
}
exports.setInChangeAtomIdMap = setInChangeAtomIdMap;
function newCrossFieldRangeTable() {
    return new index_js_2.RangeMap(offsetCrossFieldKey, subtractCrossFieldKeys);
}
exports.newCrossFieldRangeTable = newCrossFieldRangeTable;
function offsetCrossFieldKey(key, offset) {
    return {
        ...key,
        localId: (0, index_js_2.brand)(key.localId + offset),
    };
}
function subtractCrossFieldKeys(a, b) {
    const cmpTarget = a.target - b.target;
    if (cmpTarget !== 0) {
        return cmpTarget * Number.POSITIVE_INFINITY;
    }
    return (0, index_js_1.subtractChangeAtomIds)(a, b);
}
//# sourceMappingURL=modularChangeTypes.js.map