import { pEvent } from 'p-event';
import { mockMuxer } from "./mock-muxer.js";
import { multiaddrConnectionPair } from "./multiaddr-connection-pair.js";
import { echo } from "./stream-utils.js";
/**
 * Returns two streams connected to each other with a slight delay in sending
 * messages to simulate a network
 */
export async function streamPair(opts = {}) {
    const [outboundConnection, inboundConnection] = multiaddrConnectionPair({
        ...opts,
        outbound: opts.outboundConnection,
        inbound: opts.inboundConnection
    });
    const localMuxer = mockMuxer({
        streamOptions: opts.outbound
    }).createStreamMuxer(outboundConnection);
    const remoteMuxer = mockMuxer({
        streamOptions: opts.inbound
    }).createStreamMuxer(inboundConnection);
    const [inboundStream, outboundStream] = await Promise.all([
        pEvent(remoteMuxer, 'stream').then(evt => {
            return evt.detail;
        }),
        localMuxer.createStream({
            ...opts.outbound,
            protocol: opts.protocol
        })
    ]);
    return [
        outboundStream,
        inboundStream
    ];
}
export async function echoStream(opts = {}) {
    const [outbound, inbound] = await streamPair(opts);
    echo(inbound);
    return outbound;
}
//# sourceMappingURL=stream-pair.js.map