import { createScalableCuckooFilter } from '@libp2p/utils';
/**
 * Uses a Cuckoo filter to implement a mechanism for deduplicating PeerIds in a
 * way that uses a smaller amount of memory than a PeerSet.
 */
export class PeerFilter {
    filter;
    constructor(size, errorRate) {
        this.filter = createScalableCuckooFilter(size, errorRate);
    }
    has(peerId) {
        return this.filter.has(peerId.toMultihash().bytes);
    }
    add(peerId) {
        this.filter.add(peerId.toMultihash().bytes);
    }
    remove(peerId) {
        this.filter.remove?.(peerId.toMultihash().bytes);
    }
}
/**
 * Create and return a PeerFilter. This can be used by topologies to prevent
 * them receiving duplicate notifications for a peer that connects repeatedly.
 *
 * @example
 *
 * ```TypeScript
 * import { peerFilter } from '@libp2p/peer-collections'
 * import type { Registrar } from '@libp2p/interface-internal'
 *
 * const registrar: Registrar
 *
 * registrar.register('/my/protocol/1.0.0', {
 *   filter: peerFilter(),
 *   onConnect: (peerId) => {
 *     // will only be invoked for a given peerId once
 *   }
 * })
 * ```
 *
 * @param size - The maximum number of peers expected to be added to the filter
 * @param errorRate - The acceptable error rate
 */
export function peerFilter(size, errorRate = 0.001) {
    return new PeerFilter(size, errorRate);
}
//# sourceMappingURL=filter.js.map