/**
 * WarpState — the core CRDT materialized state object.
 *
 * Holds the alive sets (OR-Set for nodes and edges), property registers
 * (LWW), the observed version vector frontier, and edge birth events.
 *
 * @module domain/services/state/WarpState
 */
import ORSet from '../../crdt/ORSet.ts';
import VersionVector from '../../crdt/VersionVector.ts';
import { type LWWRegister } from '../../crdt/LWW.ts';
import { type EventId } from '../../utils/EventId.ts';
import AttachmentRecord from '../../graph/AttachmentRecord.ts';
import EdgeId from '../../graph/EdgeId.ts';
import EdgeRecord from '../../graph/EdgeRecord.ts';
import NodeId from '../../graph/NodeId.ts';
import NodeRecord from '../../graph/NodeRecord.ts';
import type { PropValue } from '../../types/PropValue.ts';
/** Decoded node property entry yielded by WarpState.nodeProperties(). */
export type NodePropertyEntry = {
    readonly encodedKey: string;
    readonly nodeId: string;
    readonly key: string;
    readonly register: LWWRegister<PropValue>;
};
/** Decoded edge property entry yielded by WarpState.edgeProperties(). */
export type EdgePropertyEntry = {
    readonly encodedKey: string;
    readonly from: string;
    readonly to: string;
    readonly label: string;
    readonly key: string;
    readonly register: LWWRegister<PropValue>;
};
export type WarpStatePropertyRegisterSource = {
    readonly prop: Map<string, LWWRegister<PropValue>>;
};
/**
 * The CRDT materialized state for a WARP graph.
 *
 * This is an entity, not a frozen value object: reduce paths mutate a
 * live instance in place for performance, then callers clone or snapshot
 * before handing state to consumers that expect isolation.
 */
export default class WarpState {
    nodeAlive: ORSet;
    edgeAlive: ORSet;
    private prop;
    observedFrontier: VersionVector;
    /** EdgeKey → EventId of most recent EdgeAdd (for clean-slate prop visibility). */
    edgeBirthEvent: Map<string, EventId>;
    constructor(fields: {
        nodeAlive: ORSet;
        edgeAlive: ORSet;
        prop: Map<string, LWWRegister<PropValue>>;
        observedFrontier: VersionVector;
        edgeBirthEvent?: Map<string, EventId>;
    });
    /** Creates an empty state with fresh OR-Sets and version vector. */
    static empty(): WarpState;
    /** Returns live graph nodes as deterministic runtime-backed records. */
    nodeRecords(): readonly NodeRecord[];
    /** Returns the live graph node record for the given id, if present. */
    getNodeRecord(nodeId: string | NodeId): NodeRecord | null;
    /** Returns true when the given node id has a live graph node record. */
    hasNodeRecord(nodeId: string | NodeId): boolean;
    /** Returns the total number of stored property registers (node + edge). */
    propSize(): number;
    /** Returns true when the given encoded prop key has a stored register. */
    hasProp(encodedKey: string): boolean;
    /** Returns true when the given node has a stored property register for the key. */
    hasNodeProp(nodeId: string, key: string): boolean;
    /** Returns the property register for a node property, or undefined if absent. */
    getNodeProp(nodeId: string, key: string): LWWRegister<PropValue> | undefined;
    /** Returns the property register for an edge property, or undefined if absent. */
    getEdgeProp(from: string, to: string, label: string, key: string): LWWRegister<PropValue> | undefined;
    /** Returns the property register for an already-encoded key, or undefined if absent. */
    getEncodedProp(encodedKey: string): LWWRegister<PropValue> | undefined;
    /**
     * LWW-max write: updates the register for an already-encoded key if the
     * incoming event wins the last-writer-wins comparison.
     */
    mutatePropLWW(encodedKey: string, eventId: EventId, value: PropValue): void;
    /** LWW-max write for callers that already hold a validated property register. */
    mutatePropRegisterLWW(encodedKey: string, register: LWWRegister<PropValue>): void;
    /** Yields every node property register with decoded identity. */
    nodeProperties(): Generator<NodePropertyEntry>;
    /** Yields every edge property register with decoded identity. */
    edgeProperties(): Generator<EdgePropertyEntry>;
    /**
     * Yields node property entries from a raw prop map.
     * Use when a prop map is available but WarpState is not (e.g. stream sessions).
     */
    static nodePropertiesFromMap(prop: Map<string, LWWRegister<PropValue>>): Generator<NodePropertyEntry>;
    /**
     * Yields node property entries from either a live WarpState or a structural
     * state snapshot whose prop map has not been hydrated into WarpState yet.
     */
    static nodePropertiesFromState(state: WarpState | WarpStatePropertyRegisterSource): Generator<NodePropertyEntry>;
    /**
     * Yields edge property entries from a raw prop map.
     * Use when a prop map is available but WarpState is not (e.g. stream sessions).
     */
    static edgePropertiesFromMap(prop: Map<string, LWWRegister<PropValue>>): Generator<EdgePropertyEntry>;
    /**
     * Yields edge property entries from either a live WarpState or a structural
     * state snapshot whose prop map has not been hydrated into WarpState yet.
     */
    static edgePropertiesFromState(state: WarpState | WarpStatePropertyRegisterSource): Generator<EdgePropertyEntry>;
    /** Yields all prop entries (node + edge) with their encoded keys, for checkpoint serialization. */
    allPropEntries(): Generator<[string, LWWRegister<PropValue>]>;
    /**
     * Yields all prop entries from either a live WarpState or a structural state
     * snapshot whose prop map has not been hydrated into WarpState yet.
     */
    static allPropEntriesFromState(state: WarpState | WarpStatePropertyRegisterSource): Generator<[string, LWWRegister<PropValue>]>;
    /** Returns visible graph edges as deterministic runtime-backed records. */
    edgeRecords(): readonly EdgeRecord[];
    /** Returns the visible graph edge record for the given id, if present. */
    getEdgeRecord(edgeId: string | EdgeId): EdgeRecord | null;
    /** Returns true when the given edge id has a visible graph edge record. */
    hasEdgeRecord(edgeId: string | EdgeId): boolean;
    /** Returns visible node and edge payloads as deterministic attachment records. */
    attachmentRecords(): readonly AttachmentRecord[];
    /** Creates a deep clone with independent data structures. */
    clone(): WarpState;
    /**
     * Normalizes a plain-object or deserialized state into a live
     * `WarpState` instance with cloned inner structures. Used by the
     * reducer and checkpoint loader to accept either class instances or
     * hydrated POJOs at the boundary.
     */
    static cloneFromSnapshot(state: WarpState | {
        readonly nodeAlive: ORSet;
        readonly edgeAlive: ORSet;
        readonly prop: Map<string, LWWRegister<PropValue>>;
        readonly observedFrontier: VersionVector;
        readonly edgeBirthEvent?: Map<string, EventId>;
    }): WarpState;
    /**
     * CRDT join with another state. Pure — does not mutate either input.
     * Components merge as:
     * - `nodeAlive` / `edgeAlive`: OR-Set join
     * - `prop`: LWW-Max per key
     * - `observedFrontier`: VersionVector merge (component-wise max)
     * - `edgeBirthEvent`: EventId max per edge key
     */
    join(other: WarpState): WarpState;
    /**
     * Folds a patch's context version vector AND its own dot
     * (writer, lamport) into this state's `observedFrontier`. Mutates
     * `this.observedFrontier` in place.
     */
    foldPatch(patch: {
        readonly writer: string;
        readonly lamport: number;
        readonly context: VersionVector | Map<string, number> | Record<string, number> | null | undefined;
    }): void;
    /** LWW-Max merge of two property maps. */
    private static _mergeProps;
    /** EventId-max merge of two edge-birth-event maps. */
    private static _mergeEdgeBirthEvent;
}
