/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { UnassignedSequenceNumber } from "./constants.js";
export function lessThan(a, b) {
    if (a.seq === UnassignedSequenceNumber) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return b.seq === UnassignedSequenceNumber && a.localSeq < b.localSeq;
    }
    if (b.seq === UnassignedSequenceNumber) {
        return true;
    }
    return a.seq < b.seq;
}
export function gte(a, b) {
    return !lessThan(a, b);
}
export function greaterThan(a, b) {
    if (a.seq === UnassignedSequenceNumber) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return b.seq !== UnassignedSequenceNumber || a.localSeq > b.localSeq;
    }
    if (b.seq === UnassignedSequenceNumber) {
        return false;
    }
    return a.seq > b.seq;
}
export function lte(a, b) {
    return !greaterThan(a, b);
}
export function equal(a, b) {
    return a.seq === b.seq && a.clientId === b.clientId && a.localSeq === b.localSeq;
}
export function isLocal(a) {
    return a.seq === UnassignedSequenceNumber;
}
export function isAcked(a) {
    return a.seq !== UnassignedSequenceNumber;
}
/**
 * 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.
 */
export 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);
    }
}
export function hasAnyAckedOperation(list) {
    return list.some((ts) => isAcked(ts));
}
export function compare(a, b) {
    if (greaterThan(a, b)) {
        return 1;
    }
    else if (lessThan(a, b)) {
        return -1;
    }
    else {
        return 0;
    }
}
//# sourceMappingURL=stamps.js.map