"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVMListTransactionsStream = void 0;
const config_1 = require("../../../../services/config");
const utils_1 = require("../../../../utils");
const streamWithEventPipe_1 = require("../../../../utils/streamWithEventPipe");
class EVMListTransactionsStream extends streamWithEventPipe_1.TransformWithEventPipe {
    constructor(walletAddresses, tokenAddress) {
        super({ objectMode: true });
        this.walletAddresses = walletAddresses;
        this.tokenAddress = tokenAddress;
    }
    async _transform(transaction, _, done) {
        const baseTx = {
            id: transaction._id,
            txid: transaction.txid,
            fee: transaction.fee,
            height: transaction.blockHeight,
            from: transaction.from,
            initialFrom: transaction.initialFrom || transaction.from,
            gasPrice: transaction.gasPrice,
            gasLimit: transaction.gasLimit,
            receipt: transaction.receipt,
            address: transaction.to,
            blockTime: transaction.blockTimeNormalized,
            error: transaction.error,
            network: transaction.network,
            chain: transaction.chain,
            nonce: transaction.nonce,
            effects: transaction.effects,
            callStack: transaction.callStack
        };
        // Add old properties if leanTxStorage is not enabled
        const config = config_1.Config.chainConfig({ chain: transaction.chain, network: transaction.network });
        if (!config || !config.leanTransactionStorage) {
            baseTx.abiType = transaction.abiType;
            baseTx.internal = transaction.internal;
            baseTx.calls = transaction.calls;
            baseTx.data = transaction.data ? transaction.data.toString() : '';
        }
        const sending = this.walletAddresses.includes(transaction.from);
        if (sending) {
            const sendingToOurself = this.walletAddresses.includes(transaction.to);
            if (!sendingToOurself) {
                baseTx.category = 'send';
                baseTx.satoshis = -transaction.value;
                this.push((0, utils_1.jsonStringify)(baseTx) + '\n');
            }
            else {
                baseTx.category = 'move';
                baseTx.satoshis = transaction.value;
                this.push((0, utils_1.jsonStringify)(baseTx) + '\n');
            }
        }
        else {
            baseTx.category = 'receive'; // assume it's a receive, but may not be sent
            const weReceived = this.walletAddresses.includes(transaction.to);
            const weReceivedInternal = (0, utils_1.overlaps)(this.walletAddresses, transaction.effects?.map(e => e.to));
            if (weReceivedInternal) {
                baseTx.satoshis = 0n;
                for (const effect of transaction.effects) {
                    if (this.walletAddresses.includes(effect.to) && (effect.contractAddress == this.tokenAddress)) {
                        baseTx.satoshis += BigInt(effect.amount || 0);
                    }
                }
                this.push((0, utils_1.jsonStringify)(baseTx) + '\n');
            }
            else if (weReceived) {
                // console.log(weReceived, weReceivedInternal, transaction.to, this.walletAddresses, transaction);
                baseTx.satoshis = BigInt(transaction.value || 0);
                this.push((0, utils_1.jsonStringify)(baseTx) + '\n');
            }
        }
        return done();
    }
}
exports.EVMListTransactionsStream = EVMListTransactionsStream;
//# sourceMappingURL=transform.js.map