/*!
 * 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);
    if (tileLabels) {
        for (const refLabel of tileLabels) {
            if (label === refLabel) {
                return true;
            }
        }
    }
    return false;
}
export function refHasRangeLabel(refPos, label) {
    const rangeLabels = refGetRangeLabels(refPos);
    if (rangeLabels) {
        for (const refLabel of rangeLabels) {
            if (label === refLabel) {
                return true;
            }
        }
    }
    return 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) {
    if (compareReferencePositions(a, b) < 0) {
        return a;
    }
    else {
        return b;
    }
}
export function maxReferencePosition(a, b) {
    if (compareReferencePositions(a, b) > 0) {
        return a;
    }
    else {
        return b;
    }
}
export function compareReferencePositions(a, b) {
    const aSeg = a.getSegment();
    const bSeg = b.getSegment();
    if (aSeg === bSeg) {
        return a.getOffset() - b.getOffset();
    }
    else {
        if (aSeg === undefined
            || (bSeg !== undefined &&
                aSeg.ordinal < bSeg.ordinal)) {
            return -1;
        }
        else {
            return 1;
        }
    }
}
//# sourceMappingURL=referencePositions.js.map