"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.idAllocatorFromState = exports.idAllocatorFromMaxId = exports.CrossFieldTarget = exports.getFirstFromCrossFieldMap = exports.setInCrossFieldMap = exports.addCrossFieldQuery = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const util_1 = require("../../util");
function addCrossFieldQuery(set, revision, id, count) {
    setInCrossFieldMap(set, revision, id, count, true);
}
exports.addCrossFieldQuery = addCrossFieldQuery;
function setInCrossFieldMap(map, revision, id, count, value) {
    (0, util_1.setInRangeMap)((0, util_1.getOrAddInMap)(map, revision, []), id, count, value);
}
exports.setInCrossFieldMap = setInCrossFieldMap;
function getFirstFromCrossFieldMap(map, revision, id, count) {
    return (0, util_1.getFirstFromRangeMap)(map.get(revision) ?? [], id, count);
}
exports.getFirstFromCrossFieldMap = getFirstFromCrossFieldMap;
/**
 * @alpha
 */
var CrossFieldTarget;
(function (CrossFieldTarget) {
    CrossFieldTarget[CrossFieldTarget["Source"] = 0] = "Source";
    CrossFieldTarget[CrossFieldTarget["Destination"] = 1] = "Destination";
})(CrossFieldTarget = exports.CrossFieldTarget || (exports.CrossFieldTarget = {}));
/**
 * @alpha
 */
function idAllocatorFromMaxId(maxId = undefined) {
    return idAllocatorFromState({ maxId: maxId ?? (0, util_1.brand)(-1) });
}
exports.idAllocatorFromMaxId = idAllocatorFromMaxId;
function idAllocatorFromState(state) {
    return (c) => {
        const count = c ?? 1;
        (0, common_utils_1.assert)(count > 0, 0x5cf /* Must allocate at least one ID */);
        const id = (0, util_1.brand)(state.maxId + 1);
        state.maxId = (0, util_1.brand)(state.maxId + count);
        return id;
    };
}
exports.idAllocatorFromState = idAllocatorFromState;
//# sourceMappingURL=crossFieldQueries.js.map