"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.blobHeadersBlobName = exports.getFluidDataStoreAttributes = exports.wrapSummaryInChannelsTree = exports.dataStoreAttributesBlobName = exports.nonDataStorePaths = exports.protocolTreeName = exports.rootHasIsolatedChannels = exports.idCompressorBlobName = exports.electedSummarizerBlobName = exports.recentBatchInfoBlobName = exports.chunksBlobName = exports.metadataBlobName = exports.aliasBlobName = exports.getMetadataFormatVersion = exports.extractSummaryMetadataMessage = exports.hasIsolatedChannels = exports.getAttributesFormatVersion = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const driver_definitions_1 = require("@fluidframework/driver-definitions");
const internal_2 = require("@fluidframework/driver-utils/internal");
const internal_3 = require("@fluidframework/runtime-definitions/internal");
const index_js_1 = require("../blobManager/index.js");
function getAttributesFormatVersion(attributes) {
    if (attributes.summaryFormatVersion) {
        /**
         * Version 2+: Introduces .channels trees for isolation of
         * channel trees from data store objects.
         */
        return attributes.summaryFormatVersion;
    }
    else if (attributes.snapshotFormatVersion === "0.1") {
        /**
         * Version 1: from this version the pkg within the data store
         * attributes blob is a JSON array rather than a string.
         */
        return 1;
    }
    /**
     * Version 0: format version is missing from summary.
     * This indicates it is an older version.
     */
    return 0;
}
exports.getAttributesFormatVersion = getAttributesFormatVersion;
function hasIsolatedChannels(attributes) {
    return !!attributes.summaryFormatVersion && !attributes.disableIsolatedChannels;
}
exports.hasIsolatedChannels = hasIsolatedChannels;
/**
 * Extracts the properties from an ISequencedDocumentMessage as defined by ISummaryMetadataMessage. This message is
 * added to the metadata blob in summary.
 */
const extractSummaryMetadataMessage = (message) => message === undefined
    ? undefined
    : {
        clientId: message.clientId,
        clientSequenceNumber: message.clientSequenceNumber,
        minimumSequenceNumber: message.minimumSequenceNumber,
        referenceSequenceNumber: message.referenceSequenceNumber,
        sequenceNumber: message.sequenceNumber,
        timestamp: message.timestamp,
        type: message.type,
    };
exports.extractSummaryMetadataMessage = extractSummaryMetadataMessage;
function getMetadataFormatVersion(metadata) {
    /**
     * Version 2+: Introduces runtime sequence number for data verification.
     *
     * Version 1+: Introduces .metadata blob and .channels trees for isolation of
     * data store trees from container-level objects.
     * Also introduces enableGC option stored in the summary.
     *
     * Version 0: metadata blob missing; format version is missing from summary.
     * This indicates it is an older version.
     */
    return metadata?.summaryFormatVersion ?? 0;
}
exports.getMetadataFormatVersion = getMetadataFormatVersion;
exports.aliasBlobName = ".aliases";
exports.metadataBlobName = ".metadata";
exports.chunksBlobName = ".chunks";
exports.recentBatchInfoBlobName = ".recentBatchInfo";
exports.electedSummarizerBlobName = ".electedSummarizer";
exports.idCompressorBlobName = ".idCompressor";
function rootHasIsolatedChannels(metadata) {
    return !!metadata && !metadata.disableIsolatedChannels;
}
exports.rootHasIsolatedChannels = rootHasIsolatedChannels;
exports.protocolTreeName = ".protocol";
/**
 * List of tree IDs at the container level which are reserved.
 * This is for older versions of summaries that do not yet have an
 * isolated data stores namespace. Without the namespace, this must
 * be used to prevent name collisions with data store IDs.
 */
exports.nonDataStorePaths = [
    exports.protocolTreeName,
    ".logTail",
    ".serviceProtocol",
    index_js_1.blobsTreeName,
    internal_3.gcTreeKey,
    exports.idCompressorBlobName,
];
exports.dataStoreAttributesBlobName = ".component";
/**
 * Modifies summary tree and stats to put tree under .channels tree.
 *
 * @param summarizeResult - Summary tree and stats to modify
 *
 * @example
 *
 * Converts from:
 *
 * ```typescript
 * {
 *     type: SummaryType.Tree,
 *     tree: { a: {...}, b: {...}, c: {...} },
 * }
 * ```
 *
 * to:
 *
 * ```typescript
 * {
 *     type: SummaryType.Tree,
 *     tree: {
 *         ".channels": {
 *             type: SummaryType.Tree,
 *             tree: { a: {...}, b: {...}, c: {...} }
 *         },
 *     },
 * }
 * ```
 *
 * And adds +1 to treeNodeCount in stats.
 */
function wrapSummaryInChannelsTree(summarizeResult) {
    summarizeResult.summary = {
        type: driver_definitions_1.SummaryType.Tree,
        tree: { [internal_3.channelsTreeName]: summarizeResult.summary },
    };
    summarizeResult.stats.treeNodeCount++;
}
exports.wrapSummaryInChannelsTree = wrapSummaryInChannelsTree;
async function getFluidDataStoreAttributes(storage, snapshot) {
    const attributes = await (0, internal_2.readAndParse)(storage, snapshot.blobs[exports.dataStoreAttributesBlobName]);
    // Use the snapshotFormatVersion to determine how the pkg is encoded in the snapshot.
    // For snapshotFormatVersion = "0.1" (1) or above, pkg is jsonified, otherwise it is just a string.
    // However the feature of loading a detached container from snapshot, is added when the
    // snapshotFormatVersion is at least "0.1" (1), so we don't expect it to be anything else.
    const formatVersion = getAttributesFormatVersion(attributes);
    (0, internal_1.assert)(formatVersion > 0, 0x1d5 /* Invalid snapshot format version */);
    return attributes;
}
exports.getFluidDataStoreAttributes = getFluidDataStoreAttributes;
var internal_4 = require("@fluidframework/driver-utils/internal");
Object.defineProperty(exports, "blobHeadersBlobName", { enumerable: true, get: function () { return internal_4.blobHeadersBlobName; } });
//# sourceMappingURL=summaryFormat.js.map