import { SyncronizationStatus } from "@superblocksteam/library-shared/types";
import { type DegradedMode, type ApplicationHashChangeSource, TracedEventEmitter } from "@superblocksteam/shared";
import { type ErrorEventMap } from "../errors/error-handler.js";
import { OperationQueue } from "../util/operation-queue.js";
import type { DraftEventMap, DraftInterface } from "../draft-interface.js";
import type { LockService } from "../lock-service/index.js";
import type { AutoConnectingRpcClient } from "../server-rpc/index.js";
import type { GenerationNumberSequence } from "../util/generation-number-sequence.js";
import type { Logger } from "../util/logger.js";
import type { Tracer } from "@opentelemetry/api";
export declare enum SyncServiceStatus {
    /** The sync service is not currently syncing or scheduled to sync */
    IDLE = "IDLE",
    /** The sync service is scheduled to sync */
    SCHEDULED = "SCHEDULED",
    /** The sync service is currently syncing server -> local */
    DOWNLOADING = "DOWNLOADING",
    /** The sync service is currently syncing local -> server */
    UPLOADING = "UPLOADING",
    /** The sync service is currently paused */
    PAUSED = "PAUSED"
}
/** Configuration for the SyncService passed to the constructor */
export interface SyncServiceConfig {
    superblocksBaseUrl: string;
    fsOperationQueue: OperationQueue;
    appRootDirPath: string;
    applicationId: string;
    branchName: string;
    lockService: LockService;
    tracer: Tracer;
    rpcClient: AutoConnectingRpcClient;
    logger: Logger;
}
interface EventMap extends ErrorEventMap, DraftEventMap {
    /** Emitted when the sync service status changes, which indicates what the service is currently doing */
    statusChange: [
        status: SyncServiceStatus,
        /** The time when the next sync is scheduled to be performed, in milliseconds in the monotonic clock (`performance.now()`) */
        scheduledSyncTime: number | undefined
    ];
    /** Emitted when the synchronization status changes, which indicates whether the server and local directories are in sync */
    synchronizationStatusChange: [
        status: SyncronizationStatus,
        generationNumber: number | undefined
    ];
    /** Emitted when the degraded mode changes */
    degradedModeChange: [degradedMode: DegradedMode | undefined];
    /** Emitted when the last commit ID changes after a successful commit */
    commitIdChanged: [commitId: string];
}
export declare class SyncService extends TracedEventEmitter<EventMap> implements DraftInterface {
    private readonly appRootDirPath;
    readonly applicationId: string;
    readonly branchName: string;
    generationNumberSequence: GenerationNumberSequence | undefined;
    private lastSyncedGenerationNumber;
    private hashCache;
    lastSyncedDirectoryHash: string | undefined;
    /** The most recent commit ID returned by the server after a successful commit */
    lastCommitId: string | undefined;
    private readonly fsOperationQueue;
    private readonly syncOperationQueue;
    /** This will be defined if (and only if) a sync is currently pending */
    private pendingSyncUp;
    private lockService;
    private _status;
    private _synchronizationStatus;
    private _tracer;
    private _rpcClient;
    private _logger;
    private _moduleInvalidationCallback?;
    constructor(config: SyncServiceConfig);
    private getLogger;
    /** The current status of the sync service, which indicates what the service is currently doing */
    get status(): SyncServiceStatus;
    private set status(value);
    /** The current synchronization status, which indicates whether the server and local directories are in sync */
    get synchronizationStatus(): SyncronizationStatus;
    private set synchronizationStatus(value);
    /**
     * Enqueues a filesystem operation to be executed one at a time and avoiding conflicts with sync operations.
     */
    private fsOperation;
    /**
     * Sets a callback to invalidate Vite's module graph when files are created/modified.
     * This ensures that TypeScript checks and builds get fresh data after file writes.
     *
     * @param callback - Function that takes a file path (relative to app root) and invalidates the module
     */
    setModuleInvalidationCallback(callback: (filePath: string) => void): void;
    /**
     * Pulls the application down from the server.
     */
    downloadDirectory(): Promise<void>;
    /**
     * Request a sync of the local application directory to the server.
     *
     * This method is asynchronous, in the sense that it does not wait for the sync to complete. It simply schedules
     * a sync to happen in the future. The returned promise resolves as soon as the method has collected enough information
     * to schedule the sync. So the caller must wait for the returned promise to resolve before performing any filesystem
     * operations.
     * This method is also idempotent.
     */
    uploadDirectory(source: ApplicationHashChangeSource): Promise<void>;
    /**
     * Requests an immediate sync of the local application directory to the server, if there are any pending changes.
     * If there are no pending changes, the method returns immediately.
     *
     * @returns the dbfs hash of the most recent sync
     */
    uploadDirectoryNowIfNeeded(source: ApplicationHashChangeSource): Promise<{
        directoryHash: string;
        commitId?: string;
    }>;
    private scheduleUploadDirectory;
    cancelPendingSync(): void;
    /**
     * Uploads the local application directory to the server, retrying if the server returns a BadRequestError.
     */
    private uploadDirectoryWithRetry;
    /**
     * Performs the actual upload of the local application directory to the server.
     */
    private performUploadDirectory;
    /**
     * Checks if there are any local draft changes (.sbswap.edit and .sbswap.add files).
     *
     * @returns A promise that resolves to true if any draft files are found, false otherwise.
     */
    hasLocalDraftChanges(): Promise<boolean>;
    /**
     * Gets information about current draft changes.
     *
     * @returns A promise that resolves to information about draft edits, additions, and deletions.
     */
    getDraftChanges(): Promise<{
        edits: string[];
        additions: string[];
        deletions: string[];
    }>;
    /**
     * Gets a git-like diff representation of all draft changes.
     *
     * @returns A promise that resolves to a git-like diff string.
     */
    getDraftDiff(specificPaths: string[]): Promise<string>;
    /**
     * Creates a draft edit or addition for a file.
     *
     * @param filePath - The path to the file to edit or add (relative to app root).
     * @param draftContent - The content of the draft edit or addition.
     * @returns A promise that resolves when the draft edit or addition is created.
     */
    createDraftFile(filePath: string, draftContent: string | Buffer): Promise<void>;
    /**
     * Creates the marker file only for a draft edit or addition.
     * Compare to `createDraftFile`. This method does not actually write content to target file.
     *
     * @param filePath - The path to the file for which to create the respective marker (relative to app root).
     * @returns A promise that resolves when the draft edit or addition marker is created.
     */
    createDraftMarkerOnly(filePath: string): Promise<void>;
    /**
     * Creates a draft deletion by backing up the file and removing it from the workspace.
     *
     * @param filePath - The path to the file to delete (relative to app root).
     * @returns A promise that resolves when the draft deletion is created.
     */
    createDraftDelete(filePath: string): Promise<void>;
    /**
     * Discards all local draft changes by restoring original files and removing draft additions.
     *
     * @returns A promise that resolves when all draft changes have been discarded.
     */
    discardLocalDraftChanges(source: ApplicationHashChangeSource): Promise<void>;
    /**
     * Commits all draft changes by removing .sbswap.edit and .sbswap.add markers.
     * This makes the current filesystem state the committed state.
     *
     * @returns A promise that resolves when all drafts have been committed.
     */
    commitLocalDraftChanges(source: ApplicationHashChangeSource): Promise<{
        commitId: string;
    } | undefined>;
    /**
     * Reports draft state to the server
     */
    reportDraftStateToServer(hasDraft: boolean): Promise<void>;
    /**
     * Clears the drafts directory without restoring or uploading.
     * Used when restoring from a checkpoint where the database provides the correct state.
     */
    clearDraftsDirectory(): Promise<void>;
}
export {};
//# sourceMappingURL=index.d.ts.map