"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _SharedTreeBranch_events;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onForkTransitive = exports.SharedTreeBranch = void 0;
const client_utils_1 = require("@fluid-internal/client-utils");
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const index_js_1 = require("../core/index.js");
const index_js_2 = require("../util/index.js");
/**
 * A branch of changes that can be applied to a SharedTree.
 */
class SharedTreeBranch {
    /**
     * Construct a new branch.
     * @param head - the head of the branch
     * @param changeFamily - determines the set of changes that this branch can commit
     * @param mintRevisionTag - used to generate a `RevisionTag` for each change.
     * @param branchTrimmer - an optional event emitter that informs the branch it has been trimmed. If this is not supplied, then the branch must
     * never be trimmed. See {@link BranchTrimmingEvents} for details on trimming.
     */
    constructor(head, changeFamily, mintRevisionTag, branchTrimmer, telemetryEventBatcher) {
        this.head = head;
        this.changeFamily = changeFamily;
        this.mintRevisionTag = mintRevisionTag;
        this.branchTrimmer = branchTrimmer;
        this.telemetryEventBatcher = telemetryEventBatcher;
        _SharedTreeBranch_events.set(this, (0, client_utils_1.createEmitter)());
        this.events = __classPrivateFieldGet(this, _SharedTreeBranch_events, "f");
        this.disposed = false;
        this.editor = this.changeFamily.buildEditor(mintRevisionTag, (change) => this.apply(change));
        this.unsubscribeBranchTrimmer = branchTrimmer?.on("ancestryTrimmed", (commit) => {
            __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("ancestryTrimmed", commit);
        });
    }
    /**
     * Sets the head of this branch.
     * @remarks This is a "manual override" of sorts, for when the branch needs to be set to a certain state without going through the usual flow of edits.
     * This might be necessary as a performance optimization, or to prevent parts of the system updating incorrectly (this method emits no change events!).
     */
    setHead(head) {
        this.assertNotDisposed();
        this.head = head;
    }
    /**
     * Apply a change to this branch.
     * @param change - the change to apply
     * @param kind - the kind of change to apply
     * @returns the change that was applied and the new head commit of the branch
     */
    apply(change, kind = index_js_1.CommitKind.Default) {
        this.assertNotDisposed();
        const revisionTag = change.revision;
        (0, internal_1.assert)(revisionTag !== undefined, 0xa49 /* Revision tag must be provided */);
        const newHead = (0, index_js_1.mintCommit)(this.head, {
            revision: revisionTag,
            change: change.change,
        });
        const changeEvent = {
            type: "append",
            kind,
            change,
            newCommits: [newHead],
        };
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("beforeChange", changeEvent);
        this.head = newHead;
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("afterChange", changeEvent);
    }
    /**
     * Gets the commit at the head of this branch.
     */
    getHead() {
        return this.head;
    }
    /**
     * Spawn a new branch that is based off of the current state of this branch.
     * @param commit - The commit to base the new branch off of. Defaults to the head of this branch.
     * @param mintRevisionTag - used to generate a `RevisionTag` for each change.
     * @remarks Changes made to the new branch will not be applied to this branch until the new branch is {@link SharedTreeBranch.merge | merged} back in.
     * Forks created during a transaction will be disposed when the transaction ends.
     */
    fork(commit = this.head, mintRevisionTag = this.mintRevisionTag) {
        this.assertNotDisposed();
        const fork = new SharedTreeBranch(commit, this.changeFamily, mintRevisionTag, this.branchTrimmer);
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("fork", fork);
        return fork;
    }
    /**
     * Rebase the changes that have been applied to this branch over divergent changes in the given branch.
     * After this operation completes, this branch will be based off of `branch`.
     *
     * @param branch - the branch to rebase onto
     * @param upTo - the furthest commit on `branch` over which to rebase (inclusive). Defaults to the head commit of `branch`.
     * @returns the result of the rebase or undefined if nothing changed
     */
    rebaseOnto(branch, upTo = branch.getHead()) {
        this.assertNotDisposed();
        // Rebase this branch onto the given branch
        const rebaseResult = this.rebaseBranch(this, branch, upTo);
        if (rebaseResult === undefined) {
            return;
        }
        // The net change to this branch is provided by the `rebaseBranch` API
        const { newSourceHead, commits } = rebaseResult;
        const { deletedSourceCommits, targetCommits, sourceCommits } = commits;
        (0, internal_1.assert)((0, index_js_2.hasSome)(targetCommits), 0xa83 /* Expected commit(s) for a non no-op rebase */);
        const newCommits = [...targetCommits, ...sourceCommits];
        const changeEvent = {
            type: "rebase",
            get change() {
                const change = rebaseResult.sourceChange;
                return change === undefined ? undefined : (0, index_js_1.makeAnonChange)(change);
            },
            removedCommits: deletedSourceCommits,
            newCommits,
        };
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("beforeChange", changeEvent);
        this.head = newSourceHead;
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("afterChange", changeEvent);
    }
    /**
     * Remove a range of commits from this branch.
     * @param commit - All commits after (but not including) this commit will be removed.
     * @returns The net change to this branch and the commits that were removed from this branch.
     */
    removeAfter(commit) {
        if (commit === this.head) {
            return;
        }
        const removedCommits = [];
        const inverses = [];
        (0, index_js_1.findAncestor)([this.head, removedCommits], (c) => {
            // TODO: Pull this side effect out if/when more diverse ancestry walking helpers are available
            if (c !== commit) {
                const revision = this.mintRevisionTag();
                const inverse = this.changeFamily.rebaser.invert(c, true, revision);
                inverses.push((0, index_js_1.tagRollbackInverse)(inverse, revision, c.revision));
                return false;
            }
            return true;
        });
        (0, internal_1.assert)((0, index_js_2.hasSome)(removedCommits), 0xa84 /* Commit must be in the branch's ancestry */);
        const change = (0, index_js_1.makeAnonChange)(this.changeFamily.rebaser.compose(inverses));
        const changeEvent = {
            type: "remove",
            change,
            removedCommits,
        };
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("beforeChange", changeEvent);
        this.head = commit;
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("afterChange", changeEvent);
    }
    /**
     * Apply all the divergent changes on the given branch to this branch.
     *
     * @param branch - the branch to merge into this branch
     * @returns the commits that were added to this branch by the merge, or undefined if nothing changed
     */
    merge(branch) {
        this.assertNotDisposed();
        branch.assertNotDisposed();
        if (branch === this) {
            return undefined;
        }
        // Rebase the given branch onto this branch
        const rebaseResult = this.rebaseBranch(branch, this);
        if (rebaseResult === undefined) {
            return undefined;
        }
        // Compute the net change to this branch
        const sourceCommits = rebaseResult.commits.sourceCommits;
        (0, internal_1.assert)((0, index_js_2.hasSome)(sourceCommits), 0xa86 /* Expected source commits in non no-op merge */);
        const { rebaser } = this.changeFamily;
        const changeEvent = (0, index_js_2.defineLazyCachedProperty)({
            type: "append",
            kind: index_js_1.CommitKind.Default,
            newCommits: sourceCommits,
        }, "change", () => (0, index_js_1.makeAnonChange)(rebaser.compose(sourceCommits)));
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("beforeChange", changeEvent);
        this.head = rebaseResult.newSourceHead;
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("afterChange", changeEvent);
        return { sourceCommits };
    }
    /** Rebase `branchHead` onto `onto`, but return undefined if nothing changed */
    rebaseBranch(branch, onto, upTo = onto.getHead()) {
        const { head } = branch;
        if (head === upTo) {
            return undefined;
        }
        const { duration, output } = (0, internal_2.measure)(() => (0, index_js_1.rebaseBranch)(this.mintRevisionTag, this.changeFamily.rebaser, head, upTo, onto.getHead()));
        this.telemetryEventBatcher?.accumulateAndLog({ duration, ...output.telemetryProperties });
        if (this.head === output.newSourceHead) {
            return undefined;
        }
        return output;
    }
    /**
     * Dispose this branch, freezing its state.
     *
     * @remarks
     * Attempts to further mutate the branch will error.
     * Any transactions in progress will be aborted.
     * Calling dispose more than once has no effect.
     */
    dispose() {
        if (this.disposed) {
            return;
        }
        this.unsubscribeBranchTrimmer?.();
        this.disposed = true;
        __classPrivateFieldGet(this, _SharedTreeBranch_events, "f").emit("dispose");
    }
    assertNotDisposed() {
        (0, internal_1.assert)(!this.disposed, 0x66e /* Branch is disposed */);
    }
}
exports.SharedTreeBranch = SharedTreeBranch;
_SharedTreeBranch_events = new WeakMap();
/**
 * Registers an event listener that fires when the given branch forks.
 * The listener will also fire when any of those forks fork, and when those forks of forks fork, and so on.
 * @param branch - the branch that will be listened to for forks
 * @param onFork - the fork event listener
 * @returns a function which when called will deregister all registrations (including transitive) created by this function.
 * The deregister function has undefined behavior if called more than once.
 */
// Branches are invariant over TChange
function onForkTransitive(branch, onFork) {
    const offs = [];
    offs.push(branch.events.on("fork", (fork) => {
        offs.push(onForkTransitive(fork, onFork));
        onFork(fork);
    }));
    return () => {
        for (const off of offs) {
            off();
        }
    };
}
exports.onForkTransitive = onForkTransitive;
//# sourceMappingURL=branch.js.map