"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSharedTreeChangeCodecFamily = exports.makeSharedTreeChangeCodec = void 0;
const index_js_1 = require("../codec/index.js");
const index_js_2 = require("../feature-libraries/index.js");
const sharedTreeChangeFormat_js_1 = require("./sharedTreeChangeFormat.js");
function makeSharedTreeChangeCodec(modularChangeCodec, codecOptions) {
    const schemaChangeCodec = (0, index_js_2.makeSchemaChangeCodec)(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 {
        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_2.defaultSchemaPolicy,
                            schema: updatedSchema,
                        }
                        : context.schema;
                    changes.push({
                        data: modularChangeCodec.encode(decodedChange.innerChange, {
                            originatorId: context.originatorId,
                            schema: schemaAndPolicy,
                        }),
                    });
                }
                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 };
        },
        encodedSchema: sharedTreeChangeFormat_js_1.EncodedSharedTreeChange,
    };
}
exports.makeSharedTreeChangeCodec = makeSharedTreeChangeCodec;
function makeSharedTreeChangeCodecFamily(modularChangeCodec, options) {
    return (0, index_js_1.makeCodecFamily)([[0, makeSharedTreeChangeCodec(modularChangeCodec, options)]]);
}
exports.makeSharedTreeChangeCodecFamily = makeSharedTreeChangeCodecFamily;
//# sourceMappingURL=sharedTreeChangeCodecs.js.map