"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PolymeshTransactionBatch = void 0;
const internal_1 = require("../internal");
const conversion_1 = require("../utils/conversion");
const internal_2 = require("../utils/internal");
/**
 * Wrapper class for a batch of Polymesh Transactions
 */
class PolymeshTransactionBatch extends internal_1.PolymeshTransactionBase {
    /**
     * @hidden
     */
    constructor(transactionSpec, context) {
        const { args } = transactionSpec, rest = __rest(transactionSpec, ["args"]);
        super(rest, context);
        this.inputArgs = args;
        this.batchSize = args.length;
    }
    /**
     * Arguments for each transaction in the batch
     */
    get args() {
        if (!this.unwrappedArgs) {
            this.unwrappedArgs = this.inputArgs.map(arg => internal_2.unwrapValues(arg));
        }
        return this.unwrappedArgs;
    }
    /**
     * @hidden
     */
    composeTx() {
        const { tx, context: { polymeshApi: { tx: { utility }, }, }, args, } = this;
        return utility.batchAtomic(args.map(arg => tx(...arg)));
    }
    /**
     * @hidden
     */
    handleExtrinsicSuccess(resolve, reject, receipt) {
        // If one of the transactions in the batch fails, this event gets emitted
        const failed = receipt.findRecord('utility', 'BatchInterrupted');
        if (failed) {
            const { event: { data: failedData }, } = failed;
            const failedIndex = conversion_1.u32ToBigNumber(failedData[0]).toNumber();
            const dispatchError = failedData[1];
            this.handleExtrinsicFailure(resolve, reject, dispatchError, { failedIndex });
        }
        else {
            resolve(receipt);
        }
    }
}
exports.PolymeshTransactionBatch = PolymeshTransactionBatch;
//# sourceMappingURL=PolymeshTransactionBatch.js.map