"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodecTreeForChangeFormat = exports.dependenciesForChangeFormat = exports.makeSharedTreeChangeCodecFamily = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../codec/index.js");
const index_js_2 = require("../core/index.js");
const index_js_3 = require("../feature-libraries/index.js");
const index_js_4 = require("../util/index.js");
const sharedTreeChangeFormat_js_1 = require("./sharedTreeChangeFormat.js");
function makeSharedTreeChangeCodecFamily(modularChangeCodecFamily, options) {
    const schemaChangeCodecs = (0, index_js_3.makeSchemaChangeCodecs)(options);
    const versions = Array.from(exports.dependenciesForChangeFormat.entries()).map(([format, { modularChange, schemaChange }]) => [
        format,
        makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(modularChange).json, schemaChangeCodecs.resolve(schemaChange).json, options),
    ]);
    return (0, index_js_1.makeCodecFamily)(versions);
}
exports.makeSharedTreeChangeCodecFamily = makeSharedTreeChangeCodecFamily;
/**
 * Defines for each SharedTree change format the corresponding dependent formats to use.
 * This is an arbitrary mapping that is injected in the SharedTree change codec.
 * Once an entry is defined and used in production, it cannot be changed.
 * This is because the format for the dependent formats are not explicitly versioned.
 */
exports.dependenciesForChangeFormat = new Map([
    [(0, index_js_4.brand)(3), { modularChange: (0, index_js_4.brand)(3), schemaChange: (0, index_js_4.brand)(index_js_2.SchemaFormatVersion.v1) }],
    [(0, index_js_4.brand)(4), { modularChange: (0, index_js_4.brand)(4), schemaChange: (0, index_js_4.brand)(index_js_2.SchemaFormatVersion.v1) }],
]);
function getCodecTreeForChangeFormat(version, clientVersion) {
    const { modularChange, schemaChange } = exports.dependenciesForChangeFormat.get(version) ?? (0, internal_1.fail)(0xc78 /* Unknown change format */);
    return {
        name: "SharedTreeChange",
        version,
        children: [
            (0, index_js_3.getCodecTreeForModularChangeFormat)(modularChange),
            (0, index_js_3.getCodecTreeForSchemaChangeFormat)(schemaChange, clientVersion),
        ],
    };
}
exports.getCodecTreeForChangeFormat = getCodecTreeForChangeFormat;
function makeSharedTreeChangeCodec(modularChangeCodec, schemaChangeCodec, codecOptions) {
    const decoderLibrary = new index_js_1.DiscriminatedUnionDispatcher({
        data(encoded, context) {
            return {
                type: "data",
                innerChange: modularChangeCodec.decode(encoded, context),
            };
        },
        schema(encoded) {
            return {
                type: "schema",
                innerChange: schemaChangeCodec.decode(encoded),
            };
        },
    });
    return (0, index_js_1.withSchemaValidation)(sharedTreeChangeFormat_js_1.EncodedSharedTreeChange, {
        encode: (change, context) => {
            const changes = [];
            let updatedSchema;
            for (const decodedChange of change.changes) {
                if (decodedChange.type === "data") {
                    const schemaAndPolicy = updatedSchema !== undefined
                        ? {
                            policy: context.schema !== undefined
                                ? context.schema.policy
                                : index_js_3.defaultSchemaPolicy,
                            schema: updatedSchema,
                        }
                        : context.schema;
                    changes.push({
                        data: modularChangeCodec.encode(decodedChange.innerChange, {
                            originatorId: context.originatorId,
                            schema: schemaAndPolicy,
                            idCompressor: context.idCompressor,
                            revision: context.revision,
                        }),
                    });
                }
                else if (decodedChange.type === "schema") {
                    changes.push({
                        schema: schemaChangeCodec.encode(decodedChange.innerChange),
                    });
                    updatedSchema = decodedChange.innerChange.schema.new;
                }
            }
            return changes;
        },
        decode: (encodedChange, context) => {
            const changes = [];
            for (const subChange of encodedChange) {
                changes.push(decoderLibrary.dispatch(subChange, context));
            }
            return { changes };
        },
    }, codecOptions.jsonValidator);
}
//# sourceMappingURL=sharedTreeChangeCodecs.js.map