"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareReferencePositions = exports.maxReferencePosition = exports.minReferencePosition = exports.DetachedReferencePosition = exports.refHasTileLabels = exports.refHasTileLabel = exports.refGetTileLabels = exports.refTypeIncludesFlag = exports.reservedRangeLabelsKey = exports.reservedTileLabelsKey = void 0;
const ops_js_1 = require("./ops.js");
const segmentInfos_js_1 = require("./segmentInfos.js");
/**
 * @internal
 */
exports.reservedTileLabelsKey = "referenceTileLabels";
/**
 * @internal
 */
exports.reservedRangeLabelsKey = "referenceRangeLabels";
/**
 * Determines if the given reference type includes the given flags.
 * @internal
 */
function refTypeIncludesFlag(refPosOrType, flags) {
    const refType = typeof refPosOrType === "number" ? refPosOrType : refPosOrType.refType;
    // eslint-disable-next-line no-bitwise
    return (refType & flags) !== 0;
}
exports.refTypeIncludesFlag = refTypeIncludesFlag;
/**
 * Gets the tile labels stored in the given reference position.
 * @legacy
 * @alpha
 */
const refGetTileLabels = (refPos) => refTypeIncludesFlag(refPos, ops_js_1.ReferenceType.Tile) && refPos.properties
    ? refPos.properties[exports.reservedTileLabelsKey]
    : undefined;
exports.refGetTileLabels = refGetTileLabels;
/**
 * Determines if a reference position has the given tile label.
 * @legacy
 * @alpha
 */
function refHasTileLabel(refPos, label) {
    const tileLabels = (0, exports.refGetTileLabels)(refPos);
    return tileLabels?.includes(label) ?? false;
}
exports.refHasTileLabel = refHasTileLabel;
/**
 * Determines if a reference position has any tile labels.
 * @internal
 */
function refHasTileLabels(refPos) {
    return (0, exports.refGetTileLabels)(refPos) !== undefined;
}
exports.refHasTileLabels = refHasTileLabels;
/**
 * @internal
 */
exports.DetachedReferencePosition = -1;
/**
 * Finds the minimum reference position.
 * @internal
 */
function minReferencePosition(a, b) {
    return compareReferencePositions(a, b) < 0 ? a : b;
}
exports.minReferencePosition = minReferencePosition;
/**
 * Finds the maximum reference position.
 * @internal
 */
function maxReferencePosition(a, b) {
    return compareReferencePositions(a, b) > 0 ? a : b;
}
exports.maxReferencePosition = maxReferencePosition;
/**
 * Compares two reference positions.
 * @internal
 */
function compareReferencePositions(a, b) {
    const aSeg = a.getSegment();
    const bSeg = b.getSegment();
    if (aSeg === bSeg) {
        return a.getOffset() - b.getOffset();
    }
    else {
        return !(0, segmentInfos_js_1.isMergeNodeInfo)(aSeg) || ((0, segmentInfos_js_1.isMergeNodeInfo)(bSeg) && aSeg.ordinal < bSeg.ordinal)
            ? -1
            : 1;
    }
}
exports.compareReferencePositions = compareReferencePositions;
//# sourceMappingURL=referencePositions.js.map