"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodecTreeForEditManagerFormatWithChange = exports.makeEditManagerCodecs = exports.makeEditManagerCodec = exports.editManagerFormatVersionSelectorForDetachedRootEditing = exports.editManagerFormatVersionSelectorForSharedBranches = exports.clientVersionToEditManagerFormatVersion = 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("../codec/index.js");
const index_js_3 = require("../util/index.js");
const editManagerCodecsV1toV4_js_1 = require("./editManagerCodecsV1toV4.js");
const editManagerCodecsVSharedBranches_js_1 = require("./editManagerCodecsVSharedBranches.js");
const editManagerFormatCommons_js_1 = require("./editManagerFormatCommons.js");
/**
 * Convert a MinimumVersionForCollab to an EditManagerFormatVersion.
 * @param clientVersion - The MinimumVersionForCollab to convert.
 * @returns The EditManagerFormatVersion that corresponds to the provided MinimumVersionForCollab.
 */
function clientVersionToEditManagerFormatVersion(clientVersion, writeVersionOverride) {
    const compatibleVersion = (0, index_js_3.brand)((0, internal_2.getConfigForMinVersionForCollab)(clientVersion, {
        [internal_2.lowestMinVersionForCollab]: editManagerFormatCommons_js_1.EditManagerFormatVersion.v3,
        [index_js_1.FluidClientVersion.v2_43]: editManagerFormatCommons_js_1.EditManagerFormatVersion.v4,
    }));
    return writeVersionOverride ?? compatibleVersion;
}
exports.clientVersionToEditManagerFormatVersion = clientVersionToEditManagerFormatVersion;
/**
 * Returns the version that should be used for testing shared branches.
 */
function editManagerFormatVersionSelectorForSharedBranches(clientVersion) {
    return (0, index_js_3.brand)(editManagerFormatCommons_js_1.EditManagerFormatVersion.vSharedBranches);
}
exports.editManagerFormatVersionSelectorForSharedBranches = editManagerFormatVersionSelectorForSharedBranches;
/**
 * Returns the version that should be used for testing shared branches.
 */
function editManagerFormatVersionSelectorForDetachedRootEditing(clientVersion) {
    return (0, index_js_3.brand)(editManagerFormatCommons_js_1.EditManagerFormatVersion.vDetachedRoots);
}
exports.editManagerFormatVersionSelectorForDetachedRootEditing = editManagerFormatVersionSelectorForDetachedRootEditing;
function editManagerFormatVersionFromOptions(options) {
    const selector = options.editManagerFormatSelector ?? clientVersionToEditManagerFormatVersion;
    return selector(options.minVersionForCollab);
}
function makeEditManagerCodec(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const family = makeEditManagerCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options);
    const writeVersion = editManagerFormatVersionFromOptions(options);
    return (0, index_js_2.makeVersionDispatchingCodec)(family, { ...options, writeVersion });
}
exports.makeEditManagerCodec = makeEditManagerCodec;
function makeEditManagerCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const registry = Array.from(editManagerFormatCommons_js_1.editManagerFormatVersions, (version) => {
        switch (version) {
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.v1:
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.v2:
                return [version, (0, index_js_1.makeDiscontinuedCodecVersion)(options, version, "2.73.0")];
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.v3:
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.v4:
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.vDetachedRoots: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version));
                return [
                    version,
                    (0, editManagerCodecsV1toV4_js_1.makeV1CodecWithVersion)(changeCodec, revisionTagCodec, options, version),
                ];
            }
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.v5:
                return [version, (0, index_js_1.makeDiscontinuedCodecVersion)(options, version, "2.74.0")];
            case editManagerFormatCommons_js_1.EditManagerFormatVersion.vSharedBranches: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version));
                return [
                    version,
                    (0, editManagerCodecsVSharedBranches_js_1.makeSharedBranchesCodecWithVersion)(changeCodec, revisionTagCodec, options, version),
                ];
            }
            default:
                (0, internal_1.unreachableCase)(version);
        }
    });
    return (0, index_js_1.makeCodecFamily)(registry);
}
exports.makeEditManagerCodecs = makeEditManagerCodecs;
function getCodecTreeForEditManagerFormatWithChange(clientVersion, changeFormat) {
    return {
        name: "EditManager",
        version: clientVersionToEditManagerFormatVersion(clientVersion),
        children: [changeFormat],
    };
}
exports.getCodecTreeForEditManagerFormatWithChange = getCodecTreeForEditManagerFormatWithChange;
//# sourceMappingURL=editManagerCodecs.js.map