"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodecTreeForMessageFormatWithChange = exports.makeMessageCodecs = exports.makeMessageCodec = exports.messageFormatVersionSelectorForSharedBranches = exports.clientVersionToMessageFormatVersion = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/runtime-utils/internal");
const index_js_1 = require("../codec/index.js");
const index_js_2 = require("../util/index.js");
const messageCodecV1ToV4_js_1 = require("./messageCodecV1ToV4.js");
const messageCodecV5_js_1 = require("./messageCodecV5.js");
const messageFormat_js_1 = require("./messageFormat.js");
/**
 * Convert a MinimumVersionForCollab to a MessageFormatVersion.
 * @param clientVersion - The MinimumVersionForCollab to convert.
 * @returns The MessageFormatVersion that corresponds to the provided MinimumVersionForCollab.
 */
function clientVersionToMessageFormatVersion(clientVersion, writeVersionOverride) {
    const compatibleVersion = (0, index_js_2.brand)((0, internal_2.getConfigForMinVersionForCollab)(clientVersion, {
        [internal_2.lowestMinVersionForCollab]: messageFormat_js_1.MessageFormatVersion.v3,
        [index_js_1.FluidClientVersion.v2_43]: messageFormat_js_1.MessageFormatVersion.v4,
    }));
    return writeVersionOverride ?? compatibleVersion;
}
exports.clientVersionToMessageFormatVersion = clientVersionToMessageFormatVersion;
function messageFormatVersionFromOptions(options) {
    const selector = options.messageFormatSelector ?? clientVersionToMessageFormatVersion;
    return selector(options.minVersionForCollab);
}
/**
 * Returns the version that should be used for testing shared branches.
 */
function messageFormatVersionSelectorForSharedBranches(clientVersion) {
    return (0, index_js_2.brand)(messageFormat_js_1.MessageFormatVersion.v5);
}
exports.messageFormatVersionSelectorForSharedBranches = messageFormatVersionSelectorForSharedBranches;
function makeMessageCodec(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const family = makeMessageCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options);
    const writeVersion = messageFormatVersionFromOptions(options);
    return (0, index_js_1.makeVersionDispatchingCodec)(family, { ...options, writeVersion });
}
exports.makeMessageCodec = makeMessageCodec;
/**
 * @privateRemarks Exported for testing.
 */
function makeMessageCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const registry = Array.from(messageFormat_js_1.messageFormatVersions).map((version) => {
        switch (version) {
            case messageFormat_js_1.MessageFormatVersion.undefined:
            case messageFormat_js_1.MessageFormatVersion.v1:
            case messageFormat_js_1.MessageFormatVersion.v2: {
                const versionOrUndefined = version === messageFormat_js_1.MessageFormatVersion.undefined ? undefined : version;
                return [
                    versionOrUndefined,
                    (0, index_js_1.makeDiscontinuedCodecVersion)(options, versionOrUndefined, "2.73.0"),
                ];
            }
            case messageFormat_js_1.MessageFormatVersion.v3:
            case messageFormat_js_1.MessageFormatVersion.v4: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version)).json;
                return [
                    version,
                    (0, messageCodecV1ToV4_js_1.makeV1ToV4CodecWithVersion)(changeCodec, revisionTagCodec, options, version),
                ];
            }
            case messageFormat_js_1.MessageFormatVersion.v5: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version)).json;
                return [
                    version,
                    (0, messageCodecV5_js_1.makeV5CodecWithVersion)(changeCodec, revisionTagCodec, options, version),
                ];
            }
            default:
                (0, internal_1.unreachableCase)(version);
        }
    });
    return (0, index_js_1.makeCodecFamily)(registry);
}
exports.makeMessageCodecs = makeMessageCodecs;
function getCodecTreeForMessageFormatWithChange(clientVersion, changeFormat) {
    return {
        name: "Message",
        version: clientVersionToMessageFormatVersion(clientVersion),
        children: [changeFormat],
    };
}
exports.getCodecTreeForMessageFormatWithChange = getCodecTreeForMessageFormatWithChange;
//# sourceMappingURL=messageCodecs.js.map