/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { ReferenceType } from "./ops";
export const reservedTileLabelsKey = "referenceTileLabels";
export const reservedRangeLabelsKey = "referenceRangeLabels";
export function refTypeIncludesFlag(refPosOrType, flags) {
    const refType = typeof refPosOrType === "number" ? refPosOrType : refPosOrType.refType;
    // eslint-disable-next-line no-bitwise
    return (refType & flags) !== 0;
}
export const refGetTileLabels = (refPos) => refTypeIncludesFlag(refPos, ReferenceType.Tile) && refPos.properties
    ? refPos.properties[reservedTileLabelsKey]
    : undefined;
export const refGetRangeLabels = (refPos) => 
// eslint-disable-next-line no-bitwise
refTypeIncludesFlag(refPos, ReferenceType.NestBegin | ReferenceType.NestEnd) &&
    refPos.properties
    ? refPos.properties[reservedRangeLabelsKey]
    : undefined;
export function refHasTileLabel(refPos, label) {
    const tileLabels = refGetTileLabels(refPos);
    return tileLabels?.includes(label) ?? false;
}
export function refHasRangeLabel(refPos, label) {
    const rangeLabels = refGetRangeLabels(refPos);
    return rangeLabels?.includes(label) ?? false;
}
export function refHasTileLabels(refPos) {
    return refGetTileLabels(refPos) !== undefined;
}
export function refHasRangeLabels(refPos) {
    return refGetRangeLabels(refPos) !== undefined;
}
export const DetachedReferencePosition = -1;
export function minReferencePosition(a, b) {
    return compareReferencePositions(a, b) < 0 ? a : b;
}
export function maxReferencePosition(a, b) {
    return compareReferencePositions(a, b) > 0 ? a : b;
}
export function compareReferencePositions(a, b) {
    const aSeg = a.getSegment();
    const bSeg = b.getSegment();
    if (aSeg === bSeg) {
        return a.getOffset() - b.getOffset();
    }
    else {
        return aSeg === undefined || (bSeg !== undefined && aSeg.ordinal < bSeg.ordinal) ? -1 : 1;
    }
}
//# sourceMappingURL=referencePositions.js.map