"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedEditManager = exports.minSequenceId = exports.equalSequenceIds = exports.sequenceIdComparator = void 0;
const typebox_1 = require("@sinclair/typebox");
const util_1 = require("../util");
const core_1 = require("../core");
const noAdditionalProps = { additionalProperties: false };
const CommitBase = (tChange) => typebox_1.Type.Object({
    revision: core_1.RevisionTagSchema,
    change: tChange,
    sessionId: core_1.SessionIdSchema,
});
/**
 * @privateRemarks Commits are generally encoded from `GraphCommit`s, which often contain extra data.
 * This `noAdditionalProps` is especially important in that light.
 */
const Commit = (tChange) => typebox_1.Type.Composite([CommitBase(tChange)], noAdditionalProps);
const SeqNumber = (0, util_1.brandedNumberType)();
const sequenceIdComparator = (a, b) => a.sequenceNumber !== b.sequenceNumber
    ? a.sequenceNumber - b.sequenceNumber
    : (a.indexInBatch ?? 0) - (b.indexInBatch ?? 0);
exports.sequenceIdComparator = sequenceIdComparator;
const equalSequenceIds = (a, b) => (0, exports.sequenceIdComparator)(a, b) === 0;
exports.equalSequenceIds = equalSequenceIds;
const minSequenceId = (a, b) => (0, exports.sequenceIdComparator)(a, b) < 0 ? a : b;
exports.minSequenceId = minSequenceId;
const SequencedCommit = (tChange) => typebox_1.Type.Composite([
    CommitBase(tChange),
    typebox_1.Type.Object({
        sequenceNumber: SeqNumber,
        indexInBatch: typebox_1.Type.Optional(typebox_1.Type.Number()),
    }),
], noAdditionalProps);
const SummarySessionBranch = (tChange) => typebox_1.Type.Object({
    base: core_1.RevisionTagSchema,
    commits: typebox_1.Type.Array(Commit(tChange)),
}, noAdditionalProps);
const EncodedEditManager = (tChange) => typebox_1.Type.Object({
    trunk: typebox_1.Type.Array(SequencedCommit(tChange)),
    branches: typebox_1.Type.Array(typebox_1.Type.Tuple([core_1.SessionIdSchema, SummarySessionBranch(tChange)])),
}, noAdditionalProps);
exports.EncodedEditManager = EncodedEditManager;
//# sourceMappingURL=editManagerFormat.js.map