/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { bufferToString } from "@fluid-internal/client-utils";
import { assert } from "@fluidframework/core-utils/internal";
import { createSingleBlobSummary } from "@fluidframework/shared-object-base/internal";
const stringKey = "String";
/**
 * Provides methods for summarizing and loading an `EditManager`
 */
export class EditManagerSummarizer {
    constructor(editManager, codec, schemaAndPolicy) {
        this.editManager = editManager;
        this.codec = codec;
        this.schemaAndPolicy = schemaAndPolicy;
        this.key = "EditManager";
    }
    getAttachSummary(stringify, fullTree, trackState, telemetryContext) {
        return this.summarizeCore(stringify);
    }
    async summarize(stringify, fullTree, trackState, telemetryContext) {
        return this.summarizeCore(stringify);
    }
    summarizeCore(stringify) {
        const context = this.schemaAndPolicy !== undefined ? { schema: this.schemaAndPolicy } : {};
        const jsonCompatible = this.codec.encode(this.editManager.getSummaryData(), context);
        const dataString = stringify(jsonCompatible);
        return 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.
        assert(this.editManager.isEmpty(), 0x42c /* There should not already be stored EditManager data when loading from summary */);
        const summary = parse(bufferToString(schemaBuffer, "utf-8"));
        const data = this.codec.decode(summary, {});
        this.editManager.loadSummaryData(data);
    }
}
//# sourceMappingURL=editManagerSummarizer.js.map