/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import type { ErasedType, IFluidLoadable } from "@fluidframework/core-interfaces/internal";
import type { IChannelStorageService } from "@fluidframework/datastore-definitions/internal";
import type { IIdCompressor } from "@fluidframework/id-compressor";
import type { MinimumVersionForCollab } from "@fluidframework/runtime-definitions/internal";
import type { IChannelView, IFluidSerializer, SharedKernel } from "@fluidframework/shared-object-base/internal";
import { type ITelemetryLoggerExt } from "@fluidframework/telemetry-utils/internal";
import { type CodecTree, type CodecWriteOptions, type ICodecOptions } from "../codec/index.js";
import { type GraphCommit, type IEditableForest, type JsonableTree, type TreeStoredSchema, TreeStoredSchemaRepository, type TreeStoredSchemaSubscription } from "../core/index.js";
import { TreeCompressionStrategy, type IncrementalEncodingPolicy } from "../feature-libraries/index.js";
import { type BranchId, type ClonableSchemaAndPolicy, type SharedTreeCoreOptionsInternal, MessageFormatVersion, SharedTreeCore, EditManagerFormatVersion } from "../shared-tree-core/index.js";
import { type ITree, type ImplicitFieldSchema, type ReadSchema, type SimpleTreeSchema, type TreeView, type TreeViewAlpha, type TreeViewConfiguration, type UnsafeUnknownSchema, type VerboseTree, type ITreeAlpha, type SchemaType } from "../simple-tree/index.js";
import { SchematizingSimpleTreeView } from "./schematizingTreeView.js";
import type { SharedTreeChange } from "./sharedTreeChangeTypes.js";
import type { IdBasedSharedTreeEditBuilder } from "./sharedTreeEditBuilder.js";
import { type TreeCheckout, type BranchableTree } from "./treeCheckout.js";
import { type Breakable, type JsonCompatible } from "../util/index.js";
import { type SharedTreeChangeFormatVersion } from "./sharedTreeChangeCodecs.js";
/**
 * Copy of data from an {@link ITreePrivate} at some point in time.
 * @remarks
 * This is unrelated to Fluids concept of "snapshots".
 */
export interface SharedTreeContentSnapshot {
    /**
     * The schema stored in the document.
     *
     * @remarks
     * Edits to the schema can mutate the schema stored of the tree which took this snapshot (but this snapshot will remain the same)
     * This is mainly useful for debugging cases where schematize reports an incompatible view schema.
     */
    readonly schema: TreeStoredSchema;
    /**
     * All {@link TreeStatus.InDocument} content.
     */
    readonly tree: JsonableTree[];
    /**
     * All {@link TreeStatus.Removed} content.
     */
    readonly removed: [string | number | undefined, number, JsonableTree][];
}
/**
 * {@link ITree} extended with some non-public APIs.
 * @internal
 */
export interface ITreeInternal extends IChannelView, ITreeAlpha {
}
/**
 * {@link ITreeInternal} extended with some non-exported APIs.
 * @remarks
 * This allows access to the tree content using the internal data model used at the storage and "flex" layers,
 * and should only be needed for testing and debugging this package's internals.
 */
export interface ITreePrivate extends ITreeInternal {
    /**
     * Provides a copy of the current content of the tree.
     * This can be useful for inspecting the tree when no suitable view schema is available.
     * This is only intended for use in testing and exceptional code paths: it is not performant.
     *
     * This does not include everything that is included in a tree summary, since information about how to merge future edits is omitted.
     */
    contentSnapshot(): SharedTreeContentSnapshot;
    /**
     * Access to internals for testing.
     */
    readonly kernel: SharedTreeKernel;
}
/**
 * The type SharedTree's kernel's view must implement so what when its merged with the underling SharedObject's API it fully implements the required tree API surface ({@link ITreePrivate }).
 */
export type SharedTreeKernelView = Omit<ITreePrivate, keyof (IChannelView & IFluidLoadable)>;
/**
 * SharedTreeCore, configured with a good set of indexes and field kinds which will maintain compatibility over time.
 *
 * TODO: detail compatibility requirements.
 */
export declare class SharedTreeKernel extends SharedTreeCore<IdBasedSharedTreeEditBuilder, SharedTreeChange> implements SharedKernel {
    private readonly logger;
    readonly checkout: TreeCheckout;
    get storedSchema(): TreeStoredSchemaRepository;
    private readonly checkouts;
    /**
     * The app-facing API for SharedTree implemented by this Kernel.
     * @remarks
     * This is the API grafted onto the ISharedObject which apps can access.
     * It includes both the APIs used for internal testing, and public facing APIs (both stable and unstable).
     * Different users will have access to different subsets of this API, see {@link ITree}, {@link ITreeAlpha} and {@link ITreeInternal} which this {@link ITreePrivate} extends.
     */
    readonly view: SharedTreeKernelView;
    constructor(breaker: Breakable, sharedObject: IChannelView & IFluidLoadable, serializer: IFluidSerializer, submitLocalMessage: (content: unknown, localOpMetadata?: unknown) => void, lastSequenceNumber: () => number | undefined, initialSequenceNumber: number, logger: ITelemetryLoggerExt | undefined, idCompressor: IIdCompressor, optionsParam: SharedTreeOptionsInternal);
    private registerCheckout;
    exportVerbose(): VerboseTree | undefined;
    exportSimpleSchema(): SimpleTreeSchema;
    contentSnapshot(): SharedTreeContentSnapshot;
    viewWith<TRoot extends ImplicitFieldSchema | UnsafeUnknownSchema>(config: TreeViewConfiguration<ReadSchema<TRoot>>): SchematizingSimpleTreeView<TRoot> & TreeView<ReadSchema<TRoot>>;
    viewWith<TRoot extends ImplicitFieldSchema>(config: TreeViewConfiguration<TRoot>): SchematizingSimpleTreeView<TRoot> & TreeView<TRoot>;
    viewBranchWith<TRoot extends ImplicitFieldSchema>(branchId: string, config: TreeViewConfiguration<TRoot>): TreeView<TRoot>;
    private getCheckout;
    private checkoutBranch;
    loadCore(services: IChannelStorageService): Promise<void>;
    didAttach(): void;
    applyStashedOp(...args: Parameters<SharedTreeCore<IdBasedSharedTreeEditBuilder, SharedTreeChange>["applyStashedOp"]>): void;
    protected submitCommit(branchId: BranchId, commit: GraphCommit<SharedTreeChange>, schemaAndPolicy: ClonableSchemaAndPolicy, isResubmit: boolean): void;
    onDisconnect(): void;
}
export declare function exportSimpleSchema(storedSchema: TreeStoredSchema): SimpleTreeSchema<SchemaType.Stored>;
/**
 * A way to parse schema in the persisted format from {@link extractPersistedSchema}.
 * @remarks
 * This behaves identically to {@link ITreeAlpha.exportSimpleSchema},
 * except that it gets the schema from the caller instead of from an existing tree.
 *
 * This can be useful for inspecting the contents of persisted schema,
 * such as those generated by {@link extractPersistedSchema} for use in testing.
 * Since that data format is otherwise unspecified,
 * this provides a way to inspect its contents with documented semantics.
 * @alpha
 */
export declare function persistedToSimpleSchema(persisted: JsonCompatible, options: ICodecOptions): SimpleTreeSchema;
/**
 * Get a {@link BranchableTree} from a {@link ITree}.
 * @remarks The branch can be used for "version control"-style coordination of edits on the tree.
 * @privateRemarks This function will be removed if/when the branching API becomes public,
 * but it (or something like it) is necessary in the meantime to prevent the alpha types from being exposed as public.
 * @alpha
 * @deprecated This API is superseded by {@link TreeBranch}, which should be used instead.
 */
export declare function getBranch(tree: ITree): BranchableTree;
/**
 * Get a {@link BranchableTree} from a {@link TreeView}.
 * @remarks The branch can be used for "version control"-style coordination of edits on the tree.
 * Branches are currently an unstable "alpha" API and are subject to change in the future.
 * @privateRemarks This function will be removed if/when the branching API becomes public,
 * but it (or something like it) is necessary in the meantime to prevent the alpha types from being exposed as public.
 * @alpha
 * @deprecated This API is superseded by {@link TreeBranch}, which should be used instead.
 */
export declare function getBranch<T extends ImplicitFieldSchema | UnsafeUnknownSchema>(view: TreeViewAlpha<T>): BranchableTree;
/**
 * Defines for each EditManagerFormatVersion the SharedTreeChangeFormatVersion to use.
 * This is an arbitrary mapping that is injected in the EditManger codec.
 * Once an entry is defined and used in production, it cannot be changed.
 * This is because the format for SharedTree changes are not explicitly versioned.
 */
export declare const changeFormatVersionForEditManager: import("../codec/codec.js").MappedDependentFormatVersion<EditManagerFormatVersion, SharedTreeChangeFormatVersion>;
/**
 * Defines for each MessageFormatVersion the SharedTreeChangeFormatVersion to use.
 * This is an arbitrary mapping that is injected in the message codec.
 * Once an entry is defined and used in production, it cannot be changed.
 * This is because the format for SharedTree changes are not explicitly versioned.
 */
export declare const changeFormatVersionForMessage: import("../codec/codec.js").MappedDependentFormatVersion<MessageFormatVersion, SharedTreeChangeFormatVersion>;
export declare function getCodecTreeForSharedTreeFormat(clientVersion: MinimumVersionForCollab): CodecTree;
/**
 * Configuration options for SharedTree.
 * @beta @input
 */
export type SharedTreeOptionsBeta = ForestOptions;
/**
 * Configuration options for SharedTree with alpha features.
 * @alpha @input
 */
export interface SharedTreeOptions extends SharedTreeOptionsBeta, Partial<CodecWriteOptions>, Partial<SharedTreeFormatOptions> {
    /**
     * Experimental feature flag to enable shared branches.
     * This feature is not yet complete and should not be used in production.
     * Defaults to false.
     */
    readonly enableSharedBranches?: boolean;
    /**
     * Experimental feature flag to enable the ability to edit detached roots.
     * This feature is not yet complete and should not be used in production.
     * Defaults to false.
     */
    readonly enableDetachedRootEditing?: boolean;
    /**
     * Returns whether a node / field should be incrementally encoded.
     * @remarks
     * See {@link IncrementalEncodingPolicy}.
     */
    shouldEncodeIncrementally?: IncrementalEncodingPolicy;
}
export interface SharedTreeOptionsInternal extends SharedTreeOptions, Partial<SharedTreeCoreOptionsInternal> {
    disposeForksAfterTransaction?: boolean;
}
/**
 * Configuration options for SharedTree's internal tree storage.
 * @beta @input
 */
export interface ForestOptions {
    /**
     * The {@link ForestType} indicating which forest type should be created for the SharedTree.
     */
    readonly forest?: ForestType;
}
/**
 * Options for configuring the persisted format SharedTree uses.
 * @alpha @input
 */
export interface SharedTreeFormatOptions {
    /**
     * See {@link TreeCompressionStrategy}.
     * default: TreeCompressionStrategy.Compressed
     */
    treeEncodeType: TreeCompressionStrategy;
}
/**
 * Used to distinguish between different forest types.
 * @remarks
 * The "Forest" is the internal data structure used to store all the trees (the main tree and any removed ones) for a given view or branch.
 * ForestTypes should all have the same behavior, but may differ in performance and debuggability.
 *
 * Current options are {@link ForestTypeReference}, {@link ForestTypeOptimized} and {@link ForestTypeExpensiveDebug}.
 * @privateRemarks
 * Implement using {@link toForestType}.
 * Consume using {@link buildConfiguredForest}.
 * @sealed @beta
 */
export interface ForestType extends ErasedType<"ForestType"> {
}
/**
 * Reference implementation of forest.
 * @remarks
 * A simple implementation with minimal complexity and moderate debuggability, validation and performance.
 * @privateRemarks
 * The "ObjectForest" forest type.
 * @beta
 */
export declare const ForestTypeReference: ForestType;
/**
 * Optimized implementation of forest.
 * @remarks
 * A complex optimized forest implementation, which has minimal validation and debuggability to optimize for performance.
 * Uses an internal representation optimized for size designed to scale to larger datasets with reduced overhead.
 * @privateRemarks
 * The "ChunkedForest" forest type.
 * @beta
 */
export declare const ForestTypeOptimized: ForestType;
/**
 * Slow implementation of forest intended only for debugging.
 * @remarks
 * Includes validation with scales poorly.
 * May be asymptotically slower than {@link ForestTypeReference}, and may perform very badly with larger data sizes.
 * @privateRemarks
 * The "ObjectForest" forest type with expensive asserts for debugging.
 * @beta
 */
export declare const ForestTypeExpensiveDebug: ForestType;
/**
 * Build and return a forest of the requested type.
 */
export declare function buildConfiguredForest(breaker: Breakable, factory: ForestType, schema: TreeStoredSchemaSubscription, idCompressor: IIdCompressor, shouldEncodeIncrementally: IncrementalEncodingPolicy): IEditableForest;
export declare const defaultSharedTreeOptions: Required<SharedTreeOptionsInternal>;
//# sourceMappingURL=sharedTree.d.ts.map