"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RippleWalletTransactions = void 0;
const stream_1 = require("stream");
const walletAddress_1 = require("../../../models/walletAddress");
class RippleWalletTransactions extends stream_1.Transform {
    constructor(wallet, csp) {
        super({ objectMode: true });
        this.wallet = wallet;
        this.csp = csp;
    }
    async getAddresses() {
        const { chain, network, _id } = this.wallet;
        if (!this.walletAddresses) {
            this.walletAddresses = await walletAddress_1.WalletAddressStorage.collection.find({ chain, network, wallet: _id }).toArray();
        }
        return this.walletAddresses;
    }
    async _transform(tx, _, done) {
        const { network } = this.wallet;
        const transaction = this.csp.transform(tx, network);
        const changes = tx.outcome.balanceChanges;
        const changed = Object.keys(changes);
        const relevantAddresses = (await this.getAddresses()).filter(w => changed.includes(w.address)).map(w => w.address);
        let sending = false;
        let receiving = false;
        for (const address of relevantAddresses) {
            for (const output of changes[address]) {
                if (Number(output.value) > 0) {
                    receiving = true;
                }
                if (Number(output.value) < 0) {
                    sending = true;
                }
            }
            for (const output of changes[address]) {
                if (sending) {
                    this.push(JSON.stringify({
                        id: tx.hash,
                        txid: tx.hash,
                        fee: transaction.fee * 1e6,
                        size: 0,
                        category: receiving ? 'move' : 'send',
                        satoshis: -1 * Number(output.value) * 1e6,
                        height: transaction.blockHeight,
                        address,
                        outputIndex: changed.indexOf(address),
                        blockTime: transaction.blockTimeNormalized
                    }) + '\n');
                    if (transaction.fee > 0) {
                        this.push(JSON.stringify({
                            id: tx.hash,
                            txid: tx.hash,
                            category: 'fee',
                            satoshis: -1 * Number(transaction.fee) * 1e6,
                            height: transaction.blockHeight,
                            blockTime: transaction.blockTimeNormalized
                        }) + '\n');
                    }
                    return done();
                }
                else {
                    if (receiving) {
                        this.push(JSON.stringify({
                            id: tx.hash,
                            txid: tx.hash,
                            fee: transaction.fee * 1e6,
                            size: 0,
                            category: 'receive',
                            satoshis: Number(output.value) * 1e6,
                            height: transaction.blockHeight,
                            address,
                            outputIndex: changed.indexOf(address),
                            blockTime: transaction.blockTimeNormalized
                        }) + '\n');
                    }
                }
            }
        }
        done();
    }
}
exports.RippleWalletTransactions = RippleWalletTransactions;
//# sourceMappingURL=transform.js.map