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