"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.toLatestVersion = exports.serializeAsMaxSupportedVersion = exports.serializeAsMinSupportedVersion = exports.hasMergeInfo = void 0;
const snapshotlegacy_js_1 = require("./snapshotlegacy.js");
/**
 * Returns true if the given 'spec' is an IJSONSegmentWithMergeInfo.
 */
function hasMergeInfo(spec) {
    return !!spec && typeof spec === "object" && "json" in spec;
}
exports.hasMergeInfo = hasMergeInfo;
function serializeAsMinSupportedVersion(path, chunk, logger, options, serializer, bind) {
    let targetChuck;
    if (chunk.version !== undefined) {
        logger.send({
            eventName: "MergeTreeChunk:serializeAsMinSupportedVersion",
            category: "generic",
            fromChunkVersion: chunk.version,
            toChunkVersion: undefined,
        });
    }
    switch (chunk.version) {
        case undefined: {
            targetChuck = chunk;
            targetChuck.headerMetadata = buildHeaderMetadataForLegacyChunk(path, targetChuck, options);
            break;
        }
        case "1": {
            const chunkV1 = chunk;
            const headerMetadata = path === snapshotlegacy_js_1.SnapshotLegacy.header ? chunkV1.headerMetadata : undefined;
            targetChuck = {
                version: undefined,
                chunkStartSegmentIndex: chunkV1.startIndex,
                chunkLengthChars: chunkV1.length,
                chunkSegmentCount: chunkV1.segmentCount,
                segmentTexts: chunkV1.segments,
                totalLengthChars: headerMetadata?.totalLength,
                totalSegmentCount: headerMetadata?.totalSegmentCount,
                chunkSequenceNumber: headerMetadata?.sequenceNumber,
                chunkMinSequenceNumber: headerMetadata?.minSequenceNumber,
                headerMetadata,
            };
            break;
        }
        default: {
            throw new Error(`Unsupported chunk path: ${path} version: ${chunk.version}`);
        }
    }
    return serializer.stringify(targetChuck, bind);
}
exports.serializeAsMinSupportedVersion = serializeAsMinSupportedVersion;
function serializeAsMaxSupportedVersion(path, chunk, logger, options, serializer, bind) {
    const targetChuck = toLatestVersion(path, chunk, logger, options);
    return serializer.stringify(targetChuck, bind);
}
exports.serializeAsMaxSupportedVersion = serializeAsMaxSupportedVersion;
function toLatestVersion(path, chunk, logger, options) {
    switch (chunk.version) {
        case undefined: {
            const chunkLegacy = chunk;
            return {
                version: "1",
                length: chunkLegacy.chunkLengthChars,
                segmentCount: chunkLegacy.chunkSegmentCount,
                headerMetadata: buildHeaderMetadataForLegacyChunk(path, chunkLegacy, options),
                segments: chunkLegacy.segmentTexts,
                startIndex: chunkLegacy.chunkStartSegmentIndex,
                attribution: chunkLegacy.attribution,
            };
        }
        case "1": {
            return chunk;
        }
        default: {
            throw new Error(`Unsupported chunk path: ${path} version: ${chunk.version}`);
        }
    }
}
exports.toLatestVersion = toLatestVersion;
function buildHeaderMetadataForLegacyChunk(path, chunk, options) {
    if (path === snapshotlegacy_js_1.SnapshotLegacy.header) {
        if (chunk.headerMetadata !== undefined) {
            return chunk.headerMetadata;
        }
        const chunkIds = [{ id: snapshotlegacy_js_1.SnapshotLegacy.header }];
        if (chunk.chunkLengthChars < chunk.totalLengthChars) {
            chunkIds.push({ id: snapshotlegacy_js_1.SnapshotLegacy.body });
        }
        return {
            orderedChunkMetadata: chunkIds,
            minSequenceNumber: chunk.chunkMinSequenceNumber,
            sequenceNumber: chunk.chunkSequenceNumber,
            totalLength: chunk.totalLengthChars,
            totalSegmentCount: chunk.totalSegmentCount,
        };
    }
    return undefined;
}
//# sourceMappingURL=snapshotChunks.js.map