/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { DiscriminatedUnionDispatcher, makeCodecFamily, withSchemaValidation, } from "../codec/index.js";
import { defaultSchemaPolicy, makeSchemaChangeCodecs, } from "../feature-libraries/index.js";
import { EncodedSharedTreeChange, } from "./sharedTreeChangeFormat.js";
export function makeSharedTreeChangeCodecFamily(modularChangeCodecFamily, options) {
    const schemaChangeCodecs = makeSchemaChangeCodecs(options);
    return makeCodecFamily([
        [
            1,
            makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(1).json, schemaChangeCodecs.resolve(1).json, options),
        ],
        [
            2,
            makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(2).json, schemaChangeCodecs.resolve(1).json, options),
        ],
        [
            3,
            makeSharedTreeChangeCodec(modularChangeCodecFamily.resolve(3).json, schemaChangeCodecs.resolve(1).json, options),
        ],
    ]);
}
function makeSharedTreeChangeCodec(modularChangeCodec, schemaChangeCodec, codecOptions) {
    const decoderLibrary = new DiscriminatedUnionDispatcher({
        data(encoded, context) {
            return {
                type: "data",
                innerChange: modularChangeCodec.decode(encoded, context),
            };
        },
        schema(encoded) {
            return {
                type: "schema",
                innerChange: schemaChangeCodec.decode(encoded),
            };
        },
    });
    return withSchemaValidation(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
                                : 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