"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetWalletBalancesForNetworkUseCase = void 0;
const not_found_error_1 = require("../../../domain/errors/not-found-error");
class GetWalletBalancesForNetworkUseCase {
    walletRepository;
    networkRepository;
    addressRepository;
    tokenRepository;
    transactionRepository;
    balanceService;
    constructor(walletRepository, networkRepository, addressRepository, tokenRepository, transactionRepository, balanceService) {
        this.walletRepository = walletRepository;
        this.networkRepository = networkRepository;
        this.addressRepository = addressRepository;
        this.tokenRepository = tokenRepository;
        this.transactionRepository = transactionRepository;
        this.balanceService = balanceService;
    }
    async execute(walletId, networkId) {
        const wallet = await this.walletRepository.findById(walletId);
        if (!wallet) {
            throw new not_found_error_1.NotFoundError('Wallet', walletId);
        }
        const network = await this.networkRepository.findById(networkId);
        if (!network) {
            throw new not_found_error_1.NotFoundError('Network', networkId);
        }
        const addresses = await this.addressRepository.findByWalletId(walletId);
        const networkAddresses = addresses.filter((address) => address.type === network.addressType);
        const addressIds = networkAddresses.map((address) => address.id);
        if (addressIds.length === 0) {
            return [];
        }
        const transactions = await this.transactionRepository.findByAddressIds(addressIds);
        const networkTransactions = transactions.filter((transaction) => transaction.networkId === networkId);
        const tokens = await this.tokenRepository.findByNetworkId(networkId);
        const balances = this.balanceService.calculate(networkAddresses, networkTransactions, tokens);
        return balances;
    }
}
exports.GetWalletBalancesForNetworkUseCase = GetWalletBalancesForNetworkUseCase;
//# sourceMappingURL=get-wallet-balances-for-network.use-case.js.map