/*!
 * 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 CodecWriteOptionsBeta, 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 ImplicitFieldSchema, type ReadSchema, type SimpleTreeSchema, type TreeView, type TreeViewConfiguration, type UnsafeUnknownSchema, type VerboseTree, type ITreeAlpha, type SchemaType } from "../simple-tree/index.js";
import { type Breakable, type JsonCompatible } from "../util/index.js";
import type { SchematizingSimpleTreeView } from "./schematizingTreeView.js";
import { SharedTreeChangeFormatVersion } from "./sharedTreeChangeCodecs.js";
import type { SharedTreeChange } from "./sharedTreeChangeTypes.js";
import type { SharedTreeEditBuilder } from "./sharedTreeEditBuilder.js";
import { type TreeCheckout } from "./treeCheckout.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<SharedTreeEditBuilder, 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<SharedTreeEditBuilder, 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;
/**
 * 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 interface SharedTreeOptionsBeta extends ForestOptions, Partial<CodecWriteOptionsBeta> {
    /**
     * When `true`, when an improperly encoded identifier is encountered in a summary,
     * a new identifier will be generated instead of throwing an error.
     *
     * @defaultValue `false`
     *
     * @remarks
     * The intended use is recovering documents whose attach summary was persisted
     * with unresolvable node identifiers (a SharedTree-attaches-to-already-attached-container
     * scenario). Without this flag enabled, a client opening such a document will throw at summary load time.
     * With this flag enabled, unresolvable identifiers are replaced at decode time with stable UUIDs
     * derived deterministically from the data store id and the unresolvable op-space integer,
     * so every reader of the same blob (other than the originator) agrees on the resulting in-memory id.
     * Healed identifiers are written back out at the next summary in their stable UUID form,
     * so the inconsistency does not need to be re-healed on every load.
     *
     * Off by default because enabling it for documents that are not actually corrupt
     * would mask genuine bugs that otherwise surface as decode failures.
     *
     * This mitigation is also not perfect as the client that originated the non-finalized
     * will not apply the same "healing" and will continue to use the original identifier.
     * The difference in identifiers between the originating client and subsequent clients that load
     * the document is not ideal but generally benign to Fluid. However, it may have application-level
     * consequences (e.g. if the application stores the identifiers elsewhere, either in other parts of
     * Fluid data or externally). Applications should consider whether this risk is acceptable before
     * enabling this option.
     *
     * @privateRemarks
     * "Unresolvable" in the public-facing remarks corresponds to non-finalized short IDs persisted without
     * any corresponding context for their originating session. See id-compressor internal documentation
     * for more details.
     */
    readonly healUnresolvableIdentifiersOnDecode?: boolean;
}
/**
 * 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;
    /**
     * 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