"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DetachedFieldIndexSummarizer = exports.DetachedFieldIndexSummaryFormatVersion = exports.detachedFieldIndexBlobKey = void 0;
const client_utils_1 = require("@fluid-internal/client-utils");
const index_js_1 = require("../shared-tree-core/index.js");
/**
 * The storage key for the blob in the summary containing schema data
 */
exports.detachedFieldIndexBlobKey = "DetachedFieldIndexBlob";
/**
 * The versions for the detached field index summary format.
 */
var DetachedFieldIndexSummaryFormatVersion;
(function (DetachedFieldIndexSummaryFormatVersion) {
    /**
     * This version represents summary format before summary versioning was introduced.
     */
    DetachedFieldIndexSummaryFormatVersion[DetachedFieldIndexSummaryFormatVersion["v1"] = 1] = "v1";
    /**
     * This version adds metadata to the summary. This is backward compatible with version 1.
     */
    DetachedFieldIndexSummaryFormatVersion[DetachedFieldIndexSummaryFormatVersion["v2"] = 2] = "v2";
    /**
     * The latest version of the summary. Must be updated when a new version is added.
     */
    DetachedFieldIndexSummaryFormatVersion[DetachedFieldIndexSummaryFormatVersion["vLatest"] = 2] = "vLatest";
})(DetachedFieldIndexSummaryFormatVersion || (exports.DetachedFieldIndexSummaryFormatVersion = DetachedFieldIndexSummaryFormatVersion = {}));
const supportedVersions = new Set([
    1 /* DetachedFieldIndexSummaryFormatVersion.v1 */,
    2 /* DetachedFieldIndexSummaryFormatVersion.v2 */,
]);
/**
 * Returns the summary version to use as per the given minimum version for collab.
 */
function minVersionToDetachedFieldIndexSummaryFormatVersion(version) {
    // Currently, version 2 is written which adds metadata blob to the summary.
    return 2 /* DetachedFieldIndexSummaryFormatVersion.v2 */;
}
/**
 * Provides methods for summarizing and loading a tree index.
 */
class DetachedFieldIndexSummarizer extends index_js_1.VersionedSummarizer {
    constructor(detachedFieldIndex, minVersionForCollab) {
        super("DetachedFieldIndex", minVersionToDetachedFieldIndexSummaryFormatVersion(minVersionForCollab), supportedVersions, true);
        this.detachedFieldIndex = detachedFieldIndex;
    }
    summarizeInternal(props) {
        const { stringify, builder } = props;
        const data = this.detachedFieldIndex.encode();
        builder.addBlob(exports.detachedFieldIndexBlobKey, stringify(data));
    }
    async loadInternal(services, parse) {
        if (await services.contains(exports.detachedFieldIndexBlobKey)) {
            const detachedFieldIndexBuffer = await services.readBlob(exports.detachedFieldIndexBlobKey);
            const treeBufferString = (0, client_utils_1.bufferToString)(detachedFieldIndexBuffer, "utf8");
            const parsed = parse(treeBufferString);
            this.detachedFieldIndex.loadData(parsed);
        }
    }
}
exports.DetachedFieldIndexSummarizer = DetachedFieldIndexSummarizer;
//# sourceMappingURL=detachedFieldIndexSummarizer.js.map