"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSchemaChangeCodec = exports.makeSchemaChangeCodecs = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../schema-index/index.js");
const schemaChangeFormat_js_1 = require("./schemaChangeFormat.js");
const index_js_3 = require("../../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.
 */
function makeSchemaChangeCodecs(options) {
    return (0, index_js_1.makeCodecFamily)([
        [index_js_3.SchemaVersion.v1, makeSchemaChangeCodecV1(options, index_js_3.SchemaVersion.v1)],
        [index_js_3.SchemaVersion.v2, makeSchemaChangeCodecV1(options, index_js_3.SchemaVersion.v2)],
    ]);
}
exports.makeSchemaChangeCodecs = makeSchemaChangeCodecs;
/**
 * 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.
 */
function makeSchemaChangeCodec(options, writeVersion) {
    const family = makeSchemaChangeCodecs(options);
    return (0, index_js_1.makeVersionDispatchingCodec)(family, { ...options, writeVersion });
}
exports.makeSchemaChangeCodec = makeSchemaChangeCodec;
/**
 * 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 = (0, index_js_2.makeSchemaCodec)(options, schemaWriteVersion);
    const schemaChangeCodec = {
        encode: (schemaChange) => {
            (0, internal_1.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: schemaChangeFormat_js_1.EncodedSchemaChange,
    };
    return (0, index_js_1.withSchemaValidation)(schemaChangeFormat_js_1.EncodedSchemaChange, schemaChangeCodec, options.jsonValidator);
}
//# sourceMappingURL=schemaChangeCodecs.js.map