"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeModularChangeCodecFamily = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const util_1 = require("../../util");
const codec_1 = require("../../codec");
const genericFieldKind_1 = require("./genericFieldKind");
const modularChangeFormat_1 = require("./modularChangeFormat");
function makeV0Codec(fieldKinds, { jsonValidator: validator }) {
    const nodeChangesetCodec = {
        encode: encodeNodeChangesForJson,
        decode: decodeNodeChangesetFromJson,
        encodedSchema: modularChangeFormat_1.EncodedNodeChangeset,
    };
    const getMapEntry = (field) => {
        const codec = field.changeHandler.codecsFactory(nodeChangesetCodec).resolve(0);
        return {
            codec,
            compiledSchema: codec.json.encodedSchema
                ? validator.compile(codec.json.encodedSchema)
                : undefined,
        };
    };
    const fieldChangesetCodecs = new Map([[genericFieldKind_1.genericFieldKind.identifier, getMapEntry(genericFieldKind_1.genericFieldKind)]]);
    fieldKinds.forEach((fieldKind, identifier) => {
        fieldChangesetCodecs.set(identifier, getMapEntry(fieldKind));
    });
    const getFieldChangesetCodec = (fieldKind) => {
        const entry = fieldChangesetCodecs.get(fieldKind);
        (0, common_utils_1.assert)(entry !== undefined, 0x5ea /* Tried to encode unsupported fieldKind */);
        return entry;
    };
    function encodeFieldChangesForJson(change) {
        const encodedFields = [];
        for (const [field, fieldChange] of change) {
            const { codec, compiledSchema } = getFieldChangesetCodec(fieldChange.fieldKind);
            const encodedChange = codec.json.encode(fieldChange.change);
            if (compiledSchema !== undefined && !compiledSchema.check(encodedChange)) {
                (0, util_1.fail)("Encoded change didn't pass schema validation.");
            }
            const fieldKey = field;
            const encodedField = {
                fieldKey,
                fieldKind: fieldChange.fieldKind,
                change: encodedChange,
            };
            encodedFields.push(encodedField);
        }
        return encodedFields;
    }
    function encodeNodeChangesForJson(change) {
        const encodedChange = {};
        const { fieldChanges, nodeExistsConstraint } = change;
        if (fieldChanges !== undefined) {
            encodedChange.fieldChanges = encodeFieldChangesForJson(fieldChanges);
        }
        if (nodeExistsConstraint !== undefined) {
            encodedChange.nodeExistsConstraint = nodeExistsConstraint;
        }
        return encodedChange;
    }
    function decodeFieldChangesFromJson(encodedChange) {
        const decodedFields = new Map();
        for (const field of encodedChange) {
            const { codec, compiledSchema } = getFieldChangesetCodec(field.fieldKind);
            if (compiledSchema !== undefined && !compiledSchema.check(field.change)) {
                (0, util_1.fail)("Encoded change didn't pass schema validation.");
            }
            const fieldChangeset = codec.json.decode(field.change);
            const fieldKey = (0, util_1.brand)(field.fieldKey);
            decodedFields.set(fieldKey, {
                fieldKind: field.fieldKind,
                change: (0, util_1.brand)(fieldChangeset),
            });
        }
        return decodedFields;
    }
    function decodeNodeChangesetFromJson(encodedChange) {
        const decodedChange = {};
        const { fieldChanges, nodeExistsConstraint } = encodedChange;
        if (fieldChanges !== undefined) {
            decodedChange.fieldChanges = decodeFieldChangesFromJson(fieldChanges);
        }
        if (nodeExistsConstraint !== undefined) {
            decodedChange.nodeExistsConstraint = nodeExistsConstraint;
        }
        return decodedChange;
    }
    return {
        encode: (change) => {
            return {
                maxId: change.maxId,
                revisions: change.revisions,
                changes: encodeFieldChangesForJson(change.fieldChanges),
            };
        },
        decode: (change) => {
            const encodedChange = change;
            const decoded = {
                fieldChanges: decodeFieldChangesFromJson(encodedChange.changes),
            };
            if (encodedChange.revisions !== undefined) {
                decoded.revisions = encodedChange.revisions;
            }
            if (encodedChange.maxId !== undefined) {
                decoded.maxId = encodedChange.maxId;
            }
            return decoded;
        },
        encodedSchema: modularChangeFormat_1.EncodedModularChangeset,
    };
}
function makeModularChangeCodecFamily(fieldKinds, options) {
    return (0, codec_1.makeCodecFamily)([[0, makeV0Codec(fieldKinds, options)]]);
}
exports.makeModularChangeCodecFamily = makeModularChangeCodecFamily;
//# sourceMappingURL=modularChangeCodecs.js.map