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 { parseArrayLikeToDetails } from '../../utils/content';
import { addContenToIpfs, getIPFSContent } from '../../utils/utils-ipfs';
function withCybFeatures(Base, options) {
    return class CybIpfsNodeMixin extends Base {
        fetchWithDetails(cid, parseAs) {
            return __awaiter(this, void 0, void 0, function* () {
                const response = yield getIPFSContent(cid, this);
                const details = (response === null || response === void 0 ? void 0 : response.result)
                    ? yield parseArrayLikeToDetails(response, cid)
                    : undefined;
                return !parseAs
                    ? details
                    : (details === null || details === void 0 ? void 0 : details.type) === parseAs
                        ? details
                        : undefined;
            });
        }
        addContent(content) {
            return __awaiter(this, void 0, void 0, function* () {
                return addContenToIpfs(this, content);
            });
        }
        isConnectedToSwarm() {
            const _super = Object.create(null, {
                getPeers: { get: () => super.getPeers }
            });
            return __awaiter(this, void 0, void 0, function* () {
                return !!(yield _super.getPeers.call(this)).find((peerId) => peerId === options.swarmPeerId);
            });
        }
        reconnectToSwarm(lastConnectedTimestamp) {
            const _super = Object.create(null, {
                connectPeer: { get: () => super.connectPeer }
            });
            return __awaiter(this, void 0, void 0, function* () {
                if (!(yield this.isConnectedToSwarm())) {
                    _super.connectPeer.call(this, options.swarmPeerAddress)
                        .then(() => {
                        console.log(`🐝 connected to swarm - ${options.swarmPeerAddress}`);
                        return true;
                    })
                        .catch((err) => {
                        console.log(`Can't connect to swarm ${options.swarmPeerAddress}: ${err.message}`);
                        return false;
                    });
                }
            });
        }
    };
}
export { withCybFeatures };
//# sourceMappingURL=withCybFeatures.js.map