"use strict";
/**
 * Blockscout token-transfer enrichment — Chain 138 txs are mostly ERC-20; native `value` is often 0.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.pickPrimaryTokenTransfer = pickPrimaryTokenTransfer;
exports.fetchTokenTransfersForTx = fetchTokenTransfersForTx;
/** Pick the largest ERC-20 transfer in a tx (typical payment). */
function pickPrimaryTokenTransfer(items) {
    let best = null;
    for (const item of items) {
        const token = item.token?.address;
        const raw = item.total?.value;
        if (!token || !raw)
            continue;
        const value = BigInt(raw);
        if (value === 0n)
            continue;
        const decimals = parseInt(item.token?.decimals || '18', 10);
        const summary = {
            token,
            tokenSymbol: item.token?.symbol || '',
            tokenDecimals: Number.isFinite(decimals) ? decimals : 18,
            from: item.from?.hash || '',
            to: item.to?.hash || '',
            value,
            logIndex: item.log_index ?? 0,
        };
        if (!best || summary.value > best.value)
            best = summary;
    }
    return best;
}
async function fetchTokenTransfersForTx(apiBase, txHash) {
    const base = apiBase.replace(/\/$/, '');
    const url = `${base}/transactions/${txHash}/token-transfers`;
    const res = await fetch(url);
    if (!res.ok)
        return null;
    const body = (await res.json());
    return pickPrimaryTokenTransfer(body.items ?? []);
}
