/**
 * PatchController — state mutation, writer lifecycle, discovery, and CRDT join.
 *
 * Extracted from patch.methods.js. WarpRuntime delegates to this controller
 * via defineProperty loops on the prototype.
 *
 * Discovery helpers (lamport, chain loading, tick enumeration) live in
 * PatchDiscovery.ts and are composed here.
 *
 * @module domain/services/controllers/PatchController
 */
import { PatchBuilder } from '../PatchBuilder.ts';
import { type WarpState } from '../JoinReducer.ts';
import { Writer } from '../../warp/Writer.ts';
import { type PatchDiscoveryHost, type LamportResult, type PatchEntry, type DiscoverTicksResult } from './PatchDiscovery.ts';
import type AdjacencyMap from '../../capabilities/AdjacencyMap.ts';
import type VersionVector from '../../crdt/VersionVector.ts';
import type Patch from '../../types/Patch.ts';
import type BlobStoragePort from '../../../ports/BlobStoragePort.ts';
import type CommitMessageCodecPort from '../../../ports/CommitMessageCodecPort.ts';
import type { PatchStorageRoute } from '../../../ports/CommitMessageCodecPort.ts';
import type { PatchDiff } from '../../types/PatchDiff.ts';
import type { TickReceipt } from '../../types/TickReceipt.ts';
import type { LogicalIndex } from '../index/logicalIndexHelpers.ts';
import type PropertyIndexReader from '../index/PropertyIndexReader.ts';
type DeletePolicy = 'reject' | 'cascade' | 'warn';
type MaterializedAdjacency = {
    outgoing: ReadonlyMap<string, readonly {
        neighborId: string;
        label: string;
    }[]>;
    incoming: ReadonlyMap<string, readonly {
        neighborId: string;
        label: string;
    }[]>;
};
type SetMaterializedState = (state: WarpState, optionsOrDiff?: PatchDiff | {
    diff?: PatchDiff | null;
}) => Promise<{
    state: WarpState;
    stateHash: string;
    adjacency: MaterializedAdjacency;
}>;
/**
 * The host interface that PatchController depends on.
 *
 * Documents the exact host surface the controller accesses,
 * making the coupling explicit and enabling lightweight mock hosts
 * in unit tests.
 */
export interface PatchHost extends PatchDiscoveryHost {
    _writerId: string;
    _versionVector: VersionVector;
    _cachedState: WarpState | null;
    _stateDirty: boolean;
    _maxObservedLamport: number;
    _patchInProgress: boolean;
    _patchesSinceCheckpoint: number;
    _onDeleteWithData: DeletePolicy;
    _blobStorage: BlobStoragePort | null | undefined;
    _patchBlobStorage: BlobStoragePort | null | undefined;
    _commitMessageCodec: CommitMessageCodecPort;
    _provenanceIndex: {
        addPatch: (sha: string, reads: string[] | undefined, writes: string[] | undefined) => void;
    } | null | undefined;
    _lastFrontier: Map<string, string> | null | undefined;
    _auditService: {
        commit: (receipt: TickReceipt) => Promise<string | null>;
    } | null | undefined;
    _auditSkipCount: number;
    _cachedViewHash: string | null;
    _materializedGraph: {
        state: WarpState;
        stateHash: string | null;
        adjacency: AdjacencyMap;
    } | null;
    _logicalIndex: LogicalIndex | null;
    _propertyReader: PropertyIndexReader | null;
    _cachedIndexTree: Record<string, Uint8Array> | null;
    _setMaterializedState: SetMaterializedState;
    _buildAdjacency: (state: WarpState) => MaterializedAdjacency;
}
export interface JoinReceipt {
    nodesAdded: number;
    nodesRemoved: number;
    edgesAdded: number;
    edgesRemoved: number;
    propsChanged: number;
    frontierMerged: boolean;
}
/**
 * Handles state mutation, writer lifecycle, discovery, and CRDT join
 * on behalf of WarpRuntime.
 */
export default class PatchController {
    readonly _host: PatchHost;
    private readonly _discovery;
    constructor(host: PatchHost);
    /**
     * Creates a new PatchBuilder for this graph.
     */
    createPatch(): Promise<PatchBuilder>;
    /**
     * Convenience wrapper: creates a patch, runs the callback, and commits.
     */
    patch(build: (p: PatchBuilder) => void | Promise<void>): Promise<string>;
    /**
     * Applies multiple patches sequentially.
     */
    patchMany(...builds: Array<(p: PatchBuilder) => void | Promise<void>>): Promise<string[]>;
    /**
     * Gets the next lamport timestamp and current parent SHA for this writer.
     */
    _nextLamport(): Promise<LamportResult>;
    /**
     * Loads a patch chain starting from an explicit tip SHA.
     */
    _loadPatchChainFromSha(tipSha: string, stopAtSha?: string | null): Promise<PatchEntry[]>;
    /**
     * Loads all patches from a writer's ref chain.
     */
    _loadWriterPatches(writerId: string, stopAtSha?: string | null): Promise<PatchEntry[]>;
    /**
     * Returns patches from a writer's ref chain (public API).
     */
    getWriterPatches(writerId: string, stopAtSha?: string | null): Promise<PatchEntry[]>;
    /**
     * Discovers all writers that have written to this graph.
     */
    discoverWriters(): Promise<string[]>;
    /**
     * Discovers all distinct Lamport ticks across all writers.
     */
    discoverTicks(): Promise<DiscoverTicksResult>;
    /**
     * Post-commit hook: updates version vector, eager re-materialize,
     * provenance index, frontier, and audit service.
     */
    _onPatchCommitted(writerId: string, { patch: committed, sha }?: {
        patch?: Patch;
        sha?: string;
    }): Promise<void>;
    /**
     * Creates a Writer bound to an existing (or resolved) writer ID.
     */
    writer(writerId?: string): Promise<Writer>;
    /**
     * Ensures cached state is fresh.
     */
    _ensureFreshState(): Promise<void>;
    /**
     * Reads a patch blob, using patchBlobStorage for encrypted patches.
     */
    _readPatchBlob(patchMeta: {
        patchOid: string;
        storage?: PatchStorageRoute;
        encrypted?: boolean;
    }): Promise<Uint8Array>;
    /**
     * Joins an external WarpState into the cached state using CRDT merge.
     */
    join(otherState: WarpState): {
        state: WarpState;
        receipt: JoinReceipt;
    };
    /**
     * Compares two version vectors for equality.
     */
    _frontierEquals(a: VersionVector, b: VersionVector): boolean;
}
export {};
