import { PersistentStore } from '../../state/persistent-store.js';
import { getDistributedNodeHostContract } from './distributed-runtime-contract.js';
import { attachDistributedRuntime, getDistributedRuntimeSnapshot, listDistributedRuntimeAudit, listDistributedRuntimePairRequests, listDistributedRuntimePeers, listDistributedRuntimeWork, startDistributedRuntime, } from './distributed-runtime-store.js';
import { approveDistributedPairRequest, authenticateDistributedPeerToken, disconnectDistributedPeer, rejectDistributedPairRequest, requestDistributedPairing, revokeDistributedPeerToken, rotateDistributedPeerToken, verifyDistributedPairRequest, heartbeatDistributedPeer, } from './distributed-runtime-pairing.js';
import { cancelDistributedWork, claimDistributedWork, completeDistributedWork, enqueueDistributedWork, invokeDistributedPeer, } from './distributed-runtime-work.js';
export class DistributedRuntimeManager {
    store;
    pairRequests = new Map();
    peers = new Map();
    work = new Map();
    audit = [];
    waiters = new Map();
    sessionBridge = null;
    approvalBridge = null;
    automationBridge = null;
    eventPublisher = null;
    loaded = false;
    constructor(storeOrPath) {
        this.store = typeof storeOrPath === 'string'
            ? new PersistentStore(storeOrPath)
            : storeOrPath;
    }
    attachRuntime(input) {
        attachDistributedRuntime(this, input);
    }
    async start() {
        await startDistributedRuntime(this);
    }
    listPairRequests(limit = 100) {
        return listDistributedRuntimePairRequests(this, limit);
    }
    listPeers(kind, limit = 200) {
        return listDistributedRuntimePeers(this, kind, limit);
    }
    listWork(limit = 200, peerId) {
        return listDistributedRuntimeWork(this, limit, peerId);
    }
    listAudit(limit = 100) {
        return listDistributedRuntimeAudit(this, limit);
    }
    getSnapshot() {
        return getDistributedRuntimeSnapshot(this);
    }
    getNodeHostContract() {
        return getDistributedNodeHostContract();
    }
    async requestPairing(input) {
        return requestDistributedPairing(this, input);
    }
    async approvePairRequest(requestId, input = {}) {
        return approveDistributedPairRequest(this, requestId, input);
    }
    async rejectPairRequest(requestId, input = {}) {
        return rejectDistributedPairRequest(this, requestId, input);
    }
    async verifyPairRequest(requestId, challenge, input = {}) {
        return verifyDistributedPairRequest(this, requestId, challenge, input);
    }
    async rotatePeerToken(peerId, input = {}) {
        return rotateDistributedPeerToken(this, peerId, input);
    }
    async revokePeerToken(peerId, input = {}) {
        return revokeDistributedPeerToken(this, peerId, input);
    }
    async disconnectPeer(peerId, input = {}) {
        return disconnectDistributedPeer(this, peerId, input);
    }
    async enqueueWork(input) {
        return enqueueDistributedWork(this, input);
    }
    async invokePeer(input) {
        return invokeDistributedPeer(this, input);
    }
    async authenticatePeerToken(tokenValue, remoteAddress) {
        return authenticateDistributedPeerToken(this, tokenValue, remoteAddress);
    }
    async heartbeatPeer(auth, input = {}) {
        return heartbeatDistributedPeer(this, auth, input);
    }
    async claimWork(auth, input = {}) {
        return claimDistributedWork(this, auth, input);
    }
    async completeWork(auth, workId, input = {}) {
        return completeDistributedWork(this, auth, workId, input);
    }
    async cancelWork(workId, input = {}) {
        return cancelDistributedWork(this, workId, input);
    }
}
