import { BaseObserver, ConnectionManager, DBAdapter, SubscribedStream, SyncStatus, type ILogLevel, type ILogger, type PowerSyncConnectionOptions, type StreamingSyncImplementation, type StreamingSyncImplementationListener, type SyncStatusOptions } from '@powersync/common';
import { Mutex } from 'async-mutex';
import * as Comlink from 'comlink';
import { WebStreamingSyncImplementation, WebStreamingSyncImplementationOptions } from '../../db/sync/WebStreamingSyncImplementation';
import { WorkerWrappedAsyncDatabaseConnection } from '../../db/adapters/WorkerWrappedAsyncDatabaseConnection';
import { ResolvedWebSQLOpenOptions } from '../../db/adapters/web-sql-flags';
import { AbstractSharedSyncClientProvider } from './AbstractSharedSyncClientProvider';
/**
 * @internal
 * Manual message events for shared sync clients
 */
export declare enum SharedSyncClientEvent {
    /**
     * This client requests the shared sync manager should
     * close it's connection to the client.
     */
    CLOSE_CLIENT = "close-client",
    CLOSE_ACK = "close-ack"
}
/**
 * @internal
 */
export type ManualSharedSyncPayload = {
    event: SharedSyncClientEvent;
    data: any;
};
/**
 * @internal
 */
export type SharedSyncInitOptions = {
    streamOptions: Omit<WebStreamingSyncImplementationOptions, 'adapter' | 'uploadCrud' | 'remote' | 'subscriptions'>;
    dbParams: ResolvedWebSQLOpenOptions;
};
/**
 * @internal
 */
export interface SharedSyncImplementationListener extends StreamingSyncImplementationListener {
    initialized: () => void;
}
/**
 * @internal
 */
export type WrappedSyncPort = {
    port: MessagePort;
    clientProvider: Comlink.Remote<AbstractSharedSyncClientProvider>;
    db?: DBAdapter;
    currentSubscriptions: SubscribedStream[];
    closeListeners: (() => void | Promise<void>)[];
    isClosing: boolean;
};
/**
 * @internal
 */
export type RemoteOperationAbortController = {
    controller: AbortController;
    activePort: WrappedSyncPort;
};
/**
 * @internal
 * Shared sync implementation which runs inside a shared webworker
 */
export declare class SharedSyncImplementation extends BaseObserver<SharedSyncImplementationListener> {
    protected ports: WrappedSyncPort[];
    protected isInitialized: Promise<void>;
    protected statusListener?: () => void;
    protected fetchCredentialsController?: RemoteOperationAbortController;
    protected uploadDataController?: RemoteOperationAbortController;
    protected syncParams: SharedSyncInitOptions | null;
    protected logger: ILogger;
    protected lastConnectOptions: PowerSyncConnectionOptions | undefined;
    protected portMutex: Mutex;
    private subscriptions;
    protected connectionManager: ConnectionManager;
    syncStatus: SyncStatus;
    broadCastLogger: ILogger;
    protected distributedDB: DBAdapter | null;
    constructor();
    get lastSyncedAt(): Date | undefined;
    get isConnected(): boolean;
    /**
     * Gets the last client port which we know is safe from unexpected closes.
     */
    protected getLastWrappedPort(): Promise<WrappedSyncPort | undefined>;
    /**
     * In some very rare cases a specific tab might not respond to requests.
     * This returns a random port which is not closing.
     */
    protected getRandomWrappedPort(): Promise<WrappedSyncPort | undefined>;
    waitForStatus(status: SyncStatusOptions): Promise<void>;
    waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
    waitForReady(): Promise<void>;
    private collectActiveSubscriptions;
    updateSubscriptions(port: WrappedSyncPort, subscriptions: SubscribedStream[]): void;
    setLogLevel(level: ILogLevel): void;
    /**
     * Configures the DBAdapter connection and a streaming sync client.
     */
    setParams(params: SharedSyncInitOptions): Promise<void>;
    dispose(): Promise<void>;
    /**
     * Connects to the PowerSync backend instance.
     * Multiple tabs can safely call this in their initialization.
     * The connection will simply be reconnected whenever a new tab
     * connects.
     */
    connect(options?: PowerSyncConnectionOptions): Promise<void>;
    disconnect(): Promise<void>;
    /**
     * Adds a new client tab's message port to the list of connected ports
     */
    addPort(port: MessagePort): Promise<{
        port: MessagePort;
        clientProvider: Comlink.Remote<AbstractSharedSyncClientProvider>;
        currentSubscriptions: never[];
        closeListeners: never[];
        isClosing: false;
    }>;
    /**
     * Removes a message port client from this manager's managed
     * clients.
     */
    removePort(port: WrappedSyncPort): Promise<() => void>;
    triggerCrudUpload(): void;
    hasCompletedSync(): Promise<boolean>;
    getWriteCheckpoint(): Promise<string>;
    protected withSyncImplementation<T>(callback: (sync: StreamingSyncImplementation) => Promise<T>): Promise<T>;
    protected generateStreamingImplementation(): WebStreamingSyncImplementation;
    /**
     * Opens a worker wrapped database connection. Using the last connected client port.
     */
    protected openInternalDB(): Promise<WorkerWrappedAsyncDatabaseConnection<ResolvedWebSQLOpenOptions>>;
    /**
     * A method to update the all shared statuses for each
     * client.
     */
    private updateAllStatuses;
}
