/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { StableOrFinalRevisionTag, version2, } from "./detachedFieldIndexFormatV2.js";
import { makeDetachedFieldIndexCodecFromMajorCodec } from "./detachedFieldIndexCodecCommon.js";
import { isStableId } from "@fluidframework/id-compressor/internal";
class MajorCodec {
    constructor(revisionTagCodec, options, idCompressor) {
        this.revisionTagCodec = revisionTagCodec;
        this.options = options;
        this.idCompressor = idCompressor;
    }
    encode(major) {
        assert(major !== undefined, 0xbfb /* Unexpected undefined revision */);
        const id = this.revisionTagCodec.encode(major);
        if (id !== "root" && id < 0) {
            /**
             * This code path handles the case where the major revision is not finalized.
             * This can happen the SharedTree is being attached to an already attached container.
             */
            assert(major !== "root", 0xbfc /* Major revision cannot be 'root' */);
            const long = this.idCompressor.decompress(major);
            return long;
        }
        return id;
    }
    decode(major) {
        assert(major === "root" || (typeof major === "string" && isStableId(major)) || major >= 0, 0xbfd /* Expected root, stable, or final compressed id */);
        if (typeof major === "string" && isStableId(major)) {
            return this.idCompressor.recompress(major);
        }
        return this.revisionTagCodec.decode(major, {
            originatorId: this.revisionTagCodec.localSessionId,
            idCompressor: this.idCompressor,
            revision: undefined,
        });
    }
}
export function makeDetachedNodeToFieldCodecV2(revisionTagCodec, options, idCompressor) {
    const majorCodec = new MajorCodec(revisionTagCodec, options, idCompressor);
    return makeDetachedFieldIndexCodecFromMajorCodec(options, majorCodec, version2, StableOrFinalRevisionTag);
}
//# sourceMappingURL=detachedFieldIndexCodecV2.js.map