/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Brand, RecursiveReadonly } from "../util";
import { AnchorSet, ChangeFamily, ChangeFamilyEditor, GraphCommit, IRepairDataStoreProvider, RevisionTag, SessionId, SimpleDependee } from "../core";
import { ISubscribable } from "../events";
import { SharedTreeBranch } from "./branch";
export declare type SeqNumber = Brand<number, "edit-manager.SeqNumber">;
/**
 * Contains a single change to the `SharedTree` and associated metadata
 */
export interface Commit<TChangeset> extends Omit<GraphCommit<TChangeset>, "parent"> {
    /** An identifier representing the session/user/client that made this commit */
    readonly sessionId: SessionId;
}
/**
 * A commit with a sequence number but no parentage; used for serializing the `EditManager` into a summary
 */
export interface SequencedCommit<TChangeset> extends Commit<TChangeset> {
    sequenceNumber: SeqNumber;
}
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 trunk commits */
    private readonly trunkMetadata;
    /** A map from a sequence number to the commit in the trunk which has that sequence number */
    private readonly sequenceMap;
    /** The {@link UndoRedoManager} associated with the trunk */
    private readonly trunkUndoRedoManager;
    /**
     * 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>;
    /** The {@link UndoRedoManager} associated with the local branch. */
    private readonly localBranchUndoRedoManager;
    /**
     * Tracks where on the trunk all registered branches are based. Each key is the sequence number 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 */
    private minimumSequenceNumber;
    /**
     * An immutable "origin" commit singleton on which the trunk is based.
     * This makes it possible to model the trunk in the same way as any other branch
     * (it branches off of a base commit) which simplifies some logic.
     */
    private readonly 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
     * @param repairDataStoreProvider - used for undoing/redoing the local branch
     * @param anchors - an optional set of anchors to be rebased by the local branch when it changes
     */
    constructor(changeFamily: TChangeFamily, localSessionId: SessionId, repairDataStoreProvider: IRepairDataStoreProvider, anchors?: AnchorSet);
    /**
     * 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.
     * @param branch - the branch to register. All branches that fork from this branch, directly or transitively,
     * will also be registered.
     */
    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
     */
    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>[];
    addSequencedChange(newCommit: Commit<TChangeset>, sequenceNumber: SeqNumber, referenceSequenceNumber: SeqNumber): void;
    findLocalCommit(revision: RevisionTag): [commit: GraphCommit<TChangeset>, commitsAfter: GraphCommit<TChangeset>[]];
    private pushToTrunk;
}
/**
 * A branch off of the trunk for use in summaries
 */
export interface SummarySessionBranch<TChangeset> {
    readonly base: RevisionTag;
    readonly commits: Commit<TChangeset>[];
}
/**
 * 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