"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultisigRelatedFilterTransform = void 0;
const stream_1 = require("stream");
class MultisigRelatedFilterTransform extends stream_1.Transform {
    constructor(multisigContractAddress, tokenAddress) {
        super({ objectMode: true });
        this.multisigContractAddress = multisigContractAddress;
        this.tokenAddress = tokenAddress;
    }
    async _transform(tx, _, done) {
        let hasEffects = false;
        if (tx.effects && tx.effects.length) {
            // All internal to and from multisig - if tokenAddress is undefined then it only gets native transfers
            const walletRelatedInternalTxs = tx.effects.filter((internalTx) => {
                if (this.tokenAddress) {
                    return [internalTx.to, internalTx.from].includes(this.multisigContractAddress) && internalTx.contractAddress && internalTx.contractAddress.toLowerCase() == this.tokenAddress.toLowerCase();
                }
                else {
                    // contractAddress is undefined on native asset transfers
                    return [internalTx.to, internalTx.from].includes(this.multisigContractAddress) && !internalTx.contractAddress;
                }
            });
            // Create a tx object for each internal transfer
            for (const internalTx of walletRelatedInternalTxs) {
                const _tx = Object.assign({}, tx);
                _tx.value = Number(internalTx.amount);
                _tx.to = internalTx.to;
                _tx.from = internalTx.from;
                this.push(_tx);
            }
            // If we didn't find any internal transfers, original tx may be inconsequential
            hasEffects = !!walletRelatedInternalTxs.length;
        }
        if (!hasEffects && tx.to !== this.multisigContractAddress) {
            // If no effects and tx isn't to multisig, we don't care about original tx, return done()
            return done();
        }
        this.push(tx);
        return done();
    }
}
exports.MultisigRelatedFilterTransform = MultisigRelatedFilterTransform;
//# sourceMappingURL=multisigTransform.js.map