"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeOptionalFieldCodecFamily = exports.noChangeCodecFamily = void 0;
const typebox_1 = require("@sinclair/typebox");
const core_utils_1 = require("@fluidframework/core-utils");
const index_js_1 = require("../../codec/index.js");
const changeAtomIdCodec_js_1 = require("../changeAtomIdCodec.js");
const optionalFieldChangeFormat_js_1 = require("./optionalFieldChangeFormat.js");
exports.noChangeCodecFamily = (0, index_js_1.makeCodecFamily)([[0, index_js_1.unitCodec]]);
const makeOptionalFieldCodecFamily = (childCodec, revisionTagCodec) => (0, index_js_1.makeCodecFamily)([[0, makeOptionalFieldCodec(childCodec, revisionTagCodec)]]);
exports.makeOptionalFieldCodecFamily = makeOptionalFieldCodecFamily;
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: optionalFieldChangeFormat_js_1.EncodedRegisterId,
    };
}
function makeOptionalFieldCodec(childCodec, 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),
                        registerIdCodec.encode("self", context),
                        true,
                    ]);
                }
                if (change.valueReplace.isEmpty) {
                    encoded.d = registerIdCodec.encode(change.valueReplace.dst, context);
                }
                else {
                    encoded.m.push([
                        registerIdCodec.encode("self", context),
                        registerIdCodec.encode(change.valueReplace.dst, context),
                        false,
                    ]);
                }
            }
            for (const [src, dst] of change.moves) {
                encoded.m.push([
                    registerIdCodec.encode(src, context),
                    registerIdCodec.encode(dst, context),
                    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),
                        childCodec.encode(childChange, context),
                    ]);
                }
            }
            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);
                    const dst = registerIdCodec.decode(encodedDst, context);
                    if (src === "self" || dst === "self") {
                        if (src === "self") {
                            (0, core_utils_1.assert)(detached === undefined, 0x8d0 /* Multiple detached nodes */);
                            detached = dst;
                        }
                        if (dst === "self") {
                            (0, core_utils_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),
                    childCodec.decode(encodedChange, context),
                ]) ?? [],
            };
            if (detached !== undefined && attached !== undefined) {
                if (detached === "self" || attached === "self") {
                    (0, core_utils_1.assert)(encoded.d !== undefined, 0x8d2 /* Invalid change: pin must have a reserved detach ID */);
                    const reserved = registerIdCodec.decode(encoded.d, context);
                    (0, core_utils_1.assert)(reserved !== "self", 0x8d3 /* Invalid reserved detach ID */);
                    decoded.valueReplace = { isEmpty: false, dst: reserved, src: "self" };
                }
                else {
                    (0, core_utils_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, core_utils_1.assert)(encoded.d !== undefined, 0x8d5 /* Invalid change: attach must have a reserved detach ID */);
                const reserved = registerIdCodec.decode(encoded.d, context);
                (0, core_utils_1.assert)(reserved !== "self", 0x8d6 /* Invalid reserved detach ID */);
                decoded.valueReplace = {
                    isEmpty: true,
                    dst: reserved,
                    src: attached,
                };
            }
            else if (detached !== undefined) {
                (0, core_utils_1.assert)(encoded.d === undefined, 0x8d7 /* Invalid change: unexpected reserved detach ID on a change that detaches a node from the field */);
                (0, core_utils_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);
                (0, core_utils_1.assert)(detachId !== "self", 0x8d9 /* Invalid detach ID */);
                decoded.valueReplace = {
                    isEmpty: true,
                    dst: detachId,
                };
            }
            return decoded;
        },
        encodedSchema: (0, optionalFieldChangeFormat_js_1.EncodedOptionalChangeset)(childCodec.encodedSchema ?? typebox_1.Type.Any()),
    };
}
//# sourceMappingURL=optionalFieldCodecs.js.map