import type { SyncularChangedRow, SyncularCrdtDocumentSnapshot, SyncularCrdtFieldCompactionReceipt, SyncularCrdtFieldDescriptor, SyncularCrdtFieldMaterialization, SyncularCrdtFieldRequest, SyncularCrdtFieldWriteReceipt, SyncularCrdtFieldYjsUpdateRequest, SyncularCrdtUpdateLogEntry, SyncularRowsChangedEvent, SyncularYjsUpdateEnvelope } from '@syncular/client';
export interface SyncularCrdtFieldHost {
    openCrdtField(request: SyncularCrdtFieldRequest): Promise<SyncularCrdtFieldDescriptor>;
    applyCrdtFieldYjsUpdate(request: SyncularCrdtFieldYjsUpdateRequest): Promise<SyncularCrdtFieldWriteReceipt>;
    enqueueCrdtFieldYjsUpdate?(request: SyncularCrdtFieldYjsUpdateRequest): Promise<string>;
    materializeCrdtField(request: SyncularCrdtFieldRequest): Promise<SyncularCrdtFieldMaterialization>;
    crdtDocumentSnapshot?(request: SyncularCrdtFieldRequest): Promise<SyncularCrdtDocumentSnapshot>;
    crdtUpdateLog?(request: SyncularCrdtFieldRequest & {
        limit?: number;
    }): Promise<SyncularCrdtUpdateLogEntry[]>;
    snapshotCrdtFieldStateVector(request: SyncularCrdtFieldRequest): Promise<{
        stateVectorBase64: string;
    }>;
    compactCrdtField(request: {
        table: string;
        rowId: string;
        field: string;
        minUncheckpointedUpdates?: number;
    }): Promise<SyncularCrdtFieldCompactionReceipt>;
}
export interface SyncularCrdtProjectionHost extends SyncularCrdtFieldHost {
    addRowsChangedListener(listener: (event: SyncularRowsChangedEvent) => void): () => void;
}
export interface YjsDocumentBinding {
    subscribeLocalUpdates(listener: (update: Uint8Array) => void): () => void;
    applyRemoteUpdate?: (update: Uint8Array) => void;
    replaceDocumentState?: (state: Uint8Array, receipt: YjsDocumentRestoreReceipt) => void;
    replaceMaterializedValue?: (value: unknown) => void;
}
export interface YjsDocumentFieldAdapterOptions {
    flushDelayMs?: number;
    maxPendingUpdates?: number;
    restoreOnStart?: boolean;
    updateId?: () => string;
    onBackpressure?: (event: YjsDocumentFieldBackpressureEvent) => void;
    onFlushStart?: (event: YjsDocumentFieldFlushEvent) => void;
    onFlushSuccess?: (event: YjsDocumentFieldFlushEvent) => void;
    onFlushError?: (error: unknown) => void;
}
export interface YjsDocumentFieldBackpressureEvent {
    pendingUpdates: number;
    maxPendingUpdates: number;
}
export interface YjsDocumentFieldFlushEvent {
    pendingUpdates: number;
}
export interface YjsDocumentFieldAdapter {
    open(): Promise<SyncularCrdtFieldDescriptor>;
    start(): Promise<() => Promise<void>>;
    flush(): Promise<void>;
    pendingUpdateCount(): number;
    restoreFromPersistedState(): Promise<YjsDocumentRestoreReceipt>;
    refreshMaterializedValue(): Promise<SyncularCrdtFieldMaterialization>;
    snapshotStateVector(): Promise<string>;
    applyRemoteUpdate(update: SyncularYjsUpdateEnvelope): void;
    compact(minUncheckpointedUpdates?: number): Promise<SyncularCrdtFieldCompactionReceipt>;
}
export interface YjsDocumentRestoreReceipt {
    field: SyncularCrdtFieldRequest;
    descriptor: SyncularCrdtFieldDescriptor;
    materialization: SyncularCrdtFieldMaterialization;
    documentSnapshot?: SyncularCrdtDocumentSnapshot;
    stateBase64?: string | null;
    stateVectorBase64: string;
    restoredState: boolean;
}
export type SyncularCrdtProjectionReason = 'local-write' | 'remote-apply' | 'compaction' | 'manual' | 'startup';
export interface SyncularCrdtProjectionCause {
    reason: SyncularCrdtProjectionReason;
    source: SyncularRowsChangedEvent['source'] | 'manual' | 'startup';
    changedRow?: SyncularChangedRow;
}
export interface SyncularCrdtProjectionEvent {
    field: SyncularCrdtFieldRequest;
    descriptor: SyncularCrdtFieldDescriptor;
    materialization: SyncularCrdtFieldMaterialization;
    documentSnapshot?: SyncularCrdtDocumentSnapshot;
    updateLog?: SyncularCrdtUpdateLogEntry[];
    latestUpdate?: SyncularCrdtUpdateLogEntry;
    reason: SyncularCrdtProjectionReason;
    source: SyncularCrdtProjectionCause['source'];
    operation?: SyncularChangedRow['operation'];
    commitId?: string | null;
    commitSeq?: number | null;
    serverVersion?: number | null;
    stateVectorBase64: string;
    changedRow?: SyncularChangedRow;
}
export interface SyncularCrdtProjectionDefinition<TProjection> {
    derive(materialization: SyncularCrdtFieldMaterialization, event: SyncularCrdtProjectionEvent): TProjection | Promise<TProjection>;
    apply(projection: TProjection, event: SyncularCrdtProjectionEvent): void | Promise<void>;
}
export interface SyncularCrdtProjectionMaterializerOptions<TProjection> {
    materializeOnStart?: boolean;
    updateLogLimit?: number;
    onMaterialized?: (projection: TProjection, event: SyncularCrdtProjectionEvent) => void;
    onError?: (error: unknown, event?: SyncularCrdtProjectionEvent) => void;
}
export interface SyncularCrdtProjectionMaterializer<_TProjection> {
    start(): Promise<() => Promise<void>>;
    stop(): Promise<void>;
    flush(): Promise<void>;
    materialize(cause?: Partial<SyncularCrdtProjectionCause>): Promise<SyncularCrdtProjectionEvent>;
}
export interface RichEditorCrdtAdapterOptions<TProjection> {
    field?: YjsDocumentFieldAdapterOptions;
    projections?: SyncularCrdtProjectionMaterializerOptions<TProjection>;
}
export interface RichEditorCrdtAdapter<_TProjection> {
    document: YjsDocumentFieldAdapter;
    projections: SyncularCrdtProjectionMaterializer<_TProjection>;
    start(): Promise<() => Promise<void>>;
    stop(): Promise<void>;
    flush(): Promise<void>;
    restoreFromPersistedState(): Promise<YjsDocumentRestoreReceipt>;
    compact(minUncheckpointedUpdates?: number): Promise<SyncularCrdtFieldCompactionReceipt>;
}
export type YjsEditorBackpressureState = 'open' | 'blocked' | 'recovering';
export interface YjsEditorBackpressureEvent {
    state: YjsEditorBackpressureState;
    pendingUpdates: number;
    maxPendingUpdates?: number;
    retryAttempt: number;
    nextRetryDelayMs?: number;
    error?: unknown;
}
export interface YjsEditorBackpressureControllerOptions {
    retryDelayMs?: number;
    maxRetryDelayMs?: number;
    autoRetry?: boolean;
    setEditorReadOnly?: (readOnly: boolean, event: YjsEditorBackpressureEvent) => void;
    showSavingBlocked?: (event: YjsEditorBackpressureEvent) => void;
    clearSavingBlocked?: (event: YjsEditorBackpressureEvent) => void;
    onStateChange?: (event: YjsEditorBackpressureEvent) => void;
    onRetryError?: (error: unknown, event: YjsEditorBackpressureEvent) => void;
}
export interface YjsEditorBackpressureController {
    readonly fieldOptions: Pick<YjsDocumentFieldAdapterOptions, 'onBackpressure' | 'onFlushError' | 'onFlushStart' | 'onFlushSuccess'>;
    attach(adapter: Pick<YjsDocumentFieldAdapter, 'flush' | 'pendingUpdateCount'>): void;
    detach(): void;
    state(): YjsEditorBackpressureState;
    retryNow(): Promise<void>;
    stop(): void;
}
export declare function createYjsDocumentFieldAdapter(host: SyncularCrdtFieldHost, field: SyncularCrdtFieldRequest, binding: YjsDocumentBinding, options?: YjsDocumentFieldAdapterOptions): YjsDocumentFieldAdapter;
export declare function createYjsEditorBackpressureController(options?: YjsEditorBackpressureControllerOptions): YjsEditorBackpressureController;
export declare function createRichEditorCrdtAdapter<TProjection>(host: SyncularCrdtProjectionHost, field: SyncularCrdtFieldRequest, binding: YjsDocumentBinding, projection: SyncularCrdtProjectionDefinition<TProjection>, options?: RichEditorCrdtAdapterOptions<TProjection>): RichEditorCrdtAdapter<TProjection>;
export declare function createCrdtFieldProjectionMaterializer<TProjection>(host: SyncularCrdtProjectionHost, field: SyncularCrdtFieldRequest, projection: SyncularCrdtProjectionDefinition<TProjection>, options?: SyncularCrdtProjectionMaterializerOptions<TProjection>): SyncularCrdtProjectionMaterializer<TProjection>;
export declare function crdtChangedRowMatchesField(row: SyncularChangedRow, field: SyncularCrdtFieldRequest, descriptor: SyncularCrdtFieldDescriptor): boolean;
export declare function projectionReasonForRowsChanged(event: SyncularRowsChangedEvent, row: SyncularChangedRow): SyncularCrdtProjectionReason;
export declare function bytesToBase64(bytes: Uint8Array): string;
export declare function base64ToBytes(value: string): Uint8Array;
//# sourceMappingURL=yjs-document-field-adapter.d.ts.map