"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeDetachedNodeToFieldCodecV2 = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/id-compressor/internal");
const detachedFieldIndexCodecCommon_js_1 = require("./detachedFieldIndexCodecCommon.js");
const detachedFieldIndexFormatCommon_js_1 = require("./detachedFieldIndexFormatCommon.js");
const detachedFieldIndexFormatV2_js_1 = require("./detachedFieldIndexFormatV2.js");
class MajorCodec {
    constructor(revisionTagCodec, idCompressor) {
        this.revisionTagCodec = revisionTagCodec;
        this.idCompressor = idCompressor;
    }
    encode(major) {
        (0, internal_1.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.
             */
            (0, internal_1.assert)(major !== "root", 0xbfc /* Major revision cannot be 'root' */);
            const long = this.idCompressor.decompress(major);
            return long;
        }
        return id;
    }
    decode(major) {
        (0, internal_1.assert)(major === "root" || (typeof major === "string" && (0, internal_2.isStableId)(major)) || major >= 0, 0xbfd /* Expected root, stable, or final compressed id */);
        if (typeof major === "string" && (0, internal_2.isStableId)(major)) {
            return this.idCompressor.recompress(major);
        }
        return this.revisionTagCodec.decode(major, {
            originatorId: this.revisionTagCodec.localSessionId,
            idCompressor: this.idCompressor,
            revision: undefined,
        });
    }
}
function makeDetachedNodeToFieldCodecV2(revisionTagCodec, idCompressor) {
    const majorCodec = new MajorCodec(revisionTagCodec, idCompressor);
    return (0, detachedFieldIndexCodecCommon_js_1.makeDetachedFieldIndexCodecFromMajorCodec)(majorCodec, detachedFieldIndexFormatCommon_js_1.DetachedFieldIndexFormatVersion.v2, detachedFieldIndexFormatV2_js_1.StableOrFinalRevisionTag);
}
exports.makeDetachedNodeToFieldCodecV2 = makeDetachedNodeToFieldCodecV2;
//# sourceMappingURL=detachedFieldIndexCodecV2.js.map