"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genericEstimateMaxSpendable = void 0;
const account_1 = require("../../account");
const alpaca_1 = require("./alpaca");
const createTransaction_1 = require("./createTransaction");
const utils_1 = require("./utils");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function genericEstimateMaxSpendable(network, kind) {
    return async ({ account, parentAccount, transaction }) => {
        const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
        const draftTransaction = {
            ...(0, createTransaction_1.createTransaction)(account),
            ...transaction,
            amount: mainAccount.spendableBalance,
        };
        const fees = await (0, alpaca_1.getAlpacaApi)(network, kind).estimateFees((0, utils_1.transactionToIntent)(mainAccount, draftTransaction));
        const bnFee = (0, bignumber_js_1.default)(fees.value.toString());
        return bignumber_js_1.default.max(0, account.spendableBalance.minus(bnFee));
    };
}
exports.genericEstimateMaxSpendable = genericEstimateMaxSpendable;
//# sourceMappingURL=estimateMaxSpendable.js.map