"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.findCommonAncestor = exports.findAncestor = exports.rebaseChangeOverChanges = exports.revisionMetadataSourceFromInfo = exports.rebaseChange = exports.rebaseBranch = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const changeRebaser_1 = require("./changeRebaser");
const types_1 = require("./types");
function rebaseBranch(changeRebaser, sourceHead, targetCommit, targetHead = targetCommit) {
    // Get both source and target as path arrays
    const sourcePath = [];
    const targetPath = [];
    const ancestor = findCommonAncestor([sourceHead, sourcePath], [targetHead, targetPath]);
    (0, core_utils_1.assert)(ancestor !== undefined, 0x675 /* branches must be related */);
    // Find where `targetCommit` is in the target branch
    const targetCommitIndex = targetPath.findIndex((r) => r === targetCommit);
    if (targetCommitIndex === -1) {
        // If the targetCommit is not in the target path, then it is either disjoint from `target` or it is behind/at
        // the commit where source and target diverge (ancestor), in which case there is nothing more to rebase
        // TODO: Ideally, this would be an "assertExpensive"
        (0, core_utils_1.assert)(findCommonAncestor(targetCommit, targetHead) !== undefined, 0x676 /* target commit is not in target branch */);
        return {
            newSourceHead: sourceHead,
            sourceChange: undefined,
            commits: { deletedSourceCommits: [], targetCommits: [], sourceCommits: sourcePath },
        };
    }
    // Iterate through the target path and look for commits that are also present on the source branch (i.e. they
    // have matching tags). Each commit found in the target branch can be skipped when processing the source branch
    // because it has already been rebased onto the target. In the case that one or more of these commits are present
    // directly after `targetCommit`, then the new base can be advanced further without having to do any work.
    const sourceSet = new Set(sourcePath.map((r) => r.revision));
    let newBaseIndex = targetCommitIndex;
    for (let i = 0; i < targetPath.length; i += 1) {
        const { revision } = targetPath[i];
        if (sourceSet.has(revision)) {
            sourceSet.delete(revision);
            newBaseIndex = Math.max(newBaseIndex, i);
        }
        else if (i > targetCommitIndex) {
            break;
        }
    }
    /** The commit on the target branch that the new source branch branches off of (i.e. the new common ancestor) */
    const newBase = targetPath[newBaseIndex];
    // Figure out how much of the trunk to start rebasing over.
    const targetCommits = targetPath.slice(0, newBaseIndex + 1);
    const deletedSourceCommits = [...sourcePath];
    // If the source and target rebase path begin with a range that has all the same revisions, remove it; it is
    // equivalent on both branches and doesn't need to be rebased.
    const targetRebasePath = [...targetCommits];
    const minLength = Math.min(sourcePath.length, targetRebasePath.length);
    for (let i = 0; i < minLength; i++) {
        if (sourcePath[0].revision === targetRebasePath[0].revision) {
            sourcePath.shift();
            targetRebasePath.shift();
        }
    }
    const sourceCommits = [];
    // If all commits that are about to be rebased over on the target branch already comprise the start of the source branch,
    // are in the same order, and have no other commits interleaving them, then no rebasing needs to occur. Those commits can
    // simply be removed from the source branch, and the remaining commits on the source branch are reparented off of the new
    // base commit.
    if (targetRebasePath.length === 0) {
        for (const c of sourcePath) {
            sourceCommits.push((0, types_1.mintCommit)(sourceCommits[sourceCommits.length - 1] ?? newBase, c));
        }
        return {
            newSourceHead: sourceCommits[sourceCommits.length - 1] ?? newBase,
            sourceChange: undefined,
            commits: {
                deletedSourceCommits,
                targetCommits,
                sourceCommits,
            },
        };
    }
    // For each source commit, rebase backwards over the inverses of any commits already rebased, and then
    // rebase forwards over the rest of the commits up to the new base before advancing the new base.
    let newHead = newBase;
    const inverses = [];
    for (const c of sourcePath) {
        if (sourceSet.has(c.revision)) {
            const change = rebaseChangeOverChanges(changeRebaser, c.change, [
                ...inverses,
                ...targetRebasePath,
            ]);
            newHead = {
                revision: c.revision,
                change,
                parent: newHead,
            };
            sourceCommits.push(newHead);
            targetRebasePath.push({ ...c, change });
        }
        inverses.unshift((0, changeRebaser_1.tagRollbackInverse)(changeRebaser.invert(c, true), (0, types_1.mintRevisionTag)(), c.revision));
    }
    let netChange;
    return {
        newSourceHead: newHead,
        get sourceChange() {
            if (netChange === undefined) {
                netChange = changeRebaser.compose([...inverses, ...targetRebasePath]);
            }
            return netChange;
        },
        commits: {
            deletedSourceCommits,
            targetCommits,
            sourceCommits,
        },
    };
}
exports.rebaseBranch = rebaseBranch;
/**
 * "Sandwich/Horseshoe Rebase" a change over the given source and target branches
 * @param changeRebaser - the change rebaser responsible for rebasing the change over the commits in each branch
 * @param change - the change to rebase
 * @param sourceHead - the head of the branch that `change` is based on
 * @param targetHead - the branch to rebase `change` onto
 * @returns the rebased change
 *
 * @remarks inverses will be cached.
 */
function rebaseChange(changeRebaser, change, sourceHead, targetHead) {
    const sourcePath = [];
    const targetPath = [];
    (0, core_utils_1.assert)(findCommonAncestor([sourceHead, sourcePath], [targetHead, targetPath]) !== undefined, 0x576 /* branch A and branch B must be related */);
    const inverses = sourcePath.map((commit) => inverseFromCommit(changeRebaser, commit, true));
    inverses.reverse();
    return rebaseChangeOverChanges(changeRebaser, change, [...inverses, ...targetPath]);
}
exports.rebaseChange = rebaseChange;
/**
 * @alpha
 */
function revisionMetadataSourceFromInfo(revInfos) {
    const getIndex = (revision) => {
        const index = revInfos.findIndex((revInfo) => revInfo.revision === revision);
        return index >= 0 ? index : undefined;
    };
    const tryGetInfo = (revision) => {
        if (revision === undefined) {
            return undefined;
        }
        const index = getIndex(revision);
        return index === undefined ? undefined : revInfos[index];
    };
    const hasRollback = (revision) => {
        return revInfos.find((info) => info.rollbackOf === revision) !== undefined;
    };
    return { getIndex, tryGetInfo, hasRollback };
}
exports.revisionMetadataSourceFromInfo = revisionMetadataSourceFromInfo;
function rebaseChangeOverChanges(changeRebaser, changeToRebase, changesToRebaseOver) {
    const revisionMetadata = revisionMetadataSourceFromInfo(getRevInfoFromTaggedChanges(changesToRebaseOver));
    return changesToRebaseOver.reduce((a, b) => changeRebaser.rebase(a, b, revisionMetadata), changeToRebase);
}
exports.rebaseChangeOverChanges = rebaseChangeOverChanges;
// TODO: Deduplicate
function getRevInfoFromTaggedChanges(changes) {
    const revInfos = [];
    for (const taggedChange of changes) {
        revInfos.push(...revisionInfoFromTaggedChange(taggedChange));
    }
    return revInfos;
}
// TODO: Deduplicate
function revisionInfoFromTaggedChange(taggedChange) {
    const revInfos = [];
    if (taggedChange.revision !== undefined) {
        const info = { revision: taggedChange.revision };
        if (taggedChange.rollbackOf !== undefined) {
            info.rollbackOf = taggedChange.rollbackOf;
        }
        revInfos.push(info);
    }
    return revInfos;
}
function inverseFromCommit(changeRebaser, commit, cache) {
    const inverse = commit.inverse ?? changeRebaser.invert(commit, true);
    if (cache === true && commit.inverse === undefined) {
        commit.inverse = inverse;
    }
    return (0, changeRebaser_1.tagRollbackInverse)(inverse, (0, types_1.mintRevisionTag)(), commit.revision);
}
function findAncestor(descendant, predicate = (t) => t.parent === undefined) {
    let d;
    let path;
    if (Array.isArray(descendant)) {
        [d, path] = descendant;
    }
    else {
        d = descendant;
    }
    for (let cur = d; cur !== undefined; cur = cur.parent) {
        if (predicate(cur)) {
            path?.reverse();
            return cur;
        }
        path?.push(cur);
    }
    if (path !== undefined) {
        path.length = 0;
    }
    return undefined;
}
exports.findAncestor = findAncestor;
/**
 * Find a common ancestor between two descendants that are linked by parent pointers.
 * @param descendantA - a descendant. If an empty `path` array is included, it will be populated
 * with the chain of commits from the ancestor to `descendantA` (not including the ancestor).
 * @param descendantB - another descendant. If an empty `path` array is included, it will be populated
 * with the chain of commits from the ancestor to `descendantB` (not including the ancestor).
 * @returns the common ancestor of `descendantA` and `descendantB`, or `undefined` if no such ancestor exists.
 *
 * @example
 *
 * ```typescript
 * interface Parented {
 *   parent?: Parented;
 * }
 * const shared = {};
 * const a = { parent: shared };
 * const b1 = { parent: shared };
 * const b2 = { parent: b1 };
 * const pathB: Parented[] = []
 * const ancestor = findCommonAncestor<Parented>(a, [b2, pathB]);
 * // ancestor === shared
 * // pathB === [b1, b2]
 * ```
 */
function findCommonAncestor(descendantA, descendantB) {
    let a;
    let b;
    let pathA;
    let pathB;
    if (Array.isArray(descendantA)) {
        [a, pathA] = descendantA;
        (0, core_utils_1.assert)(pathA === undefined || pathA.length === 0, 0x578 /* Path A must be empty */);
    }
    else {
        a = descendantA;
    }
    if (Array.isArray(descendantB)) {
        [b, pathB] = descendantB;
        (0, core_utils_1.assert)(pathB === undefined || pathB.length === 0, 0x579 /* Path B must be empty */);
    }
    else {
        b = descendantB;
    }
    if (a === b) {
        return a;
    }
    const reversePaths = () => {
        pathA?.reverse();
        pathB?.reverse();
    };
    const visited = new Set();
    while (a !== undefined || b !== undefined) {
        if (a !== undefined) {
            if (visited.has(a)) {
                if (pathB !== undefined) {
                    pathB.length = pathB.findIndex((r) => Object.is(r, a));
                }
                reversePaths();
                return a;
            }
            visited.add(a);
            pathA?.push(a);
            a = a.parent;
        }
        if (b !== undefined) {
            if (visited.has(b)) {
                if (pathA !== undefined) {
                    pathA.length = pathA.findIndex((r) => Object.is(r, b));
                }
                reversePaths();
                return b;
            }
            visited.add(b);
            pathB?.push(b);
            b = b.parent;
        }
    }
    if (pathA !== undefined) {
        pathA.length = 0;
    }
    if (pathB !== undefined) {
        pathB.length = 0;
    }
    return undefined;
}
exports.findCommonAncestor = findCommonAncestor;
//# sourceMappingURL=utils.js.map