/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, oob } from "@fluidframework/core-utils/internal";
import { makeVersionedValidatedCodec, } from "../../codec/index.js";
import { FormatV1, ForestFormatVersion } from "./format.js";
import { brand } from "../../util/index.js";
/**
 * Convert a MinimumVersionForCollab to a ForestFormatVersion.
 * @param clientVersion - The MinimumVersionForCollab to convert.
 * @returns The ForestFormatVersion that corresponds to the provided MinimumVersionForCollab.
 */
function clientVersionToForestSummaryFormatVersion(clientVersion) {
    // Currently, forest summary codec only writes in version 1.
    return brand(ForestFormatVersion.v1);
}
export function makeForestSummarizerCodec(options, fieldBatchCodec) {
    const inner = fieldBatchCodec;
    // TODO: AB#41865
    // This needs to be updated to support multiple versions.
    // The second version will be used to enable incremental summarization.
    const writeVersion = clientVersionToForestSummaryFormatVersion(options.minVersionForCollab);
    return makeVersionedValidatedCodec(options, new Set([ForestFormatVersion.v1]), FormatV1, {
        encode: (data, context) => {
            const keys = [];
            const fields = [];
            for (const [key, value] of data) {
                keys.push(key);
                fields.push(value);
            }
            return { keys, fields: inner.encode(fields, context), version: writeVersion };
        },
        decode: (data, context) => {
            const out = new Map();
            const fields = inner.decode(data.fields, context);
            assert(data.keys.length === fields.length, 0x891 /* mismatched lengths */);
            for (const [index, field] of fields.entries()) {
                out.set(data.keys[index] ?? oob(), field);
            }
            return out;
        },
    });
}
export function getCodecTreeForForestFormat(clientVersion) {
    return { name: "Forest", version: clientVersionToForestSummaryFormatVersion(clientVersion) };
}
//# sourceMappingURL=codec.js.map