"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeForestSummarizerCodec = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const util_1 = require("../util");
const forestSummarizerFormat_1 = require("./forestSummarizerFormat");
// TODO: As of now this codec does not contain that much logic on encoding, and mainly provides a codec which version stamps,
// and validates the format of the data. Eventually we will want to add more logic in here.
function makeForestSummarizerCodec({ jsonValidator: validator, }) {
    const versionedValidator = validator.compile(forestSummarizerFormat_1.Versioned);
    const formatValidator = validator.compile(forestSummarizerFormat_1.Format);
    return {
        encode: (data) => {
            const encoded = {
                version: forestSummarizerFormat_1.version,
                data,
            };
            (0, core_utils_1.assert)(versionedValidator.check(encoded), 0x837 /* Encoded data should be versioned */);
            (0, core_utils_1.assert)(formatValidator.check(encoded), 0x838 /* Encoded schema should validate */);
            return encoded;
        },
        decode: (data) => {
            if (!versionedValidator.check(data)) {
                (0, util_1.fail)("invalid serialized data: did not have a version");
            }
            // When more versions exist, we can switch on the version here.
            if (data.version !== forestSummarizerFormat_1.version) {
                (0, util_1.fail)("Unexpected version for serialized data");
            }
            if (!formatValidator.check(data)) {
                (0, util_1.fail)("Serialized data failed validation");
            }
            return data.data;
        },
    };
}
exports.makeForestSummarizerCodec = makeForestSummarizerCodec;
//# sourceMappingURL=forestSummarizerCodec.js.map