"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModularEditBuilder = exports.getChangeHandler = exports.getFieldKind = exports.revisionMetadataSourceFromInfo = exports.ModularChangeFamily = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const core_1 = require("../../core");
const util_1 = require("../../util");
const fakeRepairDataStore_1 = require("../fakeRepairDataStore");
const crossFieldQueries_1 = require("./crossFieldQueries");
const genericFieldKind_1 = require("./genericFieldKind");
const modularChangeCodecs_1 = require("./modularChangeCodecs");
/**
 * Implementation of ChangeFamily which delegates work in a given field to the appropriate FieldKind
 * as determined by the schema.
 *
 * @sealed
 * @alpha
 */
class ModularChangeFamily {
    constructor(fieldKinds) {
        this.fieldKinds = fieldKinds;
        this.codecs = (0, modularChangeCodecs_1.makeModularChangeCodecFamily)(this.fieldKinds);
    }
    get rebaser() {
        return this;
    }
    /**
     * Produces an equivalent list of `FieldChangeset`s that all target the same {@link FieldKind}.
     * @param changes - The list of `FieldChange`s whose `FieldChangeset`s needs to be normalized.
     * @returns An object that contains both the equivalent list of `FieldChangeset`s that all
     * target the same {@link FieldKind}, and the `FieldKind` that they target.
     * The returned `FieldChangeset`s may be a shallow copy of the input `FieldChange`s.
     */
    normalizeFieldChanges(changes, genId, revisionMetadata) {
        // TODO: Handle the case where changes have conflicting field kinds
        const nonGenericChange = changes.find((change) => change.fieldKind !== genericFieldKind_1.genericFieldKind.identifier);
        if (nonGenericChange === undefined) {
            // All the changes are generic
            return { fieldKind: genericFieldKind_1.genericFieldKind, changesets: changes.map((c) => c.change) };
        }
        const kind = nonGenericChange.fieldKind;
        const fieldKind = getFieldKind(this.fieldKinds, kind);
        const handler = fieldKind.changeHandler;
        const normalizedChanges = changes.map((change) => {
            if (change.fieldKind === genericFieldKind_1.genericFieldKind.identifier) {
                // The cast is based on the `fieldKind` check above
                const genericChange = change.change;
                return (0, genericFieldKind_1.convertGenericChange)(genericChange, handler, (children) => this.composeNodeChanges(children, genId, newCrossFieldTable(), revisionMetadata), genId, revisionMetadata);
            }
            return change.change;
        });
        return { fieldKind, changesets: normalizedChanges };
    }
    compose(changes) {
        const { revInfos, maxId } = getRevInfoFromTaggedChanges(changes);
        const revisionMetadata = revisionMetadataSourceFromInfo(revInfos);
        const idState = { maxId };
        const genId = (0, crossFieldQueries_1.idAllocatorFromState)(idState);
        const crossFieldTable = newCrossFieldTable();
        const changesWithoutConstraintViolations = changes.filter((change) => { var _a; return ((_a = change.change.constraintViolationCount) !== null && _a !== void 0 ? _a : 0) === 0; });
        const composedFields = this.composeFieldMaps(changesWithoutConstraintViolations.map((change) => (0, core_1.tagChange)(change.change.fieldChanges, change.revision)), genId, crossFieldTable, revisionMetadata);
        if (crossFieldTable.invalidatedFields.size > 0) {
            const fieldsToUpdate = crossFieldTable.invalidatedFields;
            crossFieldTable.invalidatedFields = new Set();
            for (const field of fieldsToUpdate) {
                const amendedChange = getChangeHandler(this.fieldKinds, field.fieldKind).rebaser.amendCompose(field.change, (children) => this.composeNodeChanges(children, genId, crossFieldTable, revisionMetadata), genId, newCrossFieldManager(crossFieldTable), revisionMetadata);
                field.change = (0, util_1.brand)(amendedChange);
            }
        }
        (0, common_utils_1.assert)(crossFieldTable.invalidatedFields.size === 0, 0x59b /* Should not need more than one amend pass. */);
        return makeModularChangeset(composedFields, idState.maxId, revInfos);
    }
    composeFieldMaps(changes, genId, crossFieldTable, revisionMetadata) {
        const fieldChanges = new Map();
        for (const change of changes) {
            for (const [key, fieldChange] of change.change) {
                const fieldChangeToCompose = fieldChange.revision !== undefined || change.revision === undefined
                    ? fieldChange
                    : Object.assign(Object.assign({}, fieldChange), { revision: change.revision });
                (0, util_1.getOrAddEmptyToMap)(fieldChanges, key).push(fieldChangeToCompose);
            }
        }
        const composedFields = new Map();
        for (const [field, changesForField] of fieldChanges) {
            let composedField;
            if (changesForField.length === 1) {
                // BUG: This field might be affected by cross-field effects, so we must recurse into it.
                composedField = changesForField[0];
            }
            else {
                const { fieldKind, changesets } = this.normalizeFieldChanges(changesForField, genId, revisionMetadata);
                (0, common_utils_1.assert)(changesets.length === changesForField.length, 0x4a8 /* Number of changes should be constant when normalizing */);
                const manager = newCrossFieldManager(crossFieldTable);
                const taggedChangesets = changesets.map((change, i) => (0, core_1.tagChange)(change, changesForField[i].revision));
                const composedChange = fieldKind.changeHandler.rebaser.compose(taggedChangesets, (children) => this.composeNodeChanges(children, genId, crossFieldTable, revisionMetadata), genId, manager, revisionMetadata);
                composedField = {
                    fieldKind: fieldKind.identifier,
                    change: (0, util_1.brand)(composedChange),
                };
                addFieldData(manager, composedField);
            }
            // TODO: Could optimize by checking that composedField is non-empty
            composedFields.set(field, composedField);
        }
        return composedFields;
    }
    composeNodeChanges(changes, genId, crossFieldTable, revisionMetadata) {
        var _a;
        const fieldChanges = [];
        let valueChange;
        let valueConstraint;
        for (const change of changes) {
            // Use the first defined value constraint before any value changes.
            // Any value constraints defined after a value change can never be violated so they are ignored in the composition.
            if (change.change.valueConstraint !== undefined &&
                valueConstraint === undefined &&
                valueChange === undefined) {
                valueConstraint = Object.assign({}, change.change.valueConstraint);
            }
            if (change.change.valueChange !== undefined) {
                valueChange = Object.assign({}, change.change.valueChange);
                (_a = valueChange.revision) !== null && _a !== void 0 ? _a : (valueChange.revision = change.revision);
            }
            if (change.change.fieldChanges !== undefined) {
                fieldChanges.push((0, core_1.tagChange)(change.change.fieldChanges, change.revision));
            }
        }
        const composedFieldChanges = this.composeFieldMaps(fieldChanges, genId, crossFieldTable, revisionMetadata);
        const composedNodeChange = {};
        if (valueChange !== undefined) {
            composedNodeChange.valueChange = valueChange;
        }
        if (composedFieldChanges.size > 0) {
            composedNodeChange.fieldChanges = composedFieldChanges;
        }
        if (valueConstraint !== undefined) {
            composedNodeChange.valueConstraint = valueConstraint;
        }
        return composedNodeChange;
    }
    /**
     * @param change - The change to invert.
     * @param isRollback - Whether the inverted change is meant to rollback a change on a branch as is the case when
     * performing a sandwich rebase.
     * @param repairStore - The store to query for repair data.
     */
    invert(change, isRollback, repairStore) {
        var _a;
        const idState = { maxId: (0, util_1.brand)((_a = change.change.maxId) !== null && _a !== void 0 ? _a : -1) };
        const genId = (0, crossFieldQueries_1.idAllocatorFromState)(idState);
        const crossFieldTable = newCrossFieldTable();
        const resolvedRepairStore = repairStore !== null && repairStore !== void 0 ? repairStore : fakeRepairDataStore_1.dummyRepairDataStore;
        const invertedFields = this.invertFieldMap((0, core_1.tagChange)(change.change.fieldChanges, change.revision), genId, resolvedRepairStore, undefined, crossFieldTable);
        if (crossFieldTable.invalidatedFields.size > 0) {
            const fieldsToUpdate = crossFieldTable.invalidatedFields;
            crossFieldTable.invalidatedFields = new Set();
            for (const { fieldKey, fieldChange, path, originalRevision } of fieldsToUpdate) {
                const amendedChange = getChangeHandler(this.fieldKinds, fieldChange.fieldKind).rebaser.amendInvert(fieldChange.change, originalRevision, (revision, index, count) => resolvedRepairStore.getNodes(revision, path, fieldKey, index, count), genId, newCrossFieldManager(crossFieldTable));
                fieldChange.change = (0, util_1.brand)(amendedChange);
            }
        }
        (0, common_utils_1.assert)(crossFieldTable.invalidatedFields.size === 0, 0x59c /* Should not need more than one amend pass. */);
        const revInfo = change.change.revisions;
        return makeModularChangeset(invertedFields, idState.maxId, revInfo === undefined
            ? undefined
            : (isRollback
                ? revInfo.map(({ revision }) => ({ revision, rollbackOf: revision }))
                : Array.from(revInfo)).reverse(), change.change.constraintViolationCount);
    }
    invertFieldMap(changes, genId, repairStore, path, crossFieldTable) {
        const invertedFields = new Map();
        for (const [field, fieldChange] of changes.change) {
            const { revision } = fieldChange.revision !== undefined ? fieldChange : changes;
            const reviver = (revisionTag, index, count) => repairStore.getNodes(revisionTag, path, field, index, count);
            const manager = newCrossFieldManager(crossFieldTable);
            const invertedChange = getChangeHandler(this.fieldKinds, fieldChange.fieldKind).rebaser.invert({ revision, change: fieldChange.change }, (childChanges, index) => this.invertNodeChange({ revision, change: childChanges }, genId, crossFieldTable, repairStore, index === undefined
                ? undefined
                : {
                    parent: path,
                    parentField: field,
                    parentIndex: index,
                }), reviver, genId, manager);
            const invertedFieldChange = Object.assign(Object.assign({}, fieldChange), { change: (0, util_1.brand)(invertedChange) });
            invertedFields.set(field, invertedFieldChange);
            const invertData = {
                fieldKey: field,
                fieldChange: invertedFieldChange,
                path,
                originalRevision: changes.revision,
            };
            addFieldData(manager, invertData);
        }
        return invertedFields;
    }
    invertNodeChange(change, genId, crossFieldTable, repairStore, path) {
        var _a;
        const inverse = {};
        if (change.change.valueChange !== undefined) {
            (0, common_utils_1.assert)(!("revert" in change.change.valueChange), 0x4a9 /* Inverting inverse changes is currently not supported */);
            (0, common_utils_1.assert)(path !== undefined, 0x59d /* Only existing nodes can have their value restored */);
            const revision = (_a = change.change.valueChange.revision) !== null && _a !== void 0 ? _a : change.revision;
            (0, common_utils_1.assert)(revision !== undefined, 0x59e /* Unable to revert to undefined revision */);
            inverse.valueChange = { value: repairStore.getValue(revision, path) };
        }
        if (change.change.fieldChanges !== undefined) {
            inverse.fieldChanges = this.invertFieldMap(Object.assign(Object.assign({}, change), { change: change.change.fieldChanges }), genId, repairStore, path, crossFieldTable);
        }
        return inverse;
    }
    rebase(change, over) {
        var _a, _b, _c;
        const maxId = Math.max((_a = change.maxId) !== null && _a !== void 0 ? _a : -1, (_b = over.change.maxId) !== null && _b !== void 0 ? _b : -1);
        const idState = { maxId: (0, util_1.brand)(maxId) };
        const genId = (0, crossFieldQueries_1.idAllocatorFromState)(idState);
        const crossFieldTable = Object.assign(Object.assign({}, newCrossFieldTable()), { baseMapToRebased: new Map(), baseChangeToContext: new Map(), baseMapToParentField: new Map() });
        const constraintState = newConstraintState((_c = change.constraintViolationCount) !== null && _c !== void 0 ? _c : 0);
        const revInfos = [];
        revInfos.push(...revisionInfoFromTaggedChange(over));
        if (change.revisions !== undefined) {
            revInfos.push(...change.revisions);
        }
        const revisionMetadata = revisionMetadataSourceFromInfo(revInfos);
        const rebasedFields = this.rebaseFieldMap(change.fieldChanges, (0, core_1.tagChange)(over.change.fieldChanges, over.revision), genId, crossFieldTable, () => true, revisionMetadata, constraintState);
        const rebasedChangeset = makeModularChangeset(rebasedFields, idState.maxId, change.revisions, constraintState.violationCount);
        crossFieldTable.baseMapToRebased.set(over.change.fieldChanges, rebasedChangeset);
        const [fieldsToAmend, invalidatedEmptyFields] = getFieldsToAmend(crossFieldTable.invalidatedFields, crossFieldTable.baseMapToRebased, crossFieldTable.baseChangeToContext, crossFieldTable.baseMapToParentField);
        const constraintViolations = constraintState.violationCount;
        this.amendRebase(crossFieldTable, fieldsToAmend, invalidatedEmptyFields, genId, revisionMetadata, constraintState);
        (0, common_utils_1.assert)(crossFieldTable.invalidatedFields.size === 0, 0x59f /* Should not need more than one amend pass. */);
        (0, common_utils_1.assert)(constraintState.violationCount === constraintViolations, 0x5b4 /* Should not change constraint violation count during amend pass */);
        if (idState.maxId >= 0) {
            rebasedChangeset.maxId = (0, util_1.brand)(idState.maxId);
        }
        return rebasedChangeset;
    }
    amendRebase(crossFieldTable, fieldsToAmend, invalidatedEmptyFields, genId, revisionMetadata, constraintState) {
        var _a, _b, _c, _d;
        crossFieldTable.invalidatedFields = new Set();
        for (const fieldToAmend of fieldsToAmend) {
            const fieldContext = crossFieldTable.baseChangeToContext.get(fieldToAmend);
            let rebasedChange;
            let baseChanges;
            let baseRevision;
            let newNode;
            let field;
            if (fieldContext !== undefined) {
                // fieldToAmend is part of the base changeset.
                newNode = crossFieldTable.baseMapToRebased.get(fieldContext.map);
                (0, common_utils_1.assert)(newNode !== undefined, 0x5b5 /* Should be a new HasFieldChanges associated with this base change */);
                baseChanges = fieldToAmend;
                field = fieldContext.field;
                rebasedChange = (_b = (_a = newNode.fieldChanges) === null || _a === void 0 ? void 0 : _a.get(field)) !== null && _b !== void 0 ? _b : {
                    fieldKind: genericFieldKind_1.genericFieldKind.identifier,
                    change: (0, util_1.brand)((0, genericFieldKind_1.newGenericChangeset)()),
                };
                baseRevision = (_c = baseChanges.revision) !== null && _c !== void 0 ? _c : fieldContext.revision;
            }
            else {
                // fieldToAmend is part of the rebased changeset.
                rebasedChange = fieldToAmend;
                baseChanges = {
                    fieldKind: genericFieldKind_1.genericFieldKind.identifier,
                    change: (0, util_1.brand)((0, genericFieldKind_1.newGenericChangeset)()),
                };
            }
            const { fieldKind, changesets: [fieldChangeset, baseChangeset], } = this.normalizeFieldChanges([rebasedChange, baseChanges], genId, revisionMetadata);
            const amendedChange = fieldKind.changeHandler.rebaser.amendRebase(fieldChangeset, (0, core_1.tagChange)(baseChangeset, baseRevision), (child, baseChild) => this.rebaseNodeChange(child, baseChild !== undefined ? (0, core_1.tagChange)(baseChild, baseRevision) : undefined, genId, crossFieldTable, undefined, (base, newNodeChange) => newNodeChange === undefined && invalidatedEmptyFields.has(base), revisionMetadata, constraintState), genId, newCrossFieldManager(crossFieldTable), revisionMetadata);
            if (newNode !== undefined && field !== undefined) {
                if (fieldKind.changeHandler.isEmpty(amendedChange)) {
                    (_d = newNode.fieldChanges) === null || _d === void 0 ? void 0 : _d.delete(field);
                }
                else {
                    if (newNode.fieldChanges === undefined) {
                        newNode.fieldChanges = new Map();
                    }
                    newNode.fieldChanges.set(field, {
                        fieldKind: fieldKind.identifier,
                        change: (0, util_1.brand)(amendedChange),
                    });
                }
            }
            else {
                rebasedChange.change = (0, util_1.brand)(amendedChange);
            }
        }
    }
    rebaseFieldMap(change, over, genId, crossFieldTable, fieldFilter, revisionMetadata, constraintState) {
        var _a, _b;
        const rebasedFields = new Map();
        // Rebase fields contained in the base changeset
        for (const [field, baseChanges] of over.change) {
            if (!fieldFilter(baseChanges, change.get(field))) {
                continue;
            }
            const fieldChange = (_a = change.get(field)) !== null && _a !== void 0 ? _a : {
                fieldKind: genericFieldKind_1.genericFieldKind.identifier,
                change: (0, util_1.brand)((0, genericFieldKind_1.newGenericChangeset)()),
            };
            const { fieldKind, changesets: [fieldChangeset, baseChangeset], } = this.normalizeFieldChanges([fieldChange, baseChanges], genId, revisionMetadata);
            const { revision } = over;
            const taggedBaseChange = { revision, change: baseChangeset };
            const manager = newCrossFieldManager(crossFieldTable);
            const rebasedField = fieldKind.changeHandler.rebaser.rebase(fieldChangeset, taggedBaseChange, (child, baseChild) => this.rebaseNodeChange(child, baseChild !== undefined ? { revision, change: baseChild } : undefined, genId, crossFieldTable, baseChanges, fieldFilter, revisionMetadata, constraintState), genId, manager, revisionMetadata);
            if (!fieldKind.changeHandler.isEmpty(rebasedField)) {
                const rebasedFieldChange = {
                    fieldKind: fieldKind.identifier,
                    change: (0, util_1.brand)(rebasedField),
                };
                rebasedFields.set(field, rebasedFieldChange);
            }
            addFieldData(manager, baseChanges);
            crossFieldTable.baseChangeToContext.set(baseChanges, {
                map: over.change,
                field,
                revision,
            });
        }
        // Rebase the fields of the new changeset which don't have a corresponding base field.
        for (const [field, fieldChange] of change) {
            if (!((_b = over.change) === null || _b === void 0 ? void 0 : _b.has(field))) {
                const baseChanges = {
                    fieldKind: genericFieldKind_1.genericFieldKind.identifier,
                    change: (0, util_1.brand)((0, genericFieldKind_1.newGenericChangeset)()),
                };
                const { fieldKind, changesets: [fieldChangeset, baseChangeset], } = this.normalizeFieldChanges([fieldChange, baseChanges], genId, revisionMetadata);
                const manager = newCrossFieldManager(crossFieldTable);
                const rebasedChangeset = fieldKind.changeHandler.rebaser.rebase(fieldChangeset, (0, core_1.tagChange)(baseChangeset, over.revision), (child, baseChild) => {
                    (0, common_utils_1.assert)(baseChild === undefined, 0x5b6 /* This field should not have any base changes */);
                    return this.rebaseNodeChange(child, undefined, genId, crossFieldTable, baseChanges, fieldFilter, revisionMetadata, constraintState);
                }, genId, manager, revisionMetadata);
                const rebasedFieldChange = {
                    fieldKind: fieldKind.identifier,
                    change: (0, util_1.brand)(rebasedChangeset),
                };
                rebasedFields.set(field, rebasedFieldChange);
                addFieldData(manager, rebasedChangeset);
            }
        }
        return rebasedFields;
    }
    rebaseNodeChange(change, over, genId, crossFieldTable, parentField, fieldFilter, revisionMetadata, constraintState) {
        var _a, _b, _c, _d, _e;
        if (change === undefined && ((_a = over === null || over === void 0 ? void 0 : over.change) === null || _a === void 0 ? void 0 : _a.fieldChanges) === undefined) {
            return undefined;
        }
        if (((_b = over === null || over === void 0 ? void 0 : over.change) === null || _b === void 0 ? void 0 : _b.fieldChanges) !== undefined && parentField !== undefined) {
            crossFieldTable.baseMapToParentField.set(over.change.fieldChanges, parentField);
        }
        const baseMap = ((_c = over === null || over === void 0 ? void 0 : over.change) === null || _c === void 0 ? void 0 : _c.fieldChanges) !== undefined
            ? Object.assign(Object.assign({}, over), { change: over.change.fieldChanges }) : (0, core_1.makeAnonChange)(new Map());
        const fieldChanges = this.rebaseFieldMap((_d = change === null || change === void 0 ? void 0 : change.fieldChanges) !== null && _d !== void 0 ? _d : new Map(), baseMap, genId, crossFieldTable, fieldFilter, revisionMetadata, constraintState);
        const rebasedChange = {};
        if ((change === null || change === void 0 ? void 0 : change.valueChange) !== undefined) {
            rebasedChange.valueChange = change.valueChange;
        }
        if (fieldChanges.size > 0) {
            rebasedChange.fieldChanges = fieldChanges;
        }
        if ((change === null || change === void 0 ? void 0 : change.valueConstraint) !== undefined) {
            rebasedChange.valueConstraint = change.valueConstraint;
        }
        // We only care if a violated constraint is fixed or if a non-violated
        // constraint becomes violated
        if (rebasedChange.valueConstraint !== undefined && (over === null || over === void 0 ? void 0 : over.change.valueChange) !== undefined) {
            const violatedByOver = over.change.valueChange.value !== rebasedChange.valueConstraint.value;
            if (rebasedChange.valueConstraint.violated !== violatedByOver) {
                rebasedChange.valueConstraint = Object.assign(Object.assign({}, rebasedChange.valueConstraint), { violated: violatedByOver });
                constraintState.violationCount += violatedByOver ? 1 : -1;
            }
        }
        if (isEmptyNodeChangeset(rebasedChange)) {
            return undefined;
        }
        if (((_e = over === null || over === void 0 ? void 0 : over.change) === null || _e === void 0 ? void 0 : _e.fieldChanges) !== undefined) {
            crossFieldTable.baseMapToRebased.set(over.change.fieldChanges, rebasedChange);
        }
        return rebasedChange;
    }
    rebaseAnchors(anchors, over) {
        anchors.applyDelta(this.intoDelta(over));
    }
    intoDelta(change) {
        return this.intoDeltaImpl(change.fieldChanges);
    }
    /**
     * @param change - The change to convert into a delta.
     * @param repairStore - The store to query for repair data.
     * @param path - The path of the node being altered by the change as defined by the input context.
     * Undefined for the root and for nodes that do not exist in the input context.
     */
    intoDeltaImpl(change) {
        const delta = new Map();
        for (const [field, fieldChange] of change) {
            const deltaField = getChangeHandler(this.fieldKinds, fieldChange.fieldKind).intoDelta(fieldChange.change, (childChange) => this.deltaFromNodeChange(childChange));
            delta.set(field, deltaField);
        }
        return delta;
    }
    deltaFromNodeChange(change) {
        const modify = {
            type: core_1.Delta.MarkType.Modify,
        };
        const valueChange = change.valueChange;
        if (valueChange !== undefined) {
            modify.setValue = valueChange.value;
        }
        if (change.fieldChanges !== undefined) {
            modify.fields = this.intoDeltaImpl(change.fieldChanges);
        }
        return modify;
    }
    buildEditor(changeReceiver, anchors) {
        return new ModularEditBuilder(this, changeReceiver, anchors);
    }
}
exports.ModularChangeFamily = ModularChangeFamily;
function getFieldsToAmend(invalidatedFields, baseMapToRebased, baseChangeToContext, baseMapToParentField) {
    const fieldsToAmend = new Set();
    const invalidatedEmptyFields = new Set();
    for (let fieldChange of invalidatedFields) {
        // fieldChange may be either part of the base changeset or the rebased changeset.
        // If `baseChangeToContext` has an entry for it, it is part of the base changeset.
        // Otherwise it is part of the rebased changeset, and there is no corresponding base FieldChange.
        // If part of the rebased changeset we can add it to `fieldsToAmend`.
        // If part of the base changeset we may have to walk up to find an ancestor with a corresponding HasFieldChanges
        // and add that ancestor to `fieldsToAmend`.
        let baseFieldContext = baseChangeToContext.get(fieldChange);
        while (baseFieldContext !== undefined && !baseMapToRebased.has(baseFieldContext.map)) {
            invalidatedEmptyFields.add(fieldChange);
            const baseFieldTemp = baseMapToParentField.get(baseFieldContext.map);
            (0, common_utils_1.assert)(baseFieldTemp !== undefined, 0x5b7 /* Should have parent for field */);
            fieldChange = baseFieldTemp;
            const contextTemp = baseChangeToContext.get(fieldChange);
            (0, common_utils_1.assert)(contextTemp !== undefined, 0x5b8 /* Should have context for field */);
            baseFieldContext = contextTemp;
        }
        fieldsToAmend.add(fieldChange);
    }
    return [fieldsToAmend, invalidatedEmptyFields];
}
/**
 * @alpha
 */
function revisionMetadataSourceFromInfo(revInfos) {
    const getIndex = (revision) => {
        const index = revInfos.findIndex((revInfo) => revInfo.revision === revision);
        (0, common_utils_1.assert)(index !== -1, 0x5a0 /* Unable to index unknown revision */);
        return index;
    };
    const getInfo = (revision) => {
        return revInfos[getIndex(revision)];
    };
    return { getIndex, getInfo };
}
exports.revisionMetadataSourceFromInfo = revisionMetadataSourceFromInfo;
function isEmptyNodeChangeset(change) {
    return (change.fieldChanges === undefined &&
        change.valueChange === undefined &&
        change.valueConstraint === undefined);
}
function getFieldKind(fieldKinds, kind) {
    if (kind === genericFieldKind_1.genericFieldKind.identifier) {
        return genericFieldKind_1.genericFieldKind;
    }
    const fieldKind = fieldKinds.get(kind);
    (0, common_utils_1.assert)(fieldKind !== undefined, 0x3ad /* Unknown field kind */);
    return fieldKind;
}
exports.getFieldKind = getFieldKind;
function getChangeHandler(fieldKinds, kind) {
    return getFieldKind(fieldKinds, kind).changeHandler;
}
exports.getChangeHandler = getChangeHandler;
function newCrossFieldTable() {
    return {
        srcTable: new Map(),
        dstTable: new Map(),
        srcDependents: new Map(),
        dstDependents: new Map(),
        invalidatedFields: new Set(),
    };
}
function newConstraintState(violationCount) {
    return {
        violationCount,
    };
}
function newCrossFieldManager(crossFieldTable) {
    const srcQueries = new Map();
    const dstQueries = new Map();
    const getMap = (target) => target === crossFieldQueries_1.CrossFieldTarget.Source ? crossFieldTable.srcTable : crossFieldTable.dstTable;
    const getQueries = (target) => target === crossFieldQueries_1.CrossFieldTarget.Source ? srcQueries : dstQueries;
    const manager = {
        table: crossFieldTable,
        srcQueries,
        dstQueries,
        fieldInvalidated: false,
        getOrCreate: (target, revision, id, newValue, invalidateDependents) => {
            if (invalidateDependents) {
                const dependents = target === crossFieldQueries_1.CrossFieldTarget.Source
                    ? crossFieldTable.srcDependents
                    : crossFieldTable.dstDependents;
                const dependent = (0, util_1.tryGetFromNestedMap)(dependents, revision, id);
                if (dependent !== undefined) {
                    crossFieldTable.invalidatedFields.add(dependent);
                }
                if ((0, util_1.nestedSetContains)(getQueries(target), revision, id)) {
                    manager.fieldInvalidated = true;
                }
            }
            return (0, util_1.getOrAddInNestedMap)(getMap(target), revision, id, newValue);
        },
        get: (target, revision, id, addDependency) => {
            if (addDependency) {
                (0, util_1.addToNestedSet)(getQueries(target), revision, id);
            }
            return (0, util_1.tryGetFromNestedMap)(getMap(target), revision, id);
        },
    };
    return manager;
}
function addFieldData(manager, fieldData) {
    for (const [revision, ids] of manager.srcQueries) {
        for (const id of ids.keys()) {
            (0, common_utils_1.assert)((0, util_1.tryGetFromNestedMap)(manager.table.srcDependents, revision, id) === undefined, 0x564 /* TODO: Support multiple dependents per key */);
            (0, util_1.setInNestedMap)(manager.table.srcDependents, revision, id, fieldData);
        }
    }
    for (const [revision, ids] of manager.dstQueries) {
        for (const id of ids.keys()) {
            (0, common_utils_1.assert)((0, util_1.tryGetFromNestedMap)(manager.table.dstDependents, revision, id) === undefined, 0x565 /* TODO: Support multiple dependents per key */);
            (0, util_1.setInNestedMap)(manager.table.dstDependents, revision, id, fieldData);
        }
    }
    if (manager.fieldInvalidated) {
        manager.table.invalidatedFields.add(fieldData);
    }
}
function makeModularChangeset(changes, maxId = -1, revisions = undefined, constraintViolationCount = undefined) {
    const changeset = { fieldChanges: changes };
    if (revisions !== undefined && revisions.length > 0) {
        changeset.revisions = revisions;
    }
    if (maxId >= 0) {
        changeset.maxId = (0, util_1.brand)(maxId);
    }
    if (constraintViolationCount !== undefined && constraintViolationCount > 0) {
        changeset.constraintViolationCount = constraintViolationCount;
    }
    return changeset;
}
/**
 * @sealed
 * @alpha
 */
class ModularEditBuilder extends core_1.EditBuilder {
    constructor(family, changeReceiver, anchors) {
        super(family, changeReceiver, anchors);
        this.transactionDepth = 0;
        this.idAllocator = (0, crossFieldQueries_1.idAllocatorFromMaxId)();
    }
    enterTransaction() {
        this.transactionDepth += 1;
        if (this.transactionDepth === 1) {
            this.idAllocator = (0, crossFieldQueries_1.idAllocatorFromMaxId)();
        }
    }
    exitTransaction() {
        (0, common_utils_1.assert)(this.transactionDepth > 0, 0x5b9 /* Cannot exit inexistent transaction */);
        this.transactionDepth -= 1;
        if (this.transactionDepth === 0) {
            this.idAllocator = (0, crossFieldQueries_1.idAllocatorFromMaxId)();
        }
    }
    apply(change) {
        this.applyChange(change);
    }
    /**
     * Adds a change to the edit builder
     * @param field - the field which is being edited
     * @param fieldKind - the kind of the field
     * @param change - the change to the field
     * @param maxId - the highest `ChangesetLocalId` used in this change
     */
    submitChange(field, fieldKind, change, maxId = (0, util_1.brand)(-1)) {
        const changeMap = this.buildChangeMap(field, fieldKind, change);
        this.applyChange(makeModularChangeset(changeMap, maxId));
    }
    submitChanges(changes, maxId = (0, util_1.brand)(-1)) {
        const changeMaps = changes.map((change) => (0, core_1.makeAnonChange)(makeModularChangeset(this.buildChangeMap(change.field, change.fieldKind, change.change))));
        const composedChange = this.changeFamily.rebaser.compose(changeMaps);
        if (maxId >= 0) {
            composedChange.maxId = maxId;
        }
        this.applyChange(composedChange);
    }
    generateId(count) {
        return this.idAllocator(count);
    }
    buildChangeMap(field, fieldKind, change) {
        let fieldChangeMap = new Map([[field.field, { fieldKind, change }]]);
        let remainingPath = field.parent;
        while (remainingPath !== undefined) {
            const nodeChange = { fieldChanges: fieldChangeMap };
            const fieldChange = genericFieldKind_1.genericFieldKind.changeHandler.editor.buildChildChange(remainingPath.parentIndex, nodeChange);
            fieldChangeMap = new Map([
                [
                    remainingPath.parentField,
                    { fieldKind: genericFieldKind_1.genericFieldKind.identifier, change: (0, util_1.brand)(fieldChange) },
                ],
            ]);
            remainingPath = remainingPath.parent;
        }
        return fieldChangeMap;
    }
    setValue(path, value) {
        const valueChange = value === undefined ? {} : { value };
        const nodeChange = { valueChange };
        const fieldChange = genericFieldKind_1.genericFieldKind.changeHandler.editor.buildChildChange(path.parentIndex, nodeChange);
        this.submitChange({ parent: path.parent, field: path.parentField }, genericFieldKind_1.genericFieldKind.identifier, (0, util_1.brand)(fieldChange));
    }
    addValueConstraint(path, currentValue) {
        const nodeChange = {
            valueConstraint: { value: currentValue, violated: false },
        };
        const fieldChange = genericFieldKind_1.genericFieldKind.changeHandler.editor.buildChildChange(path.parentIndex, nodeChange);
        this.submitChange({ parent: path.parent, field: path.parentField }, genericFieldKind_1.genericFieldKind.identifier, (0, util_1.brand)(fieldChange));
    }
}
exports.ModularEditBuilder = ModularEditBuilder;
function getRevInfoFromTaggedChanges(changes) {
    var _a;
    let maxId = -1;
    const revInfos = [];
    for (const taggedChange of changes) {
        const change = taggedChange.change;
        maxId = Math.max((_a = change.maxId) !== null && _a !== void 0 ? _a : -1, maxId);
        revInfos.push(...revisionInfoFromTaggedChange(taggedChange));
    }
    return { maxId: (0, util_1.brand)(maxId), revInfos };
}
function revisionInfoFromTaggedChange(taggedChange) {
    const revInfos = [];
    if (taggedChange.change.revisions !== undefined) {
        revInfos.push(...taggedChange.change.revisions);
    }
    else if (taggedChange.revision !== undefined) {
        const info = { revision: taggedChange.revision };
        if (taggedChange.rollbackOf !== undefined) {
            info.rollbackOf = taggedChange.rollbackOf;
        }
        revInfos.push(info);
    }
    return revInfos;
}
//# sourceMappingURL=modularChangeFamily.js.map