"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runSynchronous = exports.TreeCheckout = exports.createTreeCheckout = 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("../events/index.js");
const index_js_4 = require("../feature-libraries/index.js");
const index_js_5 = require("../shared-tree-core/index.js");
const index_js_6 = require("../util/index.js");
const sharedTreeChangeFamily_js_1 = require("./sharedTreeChangeFamily.js");
/**
 * Creates a {@link TreeCheckout}.
 * @param args - an object containing optional components that will be used to build the view.
 * Any components not provided will be created by default.
 * @remarks This does not create a {@link SharedTree}, but rather a view with the minimal state
 * and functionality required to implement {@link ITreeCheckout}.
 */
function createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, args) {
    const forest = args?.forest ?? (0, index_js_4.buildForest)();
    const schema = args?.schema ?? new index_js_2.TreeStoredSchemaRepository();
    const defaultCodecOptions = { jsonValidator: index_js_1.noopValidator };
    const defaultFieldBatchVersion = 1;
    const changeFamily = args?.changeFamily ??
        new sharedTreeChangeFamily_js_1.SharedTreeChangeFamily(revisionTagCodec, args?.fieldBatchCodec ??
            (0, index_js_4.makeFieldBatchCodec)(defaultCodecOptions, defaultFieldBatchVersion), { jsonValidator: index_js_1.noopValidator }, args?.chunkCompressionStrategy);
    const branch = args?.branch ??
        new index_js_5.SharedTreeBranch({
            change: changeFamily.rebaser.compose([]),
            revision: "root",
        }, changeFamily, () => idCompressor.generateCompressedId());
    const events = args?.events ?? (0, index_js_3.createEmitter)();
    const transaction = new Transaction(branch);
    return new TreeCheckout(transaction, branch, changeFamily, schema, forest, events, mintRevisionTag, revisionTagCodec, args?.removedRoots);
}
exports.createTreeCheckout = createTreeCheckout;
class Transaction {
    constructor(branch) {
        this.branch = branch;
    }
    start() {
        this.branch.startTransaction();
        this.branch.editor.enterTransaction();
    }
    commit() {
        this.branch.commitTransaction();
        this.branch.editor.exitTransaction();
        return index_js_6.TransactionResult.Commit;
    }
    abort() {
        this.branch.abortTransaction();
        this.branch.editor.exitTransaction();
        return index_js_6.TransactionResult.Abort;
    }
    inProgress() {
        return this.branch.isTransacting();
    }
}
/**
 * An implementation of {@link ITreeCheckoutFork}.
 */
class TreeCheckout {
    constructor(transaction, branch, changeFamily, storedSchema, forest, events, mintRevisionTag, revisionTagCodec, removedRoots = (0, index_js_2.makeDetachedFieldIndex)("repair", revisionTagCodec)) {
        this.transaction = transaction;
        this.branch = branch;
        this.changeFamily = changeFamily;
        this.storedSchema = storedSchema;
        this.forest = forest;
        this.events = events;
        this.mintRevisionTag = mintRevisionTag;
        this.revisionTagCodec = revisionTagCodec;
        this.removedRoots = removedRoots;
        this.isDisposed = false;
        /**
         * Set of revertibles maintained for automatic disposal
         */
        this.revertibles = new Set();
        /**
         * Each branch's head commit corresponds to a revertible commit.
         * Maintaining a whole branch ensures the commit graph is not pruned in a way that would prevent the commit from
         * being reverted.
         */
        this.revertibleCommitBranches = new Map();
        // We subscribe to `beforeChange` rather than `afterChange` here because it's possible that the change is invalid WRT our forest.
        // For example, a bug in the editor might produce a malformed change object and thus applying the change to the forest will throw an error.
        // In such a case we will crash here, preventing the change from being added to the commit graph, and preventing `afterChange` from firing.
        // One important consequence of this is that we will not submit the op containing the invalid change, since op submissions happens in response to `afterChange`.
        branch.on("beforeChange", (event) => {
            if (event.change !== undefined) {
                // Conflicts due to schema will be empty and thus are not applied.
                for (const change of event.change.change.changes) {
                    if (change.type === "data") {
                        const delta = (0, index_js_4.intoDelta)((0, index_js_2.tagChange)(change.innerChange, event.change.revision));
                        this.withCombinedVisitor((visitor) => {
                            (0, index_js_2.visitDelta)(delta, visitor, this.removedRoots);
                        });
                    }
                    else if (change.type === "schema") {
                        // Schema changes from a current to a new schema are expected to be backwards compatible.
                        // This guarantees that all data in the forest (which is valid before the schema change)
                        // is also valid under the new schema.
                        // Note however, that such schema changes may in some cases be rolled back:
                        // Case 1: A transaction with a schema change may be aborted.
                        // The transaction may have made some data changes that would render some trees invalid
                        // under the old schema, but these changes will also be rolled back, thereby putting the forest
                        // back in the state before the transaction, which is valid under the original (reinstated) schema.
                        // Case 2: A branch with a schema change may be rebased such that the schema change (because
                        // of a constraint) is no longer applied.
                        // Such a branch may contain data changes that would render some trees invalid under the
                        // original schema. These data changes may not necessarily be rolled back.
                        // They will however be rebased over the rollback of the schema change. This rebasing will
                        // ensure that these data changes are muted if they would render some trees invalid under the
                        // original (reinstated) schema.
                        storedSchema.apply(change.innerChange.schema.new);
                    }
                    else {
                        (0, index_js_6.fail)("Unknown Shared Tree change type.");
                    }
                }
                this.events.emit("afterBatch");
            }
            if (event.type === "replace" && (0, index_js_5.getChangeReplaceType)(event) === "transactionCommit") {
                const transactionRevision = event.newCommits[0].revision;
                for (const transactionStep of event.removedCommits) {
                    this.removedRoots.updateMajor(transactionStep.revision, transactionRevision);
                }
            }
        });
        branch.on("commitApplied", (data) => {
            const commit = branch.getHead();
            const { change, revision } = commit;
            let withinEventContext = true;
            const getRevertible = (0, sharedTreeChangeFamily_js_1.hasSchemaChange)(change)
                ? undefined
                : (onRevertibleDisposed) => {
                    (0, internal_1.assert)(withinEventContext, 0x902 /* cannot get a revertible outside of the context of a commitApplied event */);
                    (0, internal_1.assert)(this.revertibleCommitBranches.get(revision) === undefined, 0x903 /* cannot get the revertible more than once */);
                    const revertibleCommits = this.revertibleCommitBranches;
                    const revertible = {
                        get status() {
                            const revertibleCommit = revertibleCommits.get(revision);
                            return revertibleCommit === undefined
                                ? index_js_2.RevertibleStatus.Disposed
                                : index_js_2.RevertibleStatus.Valid;
                        },
                        revert: (release = true) => {
                            (0, internal_1.assert)(revertible.status === index_js_2.RevertibleStatus.Valid, 0x904 /* a disposed revertible cannot be reverted */);
                            this.revertRevertible(revision, data.kind);
                            if (release) {
                                revertible[index_js_6.disposeSymbol]();
                            }
                        },
                        [index_js_6.disposeSymbol]: () => revertible.dispose(),
                        dispose: () => {
                            (0, internal_1.assert)(revertible.status === index_js_2.RevertibleStatus.Valid, 0x910 /* a disposed revertible cannot be disposed */);
                            this.disposeRevertible(revertible, revision);
                            onRevertibleDisposed?.(revertible);
                        },
                    };
                    this.revertibleCommitBranches.set(revision, branch.fork());
                    this.revertibles.add(revertible);
                    return revertible;
                };
            this.events.emit("commitApplied", data, getRevertible);
            withinEventContext = false;
        });
    }
    withCombinedVisitor(fn) {
        const anchorVisitor = this.forest.anchors.acquireVisitor();
        const combinedVisitor = (0, index_js_2.combineVisitors)([this.forest.acquireVisitor(), anchorVisitor], [anchorVisitor]);
        fn(combinedVisitor);
        combinedVisitor.free();
    }
    checkNotDisposed() {
        (0, internal_1.assert)(!this.isDisposed, 0x911 /* Invalid operation on a disposed TreeCheckout */);
    }
    get rootEvents() {
        return this.forest.anchors;
    }
    get editor() {
        this.checkNotDisposed();
        return this.branch.editor;
    }
    locate(anchor) {
        this.checkNotDisposed();
        return this.forest.anchors.locate(anchor);
    }
    fork() {
        this.checkNotDisposed();
        const anchors = new index_js_2.AnchorSet();
        const branch = this.branch.fork();
        const storedSchema = this.storedSchema.clone();
        const forest = this.forest.clone(storedSchema, anchors);
        const transaction = new Transaction(branch);
        return new TreeCheckout(transaction, branch, this.changeFamily, storedSchema, forest, (0, index_js_3.createEmitter)(), this.mintRevisionTag, this.revisionTagCodec, this.removedRoots.clone());
    }
    rebase(view) {
        this.checkNotDisposed();
        view.branch.rebaseOnto(this.branch);
    }
    rebaseOnto(view) {
        this.checkNotDisposed();
        view.rebase(this);
    }
    merge(view, disposeView = true) {
        this.checkNotDisposed();
        (0, internal_1.assert)(!this.transaction.inProgress() || disposeView, 0x710 /* A view that is merged into an in-progress transaction must be disposed */);
        while (view.transaction.inProgress()) {
            view.transaction.commit();
        }
        this.branch.merge(view.branch);
        if (disposeView) {
            view[index_js_6.disposeSymbol]();
        }
    }
    updateSchema(newSchema) {
        this.checkNotDisposed();
        this.editor.schema.setStoredSchema(this.storedSchema.clone(), newSchema);
    }
    [index_js_6.disposeSymbol]() {
        this.checkNotDisposed();
        this.isDisposed = true;
        this.purgeRevertibles();
        this.branch.dispose();
    }
    getRemovedRoots() {
        const trees = [];
        const cursor = this.forest.allocateCursor();
        for (const { id, root } of this.removedRoots.entries()) {
            const parentField = this.removedRoots.toFieldKey(root);
            this.forest.moveCursorToPath({ parent: undefined, parentField, parentIndex: 0 }, cursor);
            const tree = (0, index_js_4.jsonableTreeFromCursor)(cursor);
            if (tree !== undefined) {
                // This method is used for tree consistency comparison.
                const { major, minor } = id;
                const finalizedMajor = major !== undefined ? this.revisionTagCodec.encode(major) : major;
                trees.push([finalizedMajor, minor, tree]);
            }
        }
        cursor.free();
        return trees;
    }
    purgeRevertibles() {
        for (const revertible of this.revertibles) {
            revertible.dispose();
        }
    }
    disposeRevertible(revertible, revision) {
        this.revertibleCommitBranches.get(revision)?.dispose();
        this.revertibleCommitBranches.delete(revision);
        this.revertibles.delete(revertible);
    }
    revertRevertible(revision, kind) {
        (0, internal_1.assert)(!this.branch.isTransacting(), 0x7cb /* Undo is not yet supported during transactions */);
        const revertibleBranch = this.revertibleCommitBranches.get(revision);
        (0, internal_1.assert)(revertibleBranch !== undefined, 0x7cc /* expected to find a revertible commit */);
        const commitToRevert = revertibleBranch.getHead();
        let change = this.changeFamily.rebaser.invert((0, index_js_2.tagChange)(commitToRevert.change, revision), false);
        const headCommit = this.branch.getHead();
        // Rebase the inverted change onto any commits that occurred after the undoable commits.
        if (commitToRevert !== headCommit) {
            change = (0, index_js_2.rebaseChange)(this.changeFamily.rebaser, change, commitToRevert, headCommit, this.mintRevisionTag);
        }
        this.branch.apply(change, this.mintRevisionTag(), kind === index_js_2.CommitKind.Default || kind === index_js_2.CommitKind.Redo
            ? index_js_2.CommitKind.Undo
            : index_js_2.CommitKind.Redo);
    }
}
exports.TreeCheckout = TreeCheckout;
/**
 * Run a synchronous transaction on the given shared tree view.
 * This is a convenience helper around the {@link SharedTreeFork#transaction} APIs.
 * @param view - the view on which to run the transaction
 * @param transaction - the transaction function. This will be executed immediately. It is passed `view` as an argument for convenience.
 * If this function returns an `Abort` result then the transaction will be aborted. Otherwise, it will be committed.
 * @returns whether or not the transaction was committed or aborted
 * @internal
 */
function runSynchronous(view, transaction) {
    view.transaction.start();
    const result = transaction(view);
    return result === index_js_6.TransactionResult.Abort
        ? view.transaction.abort()
        : view.transaction.commit();
}
exports.runSynchronous = runSynchronous;
//# sourceMappingURL=treeCheckout.js.map