/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/core-utils/internal";
import { getConfigForMinVersionForCollab, lowestMinVersionForCollab, } from "@fluidframework/runtime-utils/internal";
import { FluidClientVersion, makeCodecFamily, makeDiscontinuedCodecVersion, } from "../codec/index.js";
import { makeVersionDispatchingCodec } from "../codec/index.js";
import { brand } from "../util/index.js";
import { makeV1CodecWithVersion } from "./editManagerCodecsV1toV4.js";
import { makeSharedBranchesCodecWithVersion } from "./editManagerCodecsVSharedBranches.js";
import { EditManagerFormatVersion, editManagerFormatVersions, } from "./editManagerFormatCommons.js";
/**
 * Convert a MinimumVersionForCollab to an EditManagerFormatVersion.
 * @param clientVersion - The MinimumVersionForCollab to convert.
 * @returns The EditManagerFormatVersion that corresponds to the provided MinimumVersionForCollab.
 */
export function clientVersionToEditManagerFormatVersion(clientVersion, writeVersionOverride) {
    const compatibleVersion = brand(getConfigForMinVersionForCollab(clientVersion, {
        [lowestMinVersionForCollab]: EditManagerFormatVersion.v3,
        [FluidClientVersion.v2_43]: EditManagerFormatVersion.v4,
    }));
    return writeVersionOverride ?? compatibleVersion;
}
/**
 * Returns the version that should be used for testing shared branches.
 */
export function editManagerFormatVersionSelectorForSharedBranches(clientVersion) {
    return brand(EditManagerFormatVersion.vSharedBranches);
}
/**
 * Returns the version that should be used for testing shared branches.
 */
export function editManagerFormatVersionSelectorForDetachedRootEditing(clientVersion) {
    return brand(EditManagerFormatVersion.vDetachedRoots);
}
function editManagerFormatVersionFromOptions(options) {
    const selector = options.editManagerFormatSelector ?? clientVersionToEditManagerFormatVersion;
    return selector(options.minVersionForCollab);
}
export function makeEditManagerCodec(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const family = makeEditManagerCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options);
    const writeVersion = editManagerFormatVersionFromOptions(options);
    return makeVersionDispatchingCodec(family, { ...options, writeVersion });
}
export function makeEditManagerCodecs(changeCodecs, dependentChangeFormatVersion, revisionTagCodec, options) {
    const registry = Array.from(editManagerFormatVersions, (version) => {
        switch (version) {
            case EditManagerFormatVersion.v1:
            case EditManagerFormatVersion.v2:
                return [version, makeDiscontinuedCodecVersion(options, version, "2.73.0")];
            case EditManagerFormatVersion.v3:
            case EditManagerFormatVersion.v4:
            case EditManagerFormatVersion.vDetachedRoots: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version));
                return [
                    version,
                    makeV1CodecWithVersion(changeCodec, revisionTagCodec, options, version),
                ];
            }
            case EditManagerFormatVersion.v5:
                return [version, makeDiscontinuedCodecVersion(options, version, "2.74.0")];
            case EditManagerFormatVersion.vSharedBranches: {
                const changeCodec = changeCodecs.resolve(dependentChangeFormatVersion.lookup(version));
                return [
                    version,
                    makeSharedBranchesCodecWithVersion(changeCodec, revisionTagCodec, options, version),
                ];
            }
            default:
                unreachableCase(version);
        }
    });
    return makeCodecFamily(registry);
}
export function getCodecTreeForEditManagerFormatWithChange(clientVersion, changeFormat) {
    return {
        name: "EditManager",
        version: clientVersionToEditManagerFormatVersion(clientVersion),
        children: [changeFormat],
    };
}
//# sourceMappingURL=editManagerCodecs.js.map