"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeOptionalFieldCodec = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const changeAtomIdCodec_js_1 = require("../changeAtomIdCodec.js");
const index_js_1 = require("../modular-schema/index.js");
const optionalFieldChangeFormatV0_js_1 = require("./optionalFieldChangeFormatV0.js");
function makeRegisterIdCodec(revisionTagCodec) {
    const changeAtomIdCodec = (0, changeAtomIdCodec_js_1.makeChangeAtomIdCodec)(revisionTagCodec);
    return {
        encode: (registerId, context) => {
            if (registerId === "self") {
                return null;
            }
            return changeAtomIdCodec.encode(registerId, context);
        },
        decode: (registerId, context) => {
            if (registerId === null) {
                return "self";
            }
            return changeAtomIdCodec.decode(registerId, context);
        },
        encodedSchema: optionalFieldChangeFormatV0_js_1.EncodedRegisterId,
    };
}
function makeOptionalFieldCodec(revisionTagCodec) {
    const registerIdCodec = makeRegisterIdCodec(revisionTagCodec);
    return {
        encode: (change, context) => {
            const encoded = {};
            encoded.m = [];
            if (change.valueReplace !== undefined) {
                if (change.valueReplace.src !== undefined) {
                    encoded.m.push([
                        registerIdCodec.encode(change.valueReplace.src, context.baseContext),
                        registerIdCodec.encode("self", context.baseContext),
                        true,
                    ]);
                }
                // When the source of the replace is "self", the destination is a reserved ID that will only be used if
                // the tree in the field is concurrently replaced.
                if (change.valueReplace.isEmpty || change.valueReplace.src === "self") {
                    encoded.d = registerIdCodec.encode(change.valueReplace.dst, context.baseContext);
                }
                else {
                    encoded.m.push([
                        registerIdCodec.encode("self", context.baseContext),
                        registerIdCodec.encode(change.valueReplace.dst, context.baseContext),
                        false,
                    ]);
                }
            }
            for (const [src, dst] of change.moves) {
                encoded.m.push([
                    registerIdCodec.encode(src, context.baseContext),
                    registerIdCodec.encode(dst, context.baseContext),
                    true,
                ]);
            }
            if (encoded.m.length === 0) {
                delete encoded.m;
            }
            if (change.childChanges.length > 0) {
                encoded.c = [];
                for (const [id, childChange] of change.childChanges) {
                    encoded.c.push([
                        registerIdCodec.encode(id, context.baseContext),
                        context.encodeNode(childChange),
                    ]);
                }
            }
            return encoded;
        },
        decode: (encoded, context) => {
            // The register that the node in the optional field is moved to upon detach
            let detached;
            // The register that the node is moved from to upon attaching that node in the optional field
            let attached;
            const moves = [];
            if (encoded.m !== undefined) {
                for (const [encodedSrc, encodedDst] of encoded.m) {
                    const src = registerIdCodec.decode(encodedSrc, context.baseContext);
                    const dst = registerIdCodec.decode(encodedDst, context.baseContext);
                    if (src === "self" || dst === "self") {
                        if (src === "self") {
                            (0, internal_1.assert)(detached === undefined, 0x8d0 /* Multiple detached nodes */);
                            detached = dst;
                        }
                        if (dst === "self") {
                            (0, internal_1.assert)(attached === undefined, 0x8d1 /* Multiple attached nodes */);
                            attached = src;
                        }
                    }
                    else {
                        moves.push([src, dst]);
                    }
                }
            }
            const decoded = {
                moves,
                childChanges: encoded.c?.map(([id, encodedChange]) => [
                    registerIdCodec.decode(id, context.baseContext),
                    context.decodeNode(encodedChange),
                ]) ?? [],
            };
            if (detached !== undefined && attached !== undefined) {
                if (detached === "self" || attached === "self") {
                    (0, internal_1.assert)(encoded.d !== undefined, 0x8d2 /* Invalid change: pin must have a reserved detach ID */);
                    const reserved = registerIdCodec.decode(encoded.d, context.baseContext);
                    (0, internal_1.assert)(reserved !== "self", 0x8d3 /* Invalid reserved detach ID */);
                    decoded.valueReplace = { isEmpty: false, dst: reserved, src: "self" };
                }
                else {
                    (0, internal_1.assert)(encoded.d === undefined, 0x8d4 /* Invalid change: unexpected reserved detach ID on a change that detaches a node from the field */);
                    decoded.valueReplace = {
                        isEmpty: false,
                        dst: detached,
                        src: attached,
                    };
                }
            }
            else if (attached !== undefined) {
                (0, internal_1.assert)(encoded.d !== undefined, 0x8d5 /* Invalid change: attach must have a reserved detach ID */);
                const reserved = registerIdCodec.decode(encoded.d, context.baseContext);
                (0, internal_1.assert)(reserved !== "self", 0x8d6 /* Invalid reserved detach ID */);
                decoded.valueReplace = {
                    isEmpty: true,
                    dst: reserved,
                    src: attached,
                };
            }
            else if (detached !== undefined) {
                (0, internal_1.assert)(encoded.d === undefined, 0x8d7 /* Invalid change: unexpected reserved detach ID on a change that detaches a node from the field */);
                (0, internal_1.assert)(detached !== "self", 0x8d8 /* Invalid detach ID */);
                decoded.valueReplace = {
                    isEmpty: false,
                    dst: detached,
                };
            }
            else if (encoded.d !== undefined) {
                const detachId = registerIdCodec.decode(encoded.d, context.baseContext);
                (0, internal_1.assert)(detachId !== "self", 0x8d9 /* Invalid detach ID */);
                decoded.valueReplace = {
                    isEmpty: true,
                    dst: detachId,
                };
            }
            return decoded;
        },
        encodedSchema: (0, optionalFieldChangeFormatV0_js_1.EncodedOptionalChangeset)(index_js_1.EncodedNodeChangeset),
    };
}
exports.makeOptionalFieldCodec = makeOptionalFieldCodec;
//# sourceMappingURL=optionalFieldCodecV0.js.map