"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSharedTreeChangeCodecFamily = 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 makeSharedTreeChangeCodecFamily(modularChangeCodecFamily, options) {
    const schemaChangeCodecs = (0, index_js_2.makeSchemaChangeCodecs)(options);
    return (0, index_js_1.makeCodecFamily)([
        [
            1,
            makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(1).json, schemaChangeCodecs.resolve(1).json, options),
        ],
        [
            2,
            makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(2).json, schemaChangeCodecs.resolve(1).json, options),
        ],
    ]);
}
exports.makeSharedTreeChangeCodecFamily = makeSharedTreeChangeCodecFamily;
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_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 };
        },
    }, codecOptions.jsonValidator);
}
//# sourceMappingURL=sharedTreeChangeCodecs.js.map