"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditManagerSummarizer = exports.EditManagerSummaryFormatVersion = exports.stringKey = void 0;
const client_utils_1 = require("@fluid-internal/client-utils");
const internal_1 = require("@fluidframework/core-utils/internal");
const versionedSummarizer_js_1 = require("./versionedSummarizer.js");
exports.stringKey = "String";
/**
 * The versions for the edit manager summary format.
 */
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 || (exports.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`
 */
class EditManagerSummarizer extends versionedSummarizer_js_1.VersionedSummarizer {
    constructor(editManager, codec, idCompressor, minVersionForCollab, schemaAndPolicy, 
    /** See {@link EditManagerEncodingContext.healUnresolvableIdentifiersOnDecode}. */
    healUnresolvableIdentifiersOnDecode, 
    /** See {@link EditManagerEncodingContext.sharedObjectId}. */
    sharedObjectId) {
        super(EditManagerSummarizer.key, minVersionToEditManagerSummaryFormatVersion(minVersionForCollab), supportedVersions, true /* supportPreVersioningFormat */);
        this.editManager = editManager;
        this.codec = codec;
        this.idCompressor = idCompressor;
        this.schemaAndPolicy = schemaAndPolicy;
        this.healUnresolvableIdentifiersOnDecode = healUnresolvableIdentifiersOnDecode;
        this.sharedObjectId = sharedObjectId;
    }
    summarizeInternal(props) {
        const { stringify, builder } = props;
        const context = {
            idCompressor: this.idCompressor,
            schema: this.schemaAndPolicy,
            isSummary: true,
            healUnresolvableIdentifiersOnDecode: this.healUnresolvableIdentifiersOnDecode,
            sharedObjectId: this.sharedObjectId,
        };
        const jsonCompatible = this.codec.encode(this.editManager.getSummaryData(), context);
        const dataString = stringify(jsonCompatible);
        builder.addBlob(exports.stringKey, dataString);
    }
    async loadInternal(services, parse) {
        const schemaBuffer = await services.readBlob(exports.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, "utf8"));
        const data = this.codec.decode(summary, {
            idCompressor: this.idCompressor,
            isSummary: true,
            healUnresolvableIdentifiersOnDecode: this.healUnresolvableIdentifiersOnDecode,
            sharedObjectId: this.sharedObjectId,
        });
        this.editManager.loadSummaryData(data);
    }
}
exports.EditManagerSummarizer = EditManagerSummarizer;
EditManagerSummarizer.key = "EditManager";
//# sourceMappingURL=editManagerSummarizer.js.map