import type { EventEmitter } from 'events';

export type TinyRaftEvents = {
    change: {
        state: 'LEADER'|'FOLLOWER'|'CANDIDATE',
        term: number,
        leader: string|number,
        followers: (string|number)[],
    }[],
};

export type RaftMessage = {
    type: 'VOTE_REQUEST'|'VOTE'|'PING'|'PONG',
    term: number,
    leader?: string|number,
    priority?: number,
};

export class TinyRaft extends EventEmitter<TinyRaftEvents>
{
    constructor(cfg: {
        nodes: (number|string)[],
        nodeId: number|string,
        electionTimeout?: number,
        heartbeatTimeout?: number,
        leadershipTimeout?: number,
        initialTerm?: number,
        leaderPriority?: number,
        send: (to: number|string, msg: RaftMessage) => void,
    });
    start();
    stop();
    onReceive(from: number|string, msg: RaftMessage);
    markAlive();
    setNodes(nodes: (number|string)[]);
}
