"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.refHasRangeLabels = exports.refHasTileLabels = exports.refHasRangeLabel = exports.refHasTileLabel = exports.refGetRangeLabels = exports.refGetTileLabels = exports.refTypeIncludesFlag = exports.reservedRangeLabelsKey = exports.reservedTileLabelsKey = void 0;
const ops_1 = require("./ops");
exports.reservedTileLabelsKey = "referenceTileLabels";
exports.reservedRangeLabelsKey = "referenceRangeLabels";
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;
const refGetTileLabels = (refPos) => refTypeIncludesFlag(refPos, ops_1.ReferenceType.Tile)
    && refPos.properties ? refPos.properties[exports.reservedTileLabelsKey] : undefined;
exports.refGetTileLabels = refGetTileLabels;
const refGetRangeLabels = (refPos) => 
// eslint-disable-next-line no-bitwise
(refTypeIncludesFlag(refPos, ops_1.ReferenceType.NestBegin | ops_1.ReferenceType.NestEnd))
    && refPos.properties ? refPos.properties[exports.reservedRangeLabelsKey] : undefined;
exports.refGetRangeLabels = refGetRangeLabels;
function refHasTileLabel(refPos, label) {
    const tileLabels = (0, exports.refGetTileLabels)(refPos);
    if (tileLabels) {
        for (const refLabel of tileLabels) {
            if (label === refLabel) {
                return true;
            }
        }
    }
    return false;
}
exports.refHasTileLabel = refHasTileLabel;
function refHasRangeLabel(refPos, label) {
    const rangeLabels = (0, exports.refGetRangeLabels)(refPos);
    if (rangeLabels) {
        for (const refLabel of rangeLabels) {
            if (label === refLabel) {
                return true;
            }
        }
    }
    return false;
}
exports.refHasRangeLabel = refHasRangeLabel;
function refHasTileLabels(refPos) {
    return (0, exports.refGetTileLabels)(refPos) !== undefined;
}
exports.refHasTileLabels = refHasTileLabels;
function refHasRangeLabels(refPos) {
    return (0, exports.refGetRangeLabels)(refPos) !== undefined;
}
exports.refHasRangeLabels = refHasRangeLabels;
exports.DetachedReferencePosition = -1;
function minReferencePosition(a, b) {
    return compareReferencePositions(a, b) < 0 ? a : b;
}
exports.minReferencePosition = minReferencePosition;
function maxReferencePosition(a, b) {
    return compareReferencePositions(a, b) > 0 ? a : b;
}
exports.maxReferencePosition = maxReferencePosition;
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;
    }
}
exports.compareReferencePositions = compareReferencePositions;
//# sourceMappingURL=referencePositions.js.map