"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.depositFunds = depositFunds;
exports.withdrawFunds = withdrawFunds;
exports.withdrawAllAvailableFunds = withdrawAllAvailableFunds;
const address_1 = require("@ethersproject/address");
const constants_1 = require("@ethersproject/constants");
const interface_1 = require("./interface");
const subgraph_1 = require("./subgraph");
// Implementation
function depositFunds(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { fundsTokenAddress = constants_1.AddressZero } = args;
        if (!(0, address_1.isAddress)(fundsTokenAddress)) {
            throw new Error(`Invalid fundsTokenAddress: ${fundsTokenAddress}`);
        }
        const isNativeCoin = fundsTokenAddress === constants_1.AddressZero;
        const transactionRequest = {
            to: args.contractAddress,
            data: (0, interface_1.encodeDepositFunds)(args.entityId, fundsTokenAddress, args.fundsAmount),
            value: isNativeCoin ? args.fundsAmount : "0"
        };
        if (args.returnTxInfo) {
            return transactionRequest;
        }
        else {
            return args.web3Lib.sendTransaction(transactionRequest);
        }
    });
}
// Implementation
function withdrawFunds(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const transactionRequest = {
            to: args.contractAddress,
            data: (0, interface_1.encodeWithdrawFunds)(args.entityId, args.tokensToWithdraw, args.amountsToWithdraw)
        };
        if (args.returnTxInfo) {
            return transactionRequest;
        }
        else {
            return args.web3Lib.sendTransaction(transactionRequest);
        }
    });
}
// Implementation
function withdrawAllAvailableFunds(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const funds = yield (0, subgraph_1.getFunds)(args.subgraphUrl, {
            fundsFilter: {
                accountId: args.entityId.toString()
            }
        });
        const { tokensToWithdraw, amountsToWithdraw } = funds
            .filter((fund) => Number(fund.availableAmount))
            .reduce((acc, fund) => {
            acc.tokensToWithdraw.push(fund.token.address);
            acc.amountsToWithdraw.push(fund.availableAmount);
            return acc;
        }, {
            tokensToWithdraw: [],
            amountsToWithdraw: []
        });
        const transactionRequest = {
            to: args.contractAddress,
            data: (0, interface_1.encodeWithdrawFunds)(args.entityId, tokensToWithdraw, amountsToWithdraw)
        };
        if (args.returnTxInfo) {
            return transactionRequest;
        }
        else {
            return args.web3Lib.sendTransaction(transactionRequest);
        }
    });
}
//# sourceMappingURL=handler.js.map