"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 changeAtomIdCodec_js_1 = require("../changeAtomIdCodec.js");
const index_js_1 = require("../modular-schema/index.js");
const optionalFieldChangeFormatV2_js_1 = require("./optionalFieldChangeFormatV2.js");
function makeRegisterIdCodec(changeAtomIdCodec) {
    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: optionalFieldChangeFormatV2_js_1.EncodedRegisterId,
    };
}
function makeOptionalFieldCodec(revisionTagCodec) {
    const changeAtomIdCodec = (0, changeAtomIdCodec_js_1.makeChangeAtomIdCodec)(revisionTagCodec);
    const registerIdCodec = makeRegisterIdCodec(changeAtomIdCodec);
    return {
        encode: (change, context) => {
            const encoded = {};
            if (change.moves.length > 0) {
                encoded.m = change.moves.map(([src, dst]) => [
                    changeAtomIdCodec.encode(src, context.baseContext),
                    changeAtomIdCodec.encode(dst, context.baseContext),
                ]);
            }
            if (change.valueReplace !== undefined) {
                encoded.r = {
                    e: change.valueReplace.isEmpty,
                    d: changeAtomIdCodec.encode(change.valueReplace.dst, context.baseContext),
                };
                if (change.valueReplace.src !== undefined) {
                    encoded.r.s = registerIdCodec.encode(change.valueReplace.src, context.baseContext);
                }
            }
            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) => {
            const decoded = {
                moves: encoded.m?.map(([encodedSrc, encodedDst]) => [
                    changeAtomIdCodec.decode(encodedSrc, context.baseContext),
                    changeAtomIdCodec.decode(encodedDst, context.baseContext),
                ]) ?? [],
                childChanges: encoded.c?.map(([id, encodedChange]) => [
                    registerIdCodec.decode(id, context.baseContext),
                    context.decodeNode(encodedChange),
                ]) ?? [],
            };
            if (encoded.r !== undefined) {
                const replace = {
                    isEmpty: encoded.r.e,
                    dst: changeAtomIdCodec.decode(encoded.r.d, context.baseContext),
                };
                if (encoded.r.s !== undefined) {
                    replace.src = registerIdCodec.decode(encoded.r.s, context.baseContext);
                }
                decoded.valueReplace = replace;
            }
            return decoded;
        },
        encodedSchema: (0, optionalFieldChangeFormatV2_js_1.EncodedOptionalChangeset)(index_js_1.EncodedNodeChangeset),
    };
}
exports.makeOptionalFieldCodec = makeOptionalFieldCodec;
//# sourceMappingURL=optionalFieldCodecV2.js.map