/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { SnapshotLegacy } from "./snapshotlegacy.js";
/**
 * Returns true if the given 'spec' is an IJSONSegmentWithMergeInfo.
 */
export function hasMergeInfo(spec) {
    return !!spec && typeof spec === "object" && "json" in spec;
}
export 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.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);
}
export function serializeAsMaxSupportedVersion(path, chunk, logger, options, serializer, bind) {
    const targetChuck = toLatestVersion(path, chunk, logger, options);
    return serializer.stringify(targetChuck, bind);
}
export 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}`);
        }
    }
}
function buildHeaderMetadataForLegacyChunk(path, chunk, options) {
    if (path === SnapshotLegacy.header) {
        if (chunk.headerMetadata !== undefined) {
            return chunk.headerMetadata;
        }
        const chunkIds = [{ id: SnapshotLegacy.header }];
        if (chunk.chunkLengthChars < chunk.totalLengthChars) {
            chunkIds.push({ id: SnapshotLegacy.body });
        }
        return {
            orderedChunkMetadata: chunkIds,
            minSequenceNumber: chunk.chunkMinSequenceNumber,
            sequenceNumber: chunk.chunkSequenceNumber,
            totalLength: chunk.totalLengthChars,
            totalSegmentCount: chunk.totalSegmentCount,
        };
    }
    return undefined;
}
//# sourceMappingURL=snapshotChunks.js.map