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());
    });
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import { asyncIterableBatchProcessor } from '../../../../../utils/async/iterable';
import { CID_TWEET } from '../../../../../utils/consts';
import { dateToNumber } from '../../../../../utils/date';
import { fetchCyberlinksIterable } from '../../../dataSource/blockchain/requests';
import { MAX_PARRALEL_LINKS } from '../consts';
import { CYBER_LINK_TRANSACTION_TYPE, } from '../../../dataSource/blockchain/types';
export const getUniqueParticlesFromLinks = (links) => [
    ...new Set([
        ...links.map((link) => link.to),
        ...links.map((link) => link.from),
    ]),
];
export const fetchCyberlinksAndResolveParticles = (cyberIndexUrl, cid, timestampUpdate, particlesResolver, queuePriority) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, e_1, _b, _c;
    const cyberlinksIterable = fetchCyberlinksIterable(cyberIndexUrl, cid, timestampUpdate);
    const links = [];
    try {
        for (var _d = true, cyberlinksIterable_1 = __asyncValues(cyberlinksIterable), cyberlinksIterable_1_1; cyberlinksIterable_1_1 = yield cyberlinksIterable_1.next(), _a = cyberlinksIterable_1_1.done, !_a;) {
            _c = cyberlinksIterable_1_1.value;
            _d = false;
            try {
                const batch = _c;
                links.push(...batch);
                const particles = getUniqueParticlesFromLinks(batch);
                if (particles.length > 0) {
                    yield asyncIterableBatchProcessor(particles, (cids) => particlesResolver.enqueue(cids.map((cid) => ({
                        id: cid,
                        priority: queuePriority,
                    }))), MAX_PARRALEL_LINKS);
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = cyberlinksIterable_1.return)) yield _b.call(cyberlinksIterable_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return links;
});
export function extractCybelinksFromTransaction(batch) {
    const cyberlinks = batch.filter((l) => l.type === CYBER_LINK_TRANSACTION_TYPE);
    const particlesFound = new Set();
    const links = [];
    const tweets = cyberlinks.reduce((acc, { value, transaction: { block: { timestamp }, }, }) => {
        value.links.forEach((link) => {
            particlesFound.add(link.to);
            particlesFound.add(link.from);
            links.push(Object.assign(Object.assign({}, link), { timestamp: dateToNumber(timestamp) }));
            if (link.from === CID_TWEET) {
                acc[link.to] = {
                    timestamp: dateToNumber(timestamp),
                    direction: 'from',
                    from: CID_TWEET,
                    to: link.to,
                };
            }
        });
        return acc;
    }, {});
    return {
        tweets,
        particlesFound: [...particlesFound],
        links,
    };
}
//# sourceMappingURL=links.js.map