"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeDetachedNodeToFieldCodecV1 = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../rebase/index.js");
const detachedFieldIndexFormatV1_js_1 = require("./detachedFieldIndexFormatV1.js");
const detachedFieldIndexCodecCommon_js_1 = require("./detachedFieldIndexCodecCommon.js");
class MajorCodec {
    constructor(revisionTagCodec, options, idCompressor) {
        this.revisionTagCodec = revisionTagCodec;
        this.options = options;
        this.idCompressor = idCompressor;
    }
    encode(major) {
        (0, internal_1.assert)(major !== undefined, 0x88e /* Unexpected undefined revision */);
        const id = this.revisionTagCodec.encode(major);
        /**
         * Preface: this codec is only used at summarization time (not for ops).
         * Note that the decode path must provide a session id in which to interpret the revision tag.
         * The revision associated with a detached root generally comes from the session which detaches that subtree,
         * which isn't generally the local session (nor is it available at decode time with the layering of the tree
         * package), despite decode using the local session id.
         *
         * This is made OK by enforcing that all ids on encode/decode are non-local, since local ids won't be interpretable
         * at decode time.
         * This assert is valid because the revision for an acked edit will have already been finalized, and a revision
         * for a local-only edit will be finalizable at summarization time (local edits can only occur on a summarizing client
         * if they're created while detached, and local ids made while detached are finalized before generating the attach summary).
         *
         * WARNING: the above is true when the whole container transitions from detached to attached,
         * but not when the container is already attached and it's just the shared-tree that is attaching.
         * The assert below will fail in such a scenario. This is addressed in the v2 codec.
         */
        (0, internal_1.assert)(id === "root" || id >= 0, 0x88f /* Expected final id on encode of detached field index revision */);
        return id;
    }
    decode(major) {
        (0, internal_1.assert)(major === "root" || major >= 0, 0x890 /* Expected final id on decode of detached field index revision */);
        return this.revisionTagCodec.decode(major, {
            originatorId: this.revisionTagCodec.localSessionId,
            idCompressor: this.idCompressor,
            revision: undefined,
        });
    }
}
function makeDetachedNodeToFieldCodecV1(revisionTagCodec, options, idCompressor) {
    const majorCodec = new MajorCodec(revisionTagCodec, options, idCompressor);
    return (0, detachedFieldIndexCodecCommon_js_1.makeDetachedFieldIndexCodecFromMajorCodec)(options, majorCodec, detachedFieldIndexFormatV1_js_1.version1, index_js_1.RevisionTagSchema);
}
exports.makeDetachedNodeToFieldCodecV1 = makeDetachedNodeToFieldCodecV1;
//# sourceMappingURL=detachedFieldIndexCodecV1.js.map