"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pickUpToNPerNetwork = exports.groupByNetwork = exports.filterValerisTrendingTokens = void 0;
const ValerisNetwork_js_1 = require("../../../domain/value-objects/ValerisNetwork.js");
const filterValerisTrendingTokens = async (fetchTrending, limit = 10, networkCode) => {
    const targetNetwork = (0, ValerisNetwork_js_1.parseNetworkCode)(networkCode);
    const trending = await fetchTrending(limit * 3);
    const enriched = [];
    for (const token of trending) {
        const network = (0, ValerisNetwork_js_1.getTrendingTokenNetwork)(token);
        if (!network)
            continue;
        if (targetNetwork && network !== targetNetwork)
            continue;
        const contract = token.contractAddresses.find((c) => (0, ValerisNetwork_js_1.normalizeNetwork)(c.network) === network);
        enriched.push({
            ...token,
            valerisNetwork: network,
            contractAddress: contract?.address,
            rawNetworkName: contract?.network
        });
        if (enriched.length >= limit)
            break;
    }
    return enriched;
};
exports.filterValerisTrendingTokens = filterValerisTrendingTokens;
const groupByNetwork = (tokens) => {
    const groups = {};
    for (const token of tokens) {
        if (!groups[token.valerisNetwork])
            groups[token.valerisNetwork] = [];
        groups[token.valerisNetwork].push(token);
    }
    return groups;
};
exports.groupByNetwork = groupByNetwork;
const pickUpToNPerNetwork = (tokens, perNetwork) => {
    const groups = (0, exports.groupByNetwork)(tokens);
    const selected = [];
    for (const network of Object.keys(groups)) {
        selected.push(...groups[network].slice(0, perNetwork));
    }
    return selected;
};
exports.pickUpToNPerNetwork = pickUpToNPerNetwork;
