/*!
 * 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 { VersionedSummarizer } from "./versionedSummarizer.js";
export const stringKey = "String";
/**
 * The versions for the edit manager summary format.
 */
export var EditManagerSummaryFormatVersion;
(function (EditManagerSummaryFormatVersion) {
    /**
     * This version represents summary format before summary versioning was introduced.
     */
    EditManagerSummaryFormatVersion[EditManagerSummaryFormatVersion["v1"] = 1] = "v1";
    /**
     * This version adds metadata to the summary. This is backward compatible with version 1.
     */
    EditManagerSummaryFormatVersion[EditManagerSummaryFormatVersion["v2"] = 2] = "v2";
    /**
     * The latest version of the summary. Must be updated when a new version is added.
     */
    EditManagerSummaryFormatVersion[EditManagerSummaryFormatVersion["vLatest"] = 2] = "vLatest";
})(EditManagerSummaryFormatVersion || (EditManagerSummaryFormatVersion = {}));
const supportedVersions = new Set([
    1 /* EditManagerSummaryFormatVersion.v1 */,
    2 /* EditManagerSummaryFormatVersion.v2 */,
]);
/**
 * Returns the summary version to use as per the given minimum version for collab.
 */
function minVersionToEditManagerSummaryFormatVersion(version) {
    // Currently, version 2 is written which adds metadata blob to the summary.
    return 2 /* EditManagerSummaryFormatVersion.v2 */;
}
/**
 * Provides methods for summarizing and loading an `EditManager`
 */
export class EditManagerSummarizer extends VersionedSummarizer {
    constructor(editManager, codec, idCompressor, minVersionForCollab, schemaAndPolicy) {
        super(EditManagerSummarizer.key, minVersionToEditManagerSummaryFormatVersion(minVersionForCollab), supportedVersions, true /* supportPreVersioningFormat */);
        this.editManager = editManager;
        this.codec = codec;
        this.idCompressor = idCompressor;
        this.schemaAndPolicy = schemaAndPolicy;
    }
    summarizeInternal(props) {
        const { stringify, builder } = props;
        const context = this.schemaAndPolicy === undefined
            ? { idCompressor: this.idCompressor }
            : { schema: this.schemaAndPolicy, idCompressor: this.idCompressor };
        const jsonCompatible = this.codec.encode(this.editManager.getSummaryData(), context);
        const dataString = stringify(jsonCompatible);
        builder.addBlob(stringKey, dataString);
    }
    async loadInternal(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, "utf8"));
        const data = this.codec.decode(summary, { idCompressor: this.idCompressor });
        this.editManager.loadSummaryData(data);
    }
}
EditManagerSummarizer.key = "EditManager";
//# sourceMappingURL=editManagerSummarizer.js.map