/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { bufferToString } from "@fluid-internal/client-utils";
import { VersionedSummarizer, } from "../shared-tree-core/index.js";
/**
 * The storage key for the blob in the summary containing schema data
 */
export const detachedFieldIndexBlobKey = "DetachedFieldIndexBlob";
/**
 * The versions for the detached field index summary format.
 */
export 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 || (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.
 */
export class DetachedFieldIndexSummarizer extends 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(detachedFieldIndexBlobKey, stringify(data));
    }
    async loadInternal(services, parse) {
        if (await services.contains(detachedFieldIndexBlobKey)) {
            const detachedFieldIndexBuffer = await services.readBlob(detachedFieldIndexBlobKey);
            const treeBufferString = bufferToString(detachedFieldIndexBuffer, "utf8");
            const parsed = parse(treeBufferString);
            this.detachedFieldIndex.loadData(parsed);
        }
    }
}
//# sourceMappingURL=detachedFieldIndexSummarizer.js.map