/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { FieldKey, ProtoNodes, UpPath } from "../../core";
import { Events, ISubscribable } from "../../events";
import { EditableTree } from "./editableTreeTypes";
/**
 * Binder events reflecting atomic data operations
 * @alpha
 */
export interface OperationBinderEvents {
    delete(context: DeleteBindingContext): void;
    insert(context: InsertBindingContext): void;
    batch(context: BatchBindingContext): void;
}
/**
 * Binder events signaling state invalidation
 * @alpha
 */
export interface InvalidationBinderEvents {
    invalidation(context: InvalidationBindingContext): void;
}
/**
 * Compare function, generic.
 *
 * @alpha
 */
export type CompareFunction<T> = (a: T, b: T) => number;
/**
 * Compare function for binder events.
 *
 * @alpha
 */
export type BinderEventsCompare = CompareFunction<VisitorBindingContext>;
/**
 * Compare function for anchors.
 *
 * @alpha
 */
export type AnchorsCompare = CompareFunction<UpPath>;
/**
 * Options to configure binder behavior.
 *
 * TODO: add more options:
 * `filterFn?: (context: BindingContext) => boolean;`
 * `pathPolicy?: "relative" | "absolute";`
 * @alpha
 */
export interface BinderOptions {
    sortFn?: BinderEventsCompare;
}
/**
 * Specialized binder options for flushable binders.
 *
 * @alpha
 */
export interface FlushableBinderOptions<E extends Events<E>> extends BinderOptions {
    autoFlush: boolean;
    autoFlushPolicy: keyof Events<E>;
    sortAnchorsFn?: AnchorsCompare;
}
/**
 * Match policy for binding: subtree or path.
 *
 * - `subtree` match policy means that path filtering would return events matching the exact path and its subpaths,
 * ie. changes to (nested) children would be allowed to bubble up to parent listeners.
 * - {@link SubtreePolicy} match policy is  equivalent with `subtree` match policy, while allowing to specify a maximum
 * depth for the subtree.
 * - `path` match policy means that path filtering would return events matching the _exact_ path only. In this case
 * _exact_ semantics include interpreting an `undefined` _index_ field in the {@link PathStep} as a wildcard.
 *
 *
 * @alpha
 */
export type MatchPolicy = SubtreePolicy | "subtree" | "path";
/**
 * Subtree match policy where max depth can be specified.
 *
 * @alpha
 */
export interface SubtreePolicy {
    maxDepth: number;
}
/**
 * The data binder interface
 *
 * @alpha
 */
export interface DataBinder<B extends OperationBinderEvents | InvalidationBinderEvents> {
    /**
     * Register an event listener
     *
     * @param anchor - The anchor to register the listener on
     * @param eventType - The {@link BindingType} to listen for.
     * @param eventTrees - The {@link BindPolicy}s to filter on.
     * @param listener - The listener to register
     */
    register<K extends keyof Events<B>>(anchor: EditableTree, eventType: K, eventTrees: BindPolicy[], listener?: B[K]): void;
    /**
     * Unregister all listeners.
     */
    unregisterAll(): void;
}
/**
 * An interface describing the ability to flush.
 *
 * @alpha
 */
export interface Flushable<T> {
    flush(): T;
}
/**
 * An interface describing a flushable data binder.
 *
 * @alpha
 */
export interface FlushableDataBinder<B extends OperationBinderEvents | InvalidationBinderEvents> extends DataBinder<B>, Flushable<FlushableDataBinder<B>> {
}
/**
 * A step in a bind path
 *
 * @alpha
 */
export interface PathStep {
    /**
     * The field being traversed
     */
    readonly field: FieldKey;
    /**
     * The index of the element being navigated to
     */
    readonly index?: number;
}
/**
 * The default type for a bind tree
 *
 * @alpha
 */
export type BindTreeDefault = BindTree;
/**
 * A bind tree is a compact representation of related {@link BindPath}s.
 *
 * @alpha
 */
export interface BindTree<T = BindTreeDefault> extends PathStep {
    readonly children: Map<FieldKey, T>;
}
/**
 * A bind policy is a combination of a {@link BindTree} and a {@link MatchPolicy}.
 *
 * @alpha
 */
export interface BindPolicy {
    readonly bindTree: BindTree;
    readonly matchPolicy: MatchPolicy;
}
/**
 * Index symbol for syntax tree
 *
 * @alpha
 */
export declare const indexSymbol: unique symbol;
/**
 * A syntax node for the bind language
 *
 * The bind language is a compact representation of related {@link BindPath}s. It can be used to
 * simplify usage and construction of {@link BindTree}s.
 *
 * see {@link BindTree}
 * see {@link compileSyntaxTree}
 *
 * @alpha
 */
export interface BindSyntaxTree {
    readonly [indexSymbol]?: number;
    readonly [key: string]: true | BindSyntaxTree;
}
/**
 * A top down path in a bind or path tree is a collection of {@link PathStep}s
 *
 * see {@link BindTree}
 * see {@link UpPath}
 *
 * @alpha
 */
export type DownPath = PathStep[];
/**
 * A bind path is a top down path in a bind tree
 *
 * @alpha
 */
export type BindPath = DownPath;
/**
 * A binding context specialized for {@link PathVisitor} triggered binding events.
 *
 * @alpha
 */
export type VisitorBindingContext = DeleteBindingContext | InsertBindingContext;
/**
 * Enumeration of binding categories
 *
 * @alpha
 */
export declare const BindingType: {
    readonly Delete: "delete";
    readonly Insert: "insert";
    readonly Invalidation: "invalidation";
    readonly Batch: "batch";
};
/**
 * The type of a binding context
 *
 * @alpha
 */
export type BindingContextType = (typeof BindingType)[keyof typeof BindingType];
/**
 * The binding context attribution common to all binding events
 *
 * @alpha
 */
export interface BindingContext {
    readonly type: BindingContextType;
}
/**
 * The binding context for a delete event
 *
 * @alpha
 */
export interface DeleteBindingContext extends BindingContext {
    readonly type: typeof BindingType.Delete;
    readonly path: UpPath;
    readonly count: number;
}
/**
 * The binding context for an insert event
 *
 * @alpha
 */
export interface InsertBindingContext extends BindingContext {
    readonly type: typeof BindingType.Insert;
    readonly path: UpPath;
    readonly content: ProtoNodes;
}
/**
 * The binding context for an invalidation event
 *
 * @alpha
 */
export interface InvalidationBindingContext extends BindingContext {
    readonly type: typeof BindingType.Invalidation;
}
/**
 * The binding context for a batch event
 *
 * @alpha
 */
export interface BatchBindingContext extends BindingContext {
    readonly type: typeof BindingType.Batch;
    readonly events: VisitorBindingContext[];
}
/**
 * Compute a top-town {@link DownPath} from an {@link UpPath}.
 *
 * @alpha
 */
export declare function toDownPath(upPath: UpPath): DownPath;
/**
 * Create a buffering data binder.
 *
 * @alpha
 */
export declare function createDataBinderBuffering<E extends Events<E>>(view: ISubscribable<E>, options: FlushableBinderOptions<E>): FlushableDataBinder<OperationBinderEvents>;
/**
 * Create a direct data binder.
 *
 * @alpha
 */
export declare function createDataBinderDirect<E extends Events<E>>(view: ISubscribable<E>, options: BinderOptions): DataBinder<OperationBinderEvents>;
/**
 * Create an invalidating data binder.
 *
 * @alpha
 */
export declare function createDataBinderInvalidating<E extends Events<E>>(view: ISubscribable<E>, options: FlushableBinderOptions<E>): FlushableDataBinder<InvalidationBinderEvents>;
/**
 * Create binder options. If not specified, the default values are:
 * - sortFn: no sorting
 *
 * @alpha
 */
export declare function createBinderOptions({ sortFn }: {
    sortFn?: BinderEventsCompare;
}): BinderOptions;
/**
 * Create flushable binder options. If not specified, the default values are:
 * - sortFn: no sorting
 * - sortAnchorsFn: no sorting
 * - autoFlush: true
 *
 * @alpha
 */
export declare function createFlushableBinderOptions<E extends Events<E>>({ sortFn, sortAnchorsFn, autoFlush, autoFlushPolicy, }: {
    sortFn?: BinderEventsCompare;
    sortAnchorsFn?: AnchorsCompare;
    autoFlush?: boolean;
    autoFlushPolicy: keyof Events<E>;
}): FlushableBinderOptions<E>;
/**
 * Utility to create a compare function from a list of compare functions.
 *
 * @param fns - a list of compare functions
 * @returns a compare function that can be used for sorting
 * @alpha
 */
export declare function comparePipeline<T>(...fns: CompareFunction<T>[]): CompareFunction<T>;
/**
 * Compiles a (user friendly) syntax tree into the internal binding structure.
 * The syntax tree is a compact representation of related {@link BindPath}s.
 * The match policy can be specified. If not specified, the default value is "path".
 * @returns a {@link BindPolicy} object
 * @alpha
 */
export declare function compileSyntaxTree(syntaxTree: BindSyntaxTree, matchPolicy?: MatchPolicy): BindPolicy;
//# sourceMappingURL=editableTreeBinder.d.ts.map