import { Dot } from './Dot.ts';
/**
 * @fileoverview ORSet - Observed-Remove Set with Add-Wins Semantics
 *
 * An ORSet (Observed-Remove Set) is a CRDT that allows concurrent add and
 * remove operations on a set while guaranteeing convergence. This implementation
 * uses "add-wins" semantics: when an add and remove happen concurrently, the
 * add wins.
 *
 * ## Add-Wins Semantics
 *
 * The key insight of OR-Sets is that removals only affect adds they have
 * *observed*. When you remove an element, you're really saying "remove all
 * the add operations I've seen for this element." Any concurrent add (one
 * you haven't seen) survives.
 *
 * This is implemented via dots:
 * - Each add operation is tagged with a unique dot (writerId, counter)
 * - Remove records which dots it has observed (the "observed set")
 * - The element is present if ANY of its dots is not tombstoned
 *
 * ## Global Tombstones
 *
 * This implementation uses a **global tombstone set** rather than per-element
 * tombstones. This is an optimization for space efficiency.
 *
 * ## Semilattice Properties
 *
 * join() forms a join-semilattice:
 * - **Commutative**: a.join(b) equals b.join(a)
 * - **Associative**: a.join(b).join(c) equals a.join(b.join(c))
 * - **Idempotent**: a.join(a) equals a
 *
 * @module crdt/ORSet
 */
import type VersionVector from './VersionVector.ts';
/** Serialized form of an ORSet for CBOR encoding. */
interface SerializedORSet {
    entries: Array<[string, string[]]>;
    tombstones: string[];
}
/** Input for deserialization — entries and tombstones may be absent. */
interface DeserializeInput {
    entries?: Array<[string, string[]]>;
    tombstones?: string[];
}
/**
 * ORSet (Observed-Remove Set) — a CRDT set supporting concurrent add
 * and remove operations with add-wins semantics.
 *
 * This is a GLOBAL OR-Set (one per category, not per element). It tracks:
 * - entries: Map<element, Set<encodedDot>> — elements and the dots that added them
 * - tombstones: Set<encodedDot> — global tombstones for removed dots
 *
 * An element is present if it has at least one non-tombstoned dot.
 *
 * Fields are public because JoinReducer (the merge engine) needs direct
 * access for performance-critical operations.
 */
export default class ORSet {
    /** Element to dots that added it. */
    entries: Map<string, Set<string>>;
    /** Global tombstones for removed dots. */
    tombstones: Set<string>;
    /**
     * Creates an ORSet from existing data structures.
     */
    constructor(entries: Map<string, Set<string>>, tombstones: Set<string>);
    /** Creates an empty ORSet. */
    static empty(): ORSet;
    /** Deserializes a plain object back to an ORSet. */
    static deserialize(obj: DeserializeInput): ORSet;
    /**
     * Adds an element with the given dot.
     * Mutates the set.
     */
    add(element: string, dot: Dot): void;
    /**
     * Removes an element by adding its observed dots to the tombstones.
     * Mutates the set.
     */
    remove(observedDots: Set<string>): void;
    /**
     * Checks if an element is present.
     * An element is present if it has at least one non-tombstoned dot.
     */
    contains(element: string): boolean;
    /**
     * Returns all present elements.
     * Only returns elements that have at least one non-tombstoned dot.
     */
    elements(): string[];
    /**
     * Counts the total number of dot entries across all elements
     * (tombstoned and live alike).
     */
    countEntries(): number;
    /**
     * Counts live (non-tombstoned) dots across all elements.
     */
    countLiveDots(): number;
    /**
     * Counts tombstones that reference entry dots. Only counts tombstones
     * that actually correspond to dots in `entries` — floating tombstones
     * (for dots the replica has never observed as live) are ignored.
     */
    countTombstones(): number;
    /**
     * Returns the non-tombstoned dots for an element.
     */
    getDots(element: string): Set<string>;
    /**
     * Returns true iff the element is tagged by the given encoded dot.
     * Tombstone status is ignored — this tests raw entry membership.
     */
    hasDot(element: string, encodedDot: string): boolean;
    /**
     * Returns true iff the given encoded dot has been tombstoned.
     */
    isTombstoned(encodedDot: string): boolean;
    /**
     * Iterates `[element, dots]` pairs across all entries. The yielded
     * `dots` set includes tombstoned and live dots alike.
     */
    entriesIter(): IterableIterator<[string, ReadonlySet<string>]>;
    /**
     * Iterates every encoded dot across all entries, tombstoned or not.
     */
    entryDotsIter(): IterableIterator<string>;
    /**
     * Iterates every tombstoned encoded dot, including floating
     * tombstones that have no corresponding entry.
     */
    tombstonesIter(): IterableIterator<string>;
    /**
     * Joins with another ORSet by taking the union of entries and tombstones.
     * Returns a new ORSet; does not mutate either input.
     *
     * Properties:
     * - Commutative: a.join(b) equals b.join(a)
     * - Associative: a.join(b).join(c) equals a.join(b.join(c))
     * - Idempotent: a.join(a) equals a
     */
    join(other: ORSet): ORSet;
    /**
     * Compacts the ORSet by removing tombstoned dots that are <= includedVV.
     * Mutates the set.
     *
     * ## GC Safety Invariant
     *
     * Only compact dots that ALL replicas have observed. The `includedVV`
     * parameter represents the "stable frontier" — the version vector that
     * all known replicas have reached.
     */
    compact(includedVV: VersionVector): void;
    /** Creates a deep clone. */
    clone(): ORSet;
    /**
     * Returns a clone that retains only entries whose element matches
     * the predicate. All tombstones are copied verbatim, regardless of
     * whether their owning element survives the filter.
     */
    scopedClone(includeElement: (element: string) => boolean): ORSet;
    /**
     * Serializes to a plain object for CBOR encoding.
     * Entries are sorted by element; dots within entries are sorted.
     * Tombstones are sorted.
     */
    serialize(): SerializedORSet;
}
export {};
