"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodecTreeForForestFormat = exports.makeForestSummarizerCodec = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../codec/index.js");
const format_js_1 = require("./format.js");
const index_js_2 = require("../../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 (0, index_js_2.brand)(format_js_1.ForestFormatVersion.v1);
}
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 (0, index_js_1.makeVersionedValidatedCodec)(options, new Set([format_js_1.ForestFormatVersion.v1]), format_js_1.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);
            (0, internal_1.assert)(data.keys.length === fields.length, 0x891 /* mismatched lengths */);
            for (const [index, field] of fields.entries()) {
                out.set(data.keys[index] ?? (0, internal_1.oob)(), field);
            }
            return out;
        },
    });
}
exports.makeForestSummarizerCodec = makeForestSummarizerCodec;
function getCodecTreeForForestFormat(clientVersion) {
    return { name: "Forest", version: clientVersionToForestSummaryFormatVersion(clientVersion) };
}
exports.getCodecTreeForForestFormat = getCodecTreeForForestFormat;
//# sourceMappingURL=codec.js.map