"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeModularChangeCodecV2 = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../../core/index.js");
const index_js_3 = require("../../util/index.js");
const index_js_4 = require("../chunked-forest/index.js");
const treeCompressionUtils_js_1 = require("../treeCompressionUtils.js");
const modularChangeFamily_js_1 = require("./modularChangeFamily.js");
const modularChangeTypes_js_1 = require("./modularChangeTypes.js");
const genericFieldKind_js_1 = require("./genericFieldKind.js");
const modularChangeFormatV2_js_1 = require("./modularChangeFormatV2.js");
const changeAtomIdCodec_js_1 = require("../changeAtomIdCodec.js");
function makeModularChangeCodecV2(fieldKinds, revisionTagCodec, fieldsCodec, codecOptions, chunkCompressionStrategy = treeCompressionUtils_js_1.TreeCompressionStrategy.Compressed) {
    // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
    const getMapEntry = ({ kind, formatVersion }) => {
        const codec = kind.changeHandler.codecsFactory(revisionTagCodec).resolve(formatVersion);
        return {
            codec,
            compiledSchema: codec.json.encodedSchema
                ? (0, index_js_1.extractJsonValidator)(codecOptions.jsonValidator).compile(codec.json.encodedSchema)
                : undefined,
        };
    };
    /**
     * The codec version for the generic field kind.
     */
    const genericFieldKindFormatVersion = 1;
    const fieldChangesetCodecs = new Map([
        [
            genericFieldKind_js_1.genericFieldKind.identifier,
            getMapEntry({ kind: genericFieldKind_js_1.genericFieldKind, formatVersion: genericFieldKindFormatVersion }),
        ],
    ]);
    fieldKinds.forEach((entry, identifier) => {
        fieldChangesetCodecs.set(identifier, getMapEntry(entry));
    });
    const getFieldChangesetCodec = (fieldKind) => {
        const entry = fieldChangesetCodecs.get(fieldKind);
        (0, internal_1.assert)(entry !== undefined, 0x5ea /* Tried to encode unsupported fieldKind */);
        return entry;
    };
    const changeAtomIdCodec = (0, changeAtomIdCodec_js_1.makeChangeAtomIdCodec)(revisionTagCodec);
    function encodeFieldChangesForJson(change, parentId, fieldToRoots, context, encodeNode, getInputDetachId, isAttachId, isDetachId) {
        const encodedFields = [];
        for (const [field, fieldChange] of change) {
            const { codec, compiledSchema } = getFieldChangesetCodec(fieldChange.fieldKind);
            const rootChanges = fieldToRoots.get([parentId?.revision, parentId?.localId, field]);
            const fieldContext = {
                baseContext: context,
                rootNodeChanges: rootChanges?.nodeChanges ?? (0, index_js_3.newTupleBTree)(),
                rootRenames: rootChanges?.renames ?? (0, index_js_2.newChangeAtomIdTransform)(),
                encodeNode,
                getInputRootId: getInputDetachId,
                isAttachId,
                isDetachId,
                decodeNode: () => (0, internal_1.fail)(0xb1e /* Should not decode nodes during field encoding */),
                decodeRootNodeChange: () => (0, internal_1.fail)("Should not be called during encoding"),
                decodeRootRename: () => (0, internal_1.fail)("Should not be called during encoding"),
                decodeMoveAndDetach: () => (0, internal_1.fail)("Should not be called during encoding"),
                generateId: () => (0, internal_1.fail)("Should not be called during encoding"),
            };
            const encodedChange = codec.json.encode(fieldChange.change, fieldContext);
            if (compiledSchema !== undefined && !compiledSchema.check(encodedChange)) {
                (0, internal_1.fail)(0xb1f /* Encoded change didn't pass schema validation. */);
            }
            const fieldKey = field;
            const encodedField = {
                fieldKey,
                fieldKind: fieldChange.fieldKind,
                change: encodedChange,
            };
            encodedFields.push(encodedField);
        }
        return encodedFields;
    }
    function encodeNodeChangesForJson(change, id, fieldToRoots, context, encodeNode, getInputDetachId, isAttachId, isDetachId) {
        const encodedChange = {};
        // Note: revert constraints are ignored for now because they would only be needed if we supported reverting changes made by peers.
        const { fieldChanges, nodeExistsConstraint } = change;
        if (fieldChanges !== undefined) {
            encodedChange.fieldChanges = encodeFieldChangesForJson(fieldChanges, id, fieldToRoots, context, encodeNode, getInputDetachId, isAttachId, isDetachId);
        }
        if (nodeExistsConstraint !== undefined) {
            encodedChange.nodeExistsConstraint = nodeExistsConstraint;
        }
        return encodedChange;
    }
    function encodeRootNodesForJson(roots, context, encodeNode) {
        const encoded = [];
        for (const [[revision, localId], nodeId] of roots.entries()) {
            encoded.push({
                detachId: changeAtomIdCodec.encode({ revision, localId }, context),
                nodeChangeset: encodeNode(nodeId),
            });
        }
        return encoded;
    }
    function encodeRenamesForJson(renames, context) {
        const encoded = [];
        for (const entry of renames.entries()) {
            encoded.push({
                oldId: changeAtomIdCodec.encode(entry.start, context),
                newId: changeAtomIdCodec.encode(entry.value, context),
                count: entry.length,
            });
        }
        return encoded;
    }
    function decodeFieldChangesFromJson(encodedChange, parentId, decodedCrossFieldKeys, context, decodeNode, idAllocator) {
        const decodedFields = new Map();
        for (const field of encodedChange) {
            const { codec, compiledSchema } = getFieldChangesetCodec(field.fieldKind);
            if (compiledSchema !== undefined && !compiledSchema.check(field.change)) {
                (0, internal_1.fail)(0xb20 /* Encoded change didn't pass schema validation. */);
            }
            const fieldId = {
                nodeId: parentId,
                field: field.fieldKey,
            };
            const fieldContext = {
                baseContext: context,
                rootNodeChanges: (0, index_js_3.newTupleBTree)(),
                rootRenames: (0, index_js_2.newChangeAtomIdTransform)(),
                encodeNode: () => (0, internal_1.fail)(0xb21 /* Should not encode nodes during field decoding */),
                getInputRootId: () => (0, internal_1.fail)("Should not query during decoding"),
                isAttachId: () => (0, internal_1.fail)("Should not query during decoding"),
                isDetachId: () => (0, internal_1.fail)("Should not query during decoding"),
                decodeNode: (encodedNode) => decodeNode(encodedNode, { field: fieldId }),
                decodeRootNodeChange: (detachId, encodedNode) => { },
                decodeRootRename: (oldId, newId, count) => { },
                decodeMoveAndDetach: (detachId, count) => { },
                generateId: () => ({
                    revision: context.revision,
                    localId: (0, index_js_3.brand)(idAllocator.allocate()),
                }),
            };
            const fieldChangeset = codec.json.decode(field.change, fieldContext);
            const crossFieldKeys = getChangeHandler(fieldKinds, field.fieldKind).getCrossFieldKeys(fieldChangeset);
            for (const { key, count } of crossFieldKeys) {
                decodedCrossFieldKeys.set(key, count, fieldId);
            }
            const fieldKey = (0, index_js_3.brand)(field.fieldKey);
            decodedFields.set(fieldKey, {
                fieldKind: field.fieldKind,
                change: (0, index_js_3.brand)(fieldChangeset),
            });
        }
        return decodedFields;
    }
    function decodeNodeChangesetFromJson(encodedChange, id, decodedCrossFieldKeys, decodedRootTable, context, decodeNode, idAllocator) {
        const decodedChange = {};
        const { fieldChanges, nodeExistsConstraint } = encodedChange;
        if (fieldChanges !== undefined) {
            decodedChange.fieldChanges = decodeFieldChangesFromJson(fieldChanges, id, decodedCrossFieldKeys, context, decodeNode, idAllocator);
        }
        if (nodeExistsConstraint !== undefined) {
            decodedChange.nodeExistsConstraint = nodeExistsConstraint;
        }
        return decodedChange;
    }
    function encodeDetachedNodes(detachedNodes, context) {
        if (detachedNodes === undefined) {
            return undefined;
        }
        const treesToEncode = [];
        const buildsArray = [];
        let buildsForRevision;
        for (const [[revision, id], chunk] of detachedNodes.entries()) {
            const encodedRevision = encodeRevisionOpt(revisionTagCodec, revision, context);
            if (buildsForRevision === undefined || buildsForRevision[1] !== encodedRevision) {
                if (buildsForRevision !== undefined) {
                    buildsArray.push(buildsForRevision);
                }
                buildsForRevision = encodedRevision !== undefined ? [[], encodedRevision] : [[]];
            }
            treesToEncode.push(chunk.cursor());
            const treeIndexInBatch = treesToEncode.length - 1;
            buildsForRevision?.[0].push([id, treeIndexInBatch]);
        }
        if (buildsForRevision !== undefined) {
            buildsArray.push(buildsForRevision);
        }
        return buildsArray.length === 0
            ? undefined
            : {
                builds: buildsArray,
                trees: fieldsCodec.encode(treesToEncode, {
                    encodeType: chunkCompressionStrategy,
                    schema: context.schema,
                    originatorId: context.originatorId,
                    idCompressor: context.idCompressor,
                }),
            };
    }
    function decodeDetachedNodes(encoded, context) {
        if (encoded === undefined || encoded.builds.length === 0) {
            return undefined;
        }
        const chunks = fieldsCodec.decode(encoded.trees, {
            encodeType: chunkCompressionStrategy,
            originatorId: context.originatorId,
            idCompressor: context.idCompressor,
        });
        const getChunk = (index) => {
            (0, internal_1.assert)(index < chunks.length, 0x898 /* out of bounds index for build chunk */);
            return (0, index_js_4.chunkFieldSingle)(chunks[index] ?? (0, internal_1.oob)(), {
                policy: index_js_4.defaultChunkPolicy,
                idCompressor: context.idCompressor,
            });
        };
        const map = (0, index_js_3.newTupleBTree)();
        encoded.builds.forEach((build) => {
            // EncodedRevisionTag cannot be an array so this ensures that we can isolate the tuple
            const revision = build[1] === undefined ? context.revision : revisionTagCodec.decode(build[1], context);
            const decodedChunks = build[0].map(([i, n]) => [
                i,
                getChunk(n),
            ]);
            for (const [id, chunk] of decodedChunks) {
                map.set([revision, id], chunk);
            }
        });
        return map;
    }
    function encodeRevisionInfos(revisions, context) {
        if (context.revision !== undefined) {
            (0, internal_1.assert)(revisions.length === 1 &&
                revisions[0] !== undefined &&
                revisions[0].revision === context.revision &&
                revisions[0].rollbackOf === undefined, 0x964 /* A tagged change should only contain the tagged revision */);
            return undefined;
        }
        const encodedRevisions = [];
        for (const revision of revisions) {
            const encodedRevision = {
                revision: revisionTagCodec.encode(revision.revision, context),
            };
            if (revision.rollbackOf !== undefined) {
                encodedRevision.rollbackOf = revisionTagCodec.encode(revision.rollbackOf, context);
            }
            encodedRevisions.push(encodedRevision);
        }
        return encodedRevisions;
    }
    function decodeRevisionInfos(revisions, context) {
        if (revisions === undefined) {
            return context.revision !== undefined ? [{ revision: context.revision }] : undefined;
        }
        const decodedRevisions = [];
        for (const revision of revisions) {
            const decodedRevision = {
                revision: revisionTagCodec.decode(revision.revision, context),
            };
            if (revision.rollbackOf !== undefined) {
                decodedRevision.rollbackOf = revisionTagCodec.decode(revision.rollbackOf, context);
            }
            decodedRevisions.push(decodedRevision);
        }
        return decodedRevisions;
    }
    function decodeRootTable(encodedRoots, encodedRenames, context, decodeNode) {
        const roots = (0, modularChangeFamily_js_1.newRootTable)();
        if (encodedRoots !== undefined) {
            for (const { detachId, nodeChangeset } of encodedRoots) {
                const decodedId = changeAtomIdCodec.decode(detachId, context);
                (0, modularChangeTypes_js_1.setInChangeAtomIdMap)(roots.nodeChanges, decodedId, decodeNode(nodeChangeset, { root: decodedId }));
            }
        }
        if (encodedRenames !== undefined) {
            for (const { oldId, newId, count } of encodedRenames) {
                (0, modularChangeFamily_js_1.addNodeRename)(roots, changeAtomIdCodec.decode(oldId, context), changeAtomIdCodec.decode(newId, context), count, undefined);
            }
        }
        return roots;
    }
    const modularChangeCodec = {
        encode: (change, context) => {
            const fieldToRoots = getFieldToRoots(change.rootNodes);
            const isAttachId = (id, count) => {
                const attachEntry = (0, modularChangeFamily_js_1.getFirstAttachField)(change.crossFieldKeys, id, count);
                return { ...attachEntry, value: attachEntry.value !== undefined };
            };
            const isDetachId = (id, count) => {
                const detachEntry = (0, modularChangeFamily_js_1.getFirstDetachField)(change.crossFieldKeys, id, count);
                const renameEntry = change.rootNodes.oldToNewId.getFirst(id, detachEntry.length);
                const isDetach = (detachEntry.value ?? renameEntry.value) !== undefined;
                return { start: id, value: isDetach, length: renameEntry.length };
            };
            const getInputDetachId = (id, count) => {
                return change.rootNodes.newToOldId.getFirst(id, count);
            };
            const encodeNode = (nodeId) => {
                // TODO: Handle node aliasing.
                const node = change.nodeChanges.get([nodeId.revision, nodeId.localId]);
                (0, internal_1.assert)(node !== undefined, 0x92e /* Unknown node ID */);
                return encodeNodeChangesForJson(node, nodeId, fieldToRoots, context, encodeNode, getInputDetachId, isAttachId, isDetachId);
            };
            // Destroys only exist in rollback changesets, which are never sent.
            (0, internal_1.assert)(change.destroys === undefined, 0x899 /* Unexpected changeset with destroys */);
            const encoded = {
                maxId: change.maxId,
                revisions: change.revisions === undefined
                    ? undefined
                    : encodeRevisionInfos(change.revisions, context),
                fieldChanges: encodeFieldChangesForJson(change.fieldChanges, undefined, fieldToRoots, context, encodeNode, getInputDetachId, isAttachId, isDetachId),
                rootNodes: encodeRootNodesForJson(change.rootNodes.nodeChanges, context, encodeNode),
                nodeRenames: encodeRenamesForJson(change.rootNodes.oldToNewId, context),
                builds: encodeDetachedNodes(change.builds, context),
                refreshers: encodeDetachedNodes(change.refreshers, context),
                violations: change.constraintViolationCount,
            };
            return encoded;
        },
        decode: (encodedChange, context) => {
            const idAllocator = (0, index_js_3.idAllocatorFromMaxId)(encodedChange.maxId);
            const nodeChanges = (0, index_js_3.newTupleBTree)();
            const nodeToParent = (0, index_js_3.newTupleBTree)();
            const crossFieldKeys = (0, modularChangeTypes_js_1.newCrossFieldRangeTable)();
            const rootNodes = (0, modularChangeFamily_js_1.newRootTable)();
            const decodeNode = (encodedNode, fieldId) => {
                const nodeId = {
                    revision: context.revision,
                    localId: (0, index_js_3.brand)(idAllocator.allocate()),
                };
                const node = decodeNodeChangesetFromJson(encodedNode, nodeId, crossFieldKeys, rootNodes, context, decodeNode, idAllocator);
                nodeChanges.set([nodeId.revision, nodeId.localId], node);
                if (fieldId !== undefined) {
                    nodeToParent.set([nodeId.revision, nodeId.localId], fieldId);
                }
                return nodeId;
            };
            const decoded = {
                rebaseVersion: 2,
                fieldChanges: decodeFieldChangesFromJson(encodedChange.fieldChanges, undefined, crossFieldKeys, context, decodeNode, idAllocator),
                nodeChanges,
                rootNodes: decodeRootTable(encodedChange.rootNodes, encodedChange.nodeRenames, context, decodeNode),
                nodeToParent,
                nodeAliases: (0, index_js_3.newTupleBTree)(),
                crossFieldKeys,
            };
            if (encodedChange.builds !== undefined) {
                decoded.builds = decodeDetachedNodes(encodedChange.builds, context);
            }
            if (encodedChange.refreshers !== undefined) {
                decoded.refreshers = decodeDetachedNodes(encodedChange.refreshers, context);
            }
            if (encodedChange.violations !== undefined) {
                decoded.constraintViolationCount = encodedChange.violations;
            }
            const decodedRevInfos = decodeRevisionInfos(encodedChange.revisions, context);
            if (decodedRevInfos !== undefined) {
                decoded.revisions = decodedRevInfos;
            }
            if (encodedChange.maxId !== undefined) {
                decoded.maxId = encodedChange.maxId;
            }
            return decoded;
        },
    };
    return (0, index_js_1.withSchemaValidation)(modularChangeFormatV2_js_1.EncodedModularChangeset, modularChangeCodec, codecOptions.jsonValidator);
}
exports.makeModularChangeCodecV2 = makeModularChangeCodecV2;
function getChangeHandler(fieldKinds, fieldKind) {
    if (fieldKind === genericFieldKind_js_1.genericFieldKind.identifier) {
        return genericFieldKind_js_1.genericFieldKind.changeHandler;
    }
    const handler = fieldKinds.get(fieldKind)?.kind.changeHandler;
    (0, internal_1.assert)(handler !== undefined, 0x9c1 /* Unknown field kind */);
    return handler;
}
function encodeRevisionOpt(revisionCodec, revision, context) {
    if (revision === undefined) {
        return undefined;
    }
    return revision === context.revision ? undefined : revisionCodec.encode(revision, context);
}
function getFieldToRoots(rootTable) {
    const fieldToRoots = (0, index_js_3.newTupleBTree)();
    for (const [[revision, localId], nodeId] of rootTable.nodeChanges.entries()) {
        const detachId = { revision, localId };
        const fieldId = rootTable.detachLocations.getFirst(detachId, 1).value;
        if (fieldId !== undefined) {
            (0, modularChangeTypes_js_1.setInChangeAtomIdMap)(getOrAddInFieldRootMap(fieldToRoots, fieldId).nodeChanges, detachId, nodeId);
        }
    }
    for (const entry of rootTable.oldToNewId.entries()) {
        const fieldId = rootTable.detachLocations.getFirst(entry.start, 1).value;
        if (fieldId !== undefined) {
            getOrAddInFieldRootMap(fieldToRoots, fieldId).renames.set(entry.start, entry.length, entry.value);
        }
    }
    return fieldToRoots;
}
function getOrAddInFieldRootMap(map, fieldId) {
    const key = [fieldId.nodeId?.revision, fieldId.nodeId?.localId, fieldId.field];
    const rootChanges = map.get(key);
    if (rootChanges !== undefined) {
        return rootChanges;
    }
    const newRootChanges = {
        nodeChanges: (0, index_js_3.newTupleBTree)(),
        renames: (0, index_js_2.newChangeAtomIdTransform)(),
    };
    map.set(key, newRootChanges);
    return newRootChanges;
}
//# sourceMappingURL=modularChangeCodecV2.js.map