"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genericGetTransactionStatus = void 0;
const errors_1 = require("@ledgerhq/errors");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const alpaca_1 = require("./alpaca");
const utils_1 = require("./utils");
// => alpaca validateIntent
function genericGetTransactionStatus(network, kind) {
    return async (account, transaction) => {
        const alpacaApi = (0, alpaca_1.getAlpacaApi)(account.currency.id, kind);
        const draftTransaction = {
            mode: transaction?.mode ?? "send",
            recipient: transaction.recipient,
            amount: transaction.amount ?? new bignumber_js_1.default(0),
            useAllAmount: !!transaction.useAllAmount,
            assetReference: transaction.assetReference || "",
            assetOwner: transaction.assetOwner || "",
            subAccountId: transaction.subAccountId || "",
            memoType: transaction.memoType || "",
            memoValue: transaction.memoValue || "",
            family: transaction.family,
            feesStrategy: transaction.feesStrategy,
            data: transaction.data,
            type: transaction.type,
            sponsored: transaction.sponsored,
        };
        if (alpacaApi.getChainSpecificRules) {
            const chainSpecificValidation = alpacaApi.getChainSpecificRules();
            if (chainSpecificValidation.getTransactionStatus.throwIfPendingOperation) {
                if (account.pendingOperations.length > 0) {
                    throw new errors_1.AccountAwaitingSendPendingOperations();
                }
            }
        }
        const fees = BigInt(transaction.fees?.toString() || "0");
        const feesParameters = {
            ...(transaction.gasLimit ? { gasLimit: BigInt(transaction.gasLimit.toFixed()) } : {}),
            ...(transaction.gasPrice ? { gasPrice: BigInt(transaction.gasPrice.toFixed()) } : {}),
            ...(transaction.maxFeePerGas
                ? { maxFeePerGas: BigInt(transaction.maxFeePerGas.toFixed()) }
                : {}),
            ...(transaction.maxPriorityFeePerGas
                ? { maxPriorityFeePerGas: BigInt(transaction.maxPriorityFeePerGas.toFixed()) }
                : {}),
            ...(transaction.additionalFees
                ? { additionalFees: BigInt(transaction.additionalFees.toFixed()) }
                : {}),
        };
        const { errors, warnings, estimatedFees, amount, totalSpent, totalFees } = await alpacaApi.validateIntent((0, utils_1.transactionToIntent)(account, draftTransaction, alpacaApi.computeIntentType), { value: fees, parameters: feesParameters });
        return {
            errors,
            warnings,
            totalFees: typeof totalFees === "bigint" ? new bignumber_js_1.default(totalFees.toString()) : undefined,
            estimatedFees: !transaction.fees || transaction.fees.isZero()
                ? new bignumber_js_1.default(estimatedFees.toString())
                : transaction.fees,
            amount: transaction.amount.eq(0) ? new bignumber_js_1.default(amount.toString()) : transaction.amount,
            totalSpent: new bignumber_js_1.default(totalSpent.toString()),
        };
    };
}
exports.genericGetTransactionStatus = genericGetTransactionStatus;
//# sourceMappingURL=getTransactionStatus.js.map