/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { makeCodecFamily, makeVersionDispatchingCodec, withSchemaValidation, } from "../../codec/index.js";
import { makeSchemaCodec } from "../schema-index/index.js";
import { EncodedSchemaChange } from "./schemaChangeFormat.js";
import { SchemaVersion } from "../../core/index.js";
/**
 * Create a family of schema change codecs.
 * @param options - Specifies common codec options, including which `validator` to use.
 * @returns The composed codec family.
 */
export function makeSchemaChangeCodecs(options) {
    return makeCodecFamily([
        [SchemaVersion.v1, makeSchemaChangeCodecV1(options, SchemaVersion.v1)],
        [SchemaVersion.v2, makeSchemaChangeCodecV1(options, SchemaVersion.v2)],
    ]);
}
/**
 * Create a schema change codec.
 * @param options - Specifies common codec options, including which `validator` to use.
 * @param writeVersion - The schema change write version.
 * @returns The composed codec.
 */
export function makeSchemaChangeCodec(options, writeVersion) {
    const family = makeSchemaChangeCodecs(options);
    return makeVersionDispatchingCodec(family, { ...options, writeVersion });
}
/**
 * Compose the change codec using mostly v1 logic.
 * @param options - The codec options.
 * @param schemaWriteVersion - The schema write version.
 * @returns The composed schema change codec.
 */
function makeSchemaChangeCodecV1(options, schemaWriteVersion) {
    const schemaCodec = makeSchemaCodec(options, schemaWriteVersion);
    const schemaChangeCodec = {
        encode: (schemaChange) => {
            assert(!schemaChange.isInverse, 0x933 /* Inverse schema changes should never be transmitted */);
            return {
                new: schemaCodec.encode(schemaChange.schema.new),
                old: schemaCodec.encode(schemaChange.schema.old),
            };
        },
        decode: (encoded) => {
            return {
                schema: {
                    new: schemaCodec.decode(encoded.new),
                    old: schemaCodec.decode(encoded.old),
                },
                isInverse: false,
            };
        },
        encodedSchema: EncodedSchemaChange,
    };
    return withSchemaValidation(EncodedSchemaChange, schemaChangeCodec, options.jsonValidator);
}
//# sourceMappingURL=schemaChangeCodecs.js.map