import { peerIdFromCID } from '@libp2p/peer-id';
import map from 'it-map';
import { withArrayBuffer } from 'uint8arrays/with-array-buffer';
function peerInfoToPeer(info) {
    return {
        ...info,
        id: info.id.toCID()
    };
}
class Libp2pRouter {
    name = 'libp2p-router';
    libp2p;
    constructor(libp2p) {
        this.libp2p = libp2p;
    }
    async provide(cid, options) {
        await this.libp2p.contentRouting.provide(cid, options);
    }
    async cancelReprovide(key, options) {
        await this.libp2p.contentRouting.cancelReprovide(key, options);
    }
    async *findProviders(cid, options) {
        yield* map(this.libp2p.contentRouting.findProviders(cid, options), prov => ({
            routing: this.name,
            ...peerInfoToPeer(prov)
        }));
    }
    async put(key, value, options) {
        await this.libp2p.contentRouting.put(key, value, options);
    }
    async get(key, options) {
        return withArrayBuffer(await this.libp2p.contentRouting.get(key, options));
    }
    async findPeer(peerId, options) {
        return peerInfoToPeer(await this.libp2p.peerRouting.findPeer(peerIdFromCID(peerId), options));
    }
    async *getClosestPeers(key, options) {
        yield* map(this.libp2p.peerRouting.getClosestPeers(key, options), peerInfoToPeer);
    }
    toString() {
        return 'Libp2pRouter()';
    }
}
export function libp2pRouting(libp2p) {
    return new Libp2pRouter(libp2p);
}
//# sourceMappingURL=routing.js.map