"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
require("reflect-metadata");
const data_source_1 = require("../infrastructure/persistence/data-source");
const redis_client_1 = require("../infrastructure/cache/redis-client");
const repositories_factory_1 = require("../main/factories/repositories.factory");
const services_factory_1 = require("../main/factories/services.factory");
const token_1 = require("../domain/entities/token");
const run = async () => {
    await (0, data_source_1.initializeDatabase)();
    const repositories = (0, repositories_factory_1.createRepositories)();
    const services = (0, services_factory_1.createServices)(redis_client_1.redisClient);
    const { logger, coinMarketCapClient } = services;
    const [tokens, networks] = await Promise.all([
        repositories.tokenRepository.findAll(),
        repositories.networkRepository.findAll(),
    ]);
    const networkById = new Map(networks.map((network) => [network.id, network]));
    logger.info('Starting token nativity healing', {
        totalTokens: tokens.length,
        totalNetworks: networks.length,
    });
    const tokensWithNetwork = tokens.map((token) => {
        const network = networkById.get(token.networkId);
        return {
            id: token.id,
            name: token.name,
            symbol: token.symbol,
            address: token.address,
            networkName: network?.name ?? token.networkId,
            token,
        };
    });
    const coinEntries = await coinMarketCapClient.getTokenCoinIdsByNetwork(tokensWithNetwork);
    let healed = 0;
    let unchanged = 0;
    let undetermined = 0;
    for (const { token } of tokensWithNetwork) {
        const entry = coinEntries.get(token.id);
        if (!entry) {
            undetermined++;
            continue;
        }
        const isNative = entry.platform === null || entry.slug.toLowerCase() === entry.platform.slug.toLowerCase();
        if (isNative === token.isNative) {
            unchanged++;
            continue;
        }
        const updatedMcap = isNative ? token.mcap : 0;
        const updated = new token_1.Token(token.id, isNative, token.address, token.networkId, token.thumbnail, token.name, token.symbol, token.decimals, token.code, token.info, token.isActive, token.minimumSwapValue, updatedMcap, token.createdAt, new Date());
        await repositories.tokenRepository.update(updated);
        healed++;
        logger.info('Healed token nativity', {
            id: token.id,
            symbol: token.symbol,
            networkName: networkById.get(token.networkId)?.name ?? token.networkId,
            wasNative: token.isNative,
            isNative,
            cmcId: entry.id,
            cmcSlug: entry.slug,
            platformSlug: entry.platform?.slug ?? null,
            mcap: updatedMcap,
        });
    }
    logger.info('Token nativity healing complete', {
        totalTokens: tokens.length,
        healed,
        unchanged,
        undetermined,
    });
    await redis_client_1.redisClient.quit();
    process.exit(0);
};
run().catch((error) => {
    // eslint-disable-next-line no-console
    console.error('Token nativity healing failed', error);
    process.exit(1);
});
//# sourceMappingURL=heal-token-nativity.js.map