var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { create as createKuboClient } from 'kubo-rpc-client';
import { multiaddr } from '@multiformats/multiaddr';
import { stringToCid, stringToIpfsPath } from '../../utils/cid';
import { CYBER_GATEWAY_URL } from '../../config';
class KuboNode {
    constructor() {
        this.nodeType = 'external';
        this._config = {};
        this._isStarted = false;
    }
    get config() {
        return this._config;
    }
    get isStarted() {
        return this._isStarted;
    }
    initConfig() {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield this.node.config.get('Addresses.Gateway');
            if (!response) {
                return { gatewayUrl: CYBER_GATEWAY_URL };
            }
            const address = multiaddr(response).nodeAddress();
            return { gatewayUrl: `http://${address.address}:${address.port}` };
        });
    }
    init(options) {
        return __awaiter(this, void 0, void 0, function* () {
            this.node = createKuboClient(options);
            this._config = yield this.initConfig();
            if (typeof window !== 'undefined') {
                window.node = this.node;
                window.toCid = stringToCid;
            }
            console.log('IPFS - Kubo addrs', (yield this.node.swarm.localAddrs()).map((a) => a.toString()));
            this._isStarted = true;
        });
    }
    stat(cid, options = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.node.files.stat(stringToIpfsPath(cid), Object.assign(Object.assign({}, options), { withLocal: true, size: true })).then((result) => {
                const { type, size, sizeLocal, local, blocks } = result;
                return {
                    type,
                    size: size || -1,
                    sizeLocal: sizeLocal || -1,
                    blocks,
                };
            });
        });
    }
    cat(cid, options = {}) {
        return this.node.cat(stringToCid(cid), options);
    }
    add(content, options = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.node.add(content, options)).cid.toString();
        });
    }
    pin(cid, options = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.node.pin.add(stringToCid(cid), options)).toString();
        });
    }
    getPeers() {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.node.swarm.peers()).map((c) => c.peer.toString());
        });
    }
    stop() {
        return __awaiter(this, void 0, void 0, function* () { });
    }
    start() {
        return __awaiter(this, void 0, void 0, function* () { });
    }
    connectPeer(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const addr = multiaddr(address);
            yield this.node.bootstrap.add(addr);
            yield this.node.swarm.connect(addr);
            return true;
        });
    }
    ls() {
        return this.node.pin.ls();
    }
    info() {
        return __awaiter(this, void 0, void 0, function* () {
            const { repoSize } = yield this.node.stats.repo();
            const responseId = yield this.node.id();
            const { agentVersion, id } = responseId;
            return { id: id.toString(), agentVersion, repoSize };
        });
    }
}
export default KuboNode;
//# sourceMappingURL=kubo.js.map