"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.hasAnyAckedOperation = exports.spliceIntoList = exports.isAcked = exports.isLocal = exports.equal = exports.lte = exports.greaterThan = exports.gte = exports.lessThan = void 0;
const constants_js_1 = require("./constants.js");
function lessThan(a, b) {
    if (a.seq === constants_js_1.UnassignedSequenceNumber) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return b.seq === constants_js_1.UnassignedSequenceNumber && a.localSeq < b.localSeq;
    }
    if (b.seq === constants_js_1.UnassignedSequenceNumber) {
        return true;
    }
    return a.seq < b.seq;
}
exports.lessThan = lessThan;
function gte(a, b) {
    return !lessThan(a, b);
}
exports.gte = gte;
function greaterThan(a, b) {
    if (a.seq === constants_js_1.UnassignedSequenceNumber) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return b.seq !== constants_js_1.UnassignedSequenceNumber || a.localSeq > b.localSeq;
    }
    if (b.seq === constants_js_1.UnassignedSequenceNumber) {
        return false;
    }
    return a.seq > b.seq;
}
exports.greaterThan = greaterThan;
function lte(a, b) {
    return !greaterThan(a, b);
}
exports.lte = lte;
function equal(a, b) {
    return a.seq === b.seq && a.clientId === b.clientId && a.localSeq === b.localSeq;
}
exports.equal = equal;
function isLocal(a) {
    return a.seq === constants_js_1.UnassignedSequenceNumber;
}
exports.isLocal = isLocal;
function isAcked(a) {
    return a.seq !== constants_js_1.UnassignedSequenceNumber;
}
exports.isAcked = isAcked;
/**
 * Inserts a stamp into a sorted list of stamps in the correct (sorted) position.
 *
 * Beware that this uses Array.splice, thus requires asymptotics considerations.
 * If inserting a variable number of timestamps, consider just pushing them and sorting the list
 * after using {@link compare} instead.
 */
function spliceIntoList(list, stamp) {
    if (isLocal(stamp) || list.length === 0) {
        list.push(stamp);
    }
    else {
        for (let i = list.length - 1; i >= 0; i--) {
            if (greaterThan(stamp, list[i])) {
                list.splice(i + 1, 0, stamp);
                return;
            }
        }
        // Less than all stamps in the list: put it at the beginning.
        list.unshift(stamp);
    }
}
exports.spliceIntoList = spliceIntoList;
function hasAnyAckedOperation(list) {
    return list.some((ts) => isAcked(ts));
}
exports.hasAnyAckedOperation = hasAnyAckedOperation;
function compare(a, b) {
    if (greaterThan(a, b)) {
        return 1;
    }
    else if (lessThan(a, b)) {
        return -1;
    }
    else {
        return 0;
    }
}
exports.compare = compare;
//# sourceMappingURL=stamps.js.map