/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { RecursiveReadonly } from "../util";
import { ChangeFamily, ChangeFamilyEditor, GraphCommit, RevisionTag, SessionId, SimpleDependee } from "../core";
import { ISubscribable } from "../events";
import { SharedTreeBranch } from "./branch";
import { Commit, SeqNumber, SequencedCommit, SummarySessionBranch } from "./editManagerFormat";
export declare const minimumPossibleSequenceNumber: SeqNumber;
export interface EditManagerEvents<TChangeset> {
    /**
     * Fired every time that a new commit is added to the trunk
     * @param newHead - the new head of the trunk
     */
    newTrunkHead(newHead: GraphCommit<TChangeset>): void;
}
/**
 * Represents a local branch of a document and interprets the effect on the document of adding sequenced changes,
 * which were based on a given session's branch, to the document history
 */
export declare class EditManager<TEditor extends ChangeFamilyEditor, TChangeset, TChangeFamily extends ChangeFamily<TEditor, TChangeset>> extends SimpleDependee implements ISubscribable<EditManagerEvents<TChangeset>> {
    readonly changeFamily: TChangeFamily;
    readonly localSessionId: SessionId;
    /** The "trunk" branch. The trunk represents the list of received sequenced changes. */
    private readonly trunk;
    /**
     * Records extra data associated with commits in the {@link trunk}.
     * This does not include an entry for the {@link trunkBase}.
     */
    private readonly trunkMetadata;
    /**
     * A map from a sequence id to the commit in the {@link trunk} which has that sequence id.
     * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
     */
    private readonly sequenceMap;
    /**
     * Branches are maintained to represent the local change list that the issuing client had
     * at the time of submitting the latest known edit on the branch.
     * This means the head commit of each branch is always in its original (non-rebased) form.
     */
    private readonly peerLocalBranches;
    /**
     * This branch holds the changes made by this client which have not yet been confirmed as sequenced changes.
     */
    readonly localBranch: SharedTreeBranch<TEditor, TChangeset>;
    /**
     * Tracks where on the trunk all registered branches are based. Each key is the sequence id of a commit on
     * the trunk, and the value is the set of all branches who have that commit as their common ancestor with the trunk.
     */
    private readonly trunkBranches;
    /**
     * The sequence number of the newest commit on the trunk that has been received by all peers.
     * Defaults to {@link minimumPossibleSequenceNumber} if no commits have been received.
     *
     * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
     */
    private minimumSequenceNumber;
    /**
     * A special commit that is a "base" (tail) of the trunk, though not part of the trunk itself.
     * This makes it possible to model the trunk in the same way as any other branch (it branches off of a base commit)
     * which allows it to use branching APIs to interact with the other branches.
     * Each time trunk eviction occurs, the most recent evicted commit becomes the new `trunkBase`.
     */
    private trunkBase;
    private readonly events;
    on<K extends keyof EditManagerEvents<TChangeset>>(eventName: K, listener: EditManagerEvents<TChangeset>[K]): () => void;
    /**
     * @param changeFamily - the change family of changes on the trunk and local branch
     * @param localSessionId - the id of the local session that will be used for local commits
     */
    constructor(changeFamily: TChangeFamily, localSessionId: SessionId);
    /**
     * Make the given branch known to the `EditManager`. The `EditManager` will ensure that all registered
     * branches remain usable even as the minimum sequence number advances.
     */
    private registerBranch;
    /**
     * Advances the minimum sequence number, and removes all commits from the trunk which lie outside the collaboration window.
     * @param minimumSequenceNumber - the sequence number of the newest commit that all peers (including this one) have received and applied to their trunks.
     *
     * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
     */
    advanceMinimumSequenceNumber(minimumSequenceNumber: SeqNumber): void;
    /**
     * Examines the latest known minimum sequence number and the trunk bases of any registered branches to determine
     * if any commits on the trunk are unreferenced and unneeded for future computation; those found are evicted from the trunk.
     * @returns the number of commits that were removed from the trunk
     */
    private trimTrunk;
    isEmpty(): boolean;
    getSummaryData(): SummaryData<TChangeset>;
    loadSummaryData(data: SummaryData<TChangeset>): void;
    getTrunkChanges(): readonly RecursiveReadonly<TChangeset>[];
    getTrunkHead(): GraphCommit<TChangeset>;
    getLocalChanges(): readonly RecursiveReadonly<TChangeset>[];
    /**
     * @returns The length of the longest branch maintained by this EditManager.
     * This may be the length of a peer branch or the local branch.
     * The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would
     * have length zero.
     */
    getLongestBranchLength(): number;
    addSequencedChange(newCommit: Commit<TChangeset>, sequenceNumber: SeqNumber, referenceSequenceNumber: SeqNumber): void;
    findLocalCommit(revision: RevisionTag): [commit: GraphCommit<TChangeset>, commitsAfter: GraphCommit<TChangeset>[]];
    private pushToTrunk;
    /**
     * Finds the most recent trunk commit that was sequenced at or before the given point.
     * @param searchBy - the sequence number or the sequence id to search for
     * @remarks Fails if there is no eligible commit.
     * @returns the closest commit and its sequence id
     */
    private getClosestTrunkCommit;
    private getBatch;
}
/**
 * The in-memory data that summaries contain
 */
export interface SummaryData<TChangeset> {
    readonly trunk: readonly SequencedCommit<TChangeset>[];
    readonly branches: ReadonlyMap<SessionId, SummarySessionBranch<TChangeset>>;
}
//# sourceMappingURL=editManager.d.ts.map