"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.seedTokensForNetwork = void 0;
const networks_seed_data_1 = require("./networks.seed-data");
const buildTokenThumbnail = (entry) => {
    const symbol = entry.symbol.toLowerCase();
    return `https://cryptologos.cc/logos/${entry.slug}-${symbol}-logo.png`;
};
const buildCreateTokenInput = (entry, network) => {
    const isNative = entry.platform === null;
    const address = isNative ? null : entry.platform?.token_address ?? null;
    const decimals = isNative ? (0, networks_seed_data_1.getNativeDecimals)(network.addressType, network.code) : 18;
    return {
        isNative,
        address,
        networkId: network.id,
        thumbnail: buildTokenThumbnail(entry),
        name: entry.name,
        symbol: entry.symbol,
        decimals,
        code: entry.slug,
        cmcId: entry.id,
        isActive: true,
    };
};
const seedTokensForNetwork = async (network, useCases, tokenRepository, coinMarketCapClient, logger, limit = 20) => {
    const entries = await coinMarketCapClient.getTopTokensByNetwork({ name: network.name, symbol: network.code }, limit);
    const existingTokens = await tokenRepository.findByNetworkId(network.id);
    const existingCodes = new Set(existingTokens.map((token) => token.code));
    let created = 0;
    let skipped = 0;
    for (const entry of entries) {
        if (existingCodes.has(entry.slug)) {
            logger.info('Token already exists, skipping', {
                network: network.name,
                token: entry.name,
                code: entry.slug,
            });
            skipped++;
            continue;
        }
        try {
            await useCases.createToken.execute(buildCreateTokenInput(entry, network));
            logger.info('Token created', { network: network.name, token: entry.name, code: entry.slug });
            created++;
        }
        catch (error) {
            logger.error('Failed to create token', {
                network: network.name,
                token: entry.name,
                code: entry.slug,
                error,
            });
        }
    }
    return { fetched: entries.length, created, skipped };
};
exports.seedTokensForNetwork = seedTokensForNetwork;
//# sourceMappingURL=seed-tokens.helper.js.map