"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 core_utils_1 = require("@fluidframework/core-utils");
const shared_object_base_1 = require("@fluidframework/shared-object-base");
const editManagerCodecs_1 = require("./editManagerCodecs");
const stringKey = "String";
const formatVersion = 0;
/**
 * Provides methods for summarizing and loading an `EditManager`
 */
class EditManagerSummarizer {
    constructor(editManager, options) {
        this.editManager = editManager;
        this.key = "EditManager";
        const changesetCodec = this.editManager.changeFamily.codecs.resolve(formatVersion);
        this.codec = (0, editManagerCodecs_1.makeEditManagerCodec)(changesetCodec, options);
    }
    getAttachSummary(stringify, fullTree, trackState, telemetryContext) {
        return this.summarizeCore(stringify);
    }
    async summarize(stringify, fullTree, trackState, telemetryContext) {
        return this.summarizeCore(stringify);
    }
    summarizeCore(stringify) {
        const jsonCompatible = this.codec.encode(this.editManager.getSummaryData());
        const dataString = stringify(jsonCompatible);
        return (0, shared_object_base_1.createSingleBlobSummary)(stringKey, dataString);
    }
    getGCData(fullGC) {
        // TODO: Properly implement garbage collection. Right now, garbage collection is performed automatically
        // by the code in SharedObject (from which SharedTreeCore extends). The `runtime.uploadBlob` API delegates
        // to the `BlobManager`, which automatically populates the summary with ISummaryAttachment entries for each
        // blob.
        return {
            gcNodes: {},
        };
    }
    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, core_utils_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);
        this.editManager.loadSummaryData(data);
    }
}
exports.EditManagerSummarizer = EditManagerSummarizer;
//# sourceMappingURL=editManagerSummarizer.js.map