"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditManagerSummarizer = void 0;
const client_utils_1 = require("@fluid-internal/client-utils");
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/shared-object-base/internal");
const stringKey = "String";
/**
 * Provides methods for summarizing and loading an `EditManager`
 */
class EditManagerSummarizer {
    constructor(editManager, codec, idCompressor, schemaAndPolicy) {
        this.editManager = editManager;
        this.codec = codec;
        this.idCompressor = idCompressor;
        this.schemaAndPolicy = schemaAndPolicy;
        this.key = "EditManager";
    }
    summarize(props) {
        return this.summarizeCore(props.stringify);
    }
    summarizeCore(stringify) {
        const context = this.schemaAndPolicy !== undefined
            ? { schema: this.schemaAndPolicy, idCompressor: this.idCompressor }
            : { idCompressor: this.idCompressor };
        const jsonCompatible = this.codec.encode(this.editManager.getSummaryData(), context);
        const dataString = stringify(jsonCompatible);
        return (0, internal_2.createSingleBlobSummary)(stringKey, dataString);
    }
    async load(services, parse) {
        const schemaBuffer = await services.readBlob(stringKey);
        // After the awaits, validate that the data is in a clean state.
        // This detects any data that could have been accidentally added through
        // invalid means and is about to be overwritten.
        (0, internal_1.assert)(this.editManager.isEmpty(), 0x42c /* There should not already be stored EditManager data when loading from summary */);
        const summary = parse((0, client_utils_1.bufferToString)(schemaBuffer, "utf-8"));
        const data = this.codec.decode(summary, { idCompressor: this.idCompressor });
        this.editManager.loadSummaryData(data);
    }
}
exports.EditManagerSummarizer = EditManagerSummarizer;
//# sourceMappingURL=editManagerSummarizer.js.map