import { buildContractInput, invokeContractRoute, requireContractRoute, } from '../transport-http/index.js';
function splitArgs(args) {
    return args;
}
function requireEndpoint(contract, endpointId) {
    return requireContractRoute(contract.endpoints, endpointId, 'peer endpoint');
}
export function createPeerRemoteClient(transport, contract) {
    function invokeTyped(endpointId, input, options = {}) {
        return invokeContractRoute(transport, requireEndpoint(contract, endpointId), input, options);
    }
    const client = {
        transport,
        contract,
        listEndpoints() {
            return contract.endpoints;
        },
        getEndpoint(endpointId) {
            return requireEndpoint(contract, endpointId);
        },
        invoke: invokeTyped,
        pairing: {
            request: (...args) => invokeTyped('pair.request', ...args),
            verify: (...args) => invokeTyped('pair.verify', ...args),
        },
        peer: {
            heartbeat: (...args) => invokeTyped('peer.heartbeat', ...args),
        },
        work: {
            pull: (...args) => invokeTyped('work.pull', ...args),
            complete: (workId, ...args) => {
                const [input, options] = splitArgs(args);
                return invokeTyped('work.complete', buildContractInput('workId', workId, input), options);
            },
        },
        operator: {
            snapshot: (...args) => invokeTyped('operator.snapshot', ...args),
        },
    };
    return client;
}
