"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.calcTxCost = exports.calcIndividualTxCost = void 0;
const polymesh_sdk_1 = require("@polymathnetwork/polymesh-sdk");
const calcIndividualTxCost = (transaction, userAddress) => __awaiter(void 0, void 0, void 0, function* () {
    let cost = new polymesh_sdk_1.BigNumber(0);
    try {
        const dispatchInfo = yield transaction
            .method(...transaction.params)
            .paymentInfo(userAddress);
        cost = new polymesh_sdk_1.BigNumber(dispatchInfo.partialFee.toString()).div(new polymesh_sdk_1.BigNumber(1000000));
    }
    catch (error) {
        // eslint-disable-next-line no-console
        console.error('Error when calculating tx cost for transaction', transaction);
    }
    return cost;
});
exports.calcIndividualTxCost = calcIndividualTxCost;
const calcTxCost = (transactions = [], userAddress) => __awaiter(void 0, void 0, void 0, function* () {
    const costs = yield Promise.all(transactions.map((tx) => exports.calcIndividualTxCost(tx, userAddress)));
    return costs.reduce((totalCost, current) => totalCost.plus(current), new polymesh_sdk_1.BigNumber(0));
});
exports.calcTxCost = calcTxCost;
//# sourceMappingURL=calcTxCost.js.map