"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSignatureArguments = exports.createPushOutcomeAndTransferAllTransaction = exports.createPushOutcomeTransaction = exports.createConcludeTransaction = exports.createConcludePushOutcomeAndTransferAllTransaction = exports.createCheckpointTransaction = exports.createRespondTransaction = exports.createChallengeTransaction = exports.NITRO_MAX_GAS = exports.MAX_TX_DATA_SIZE = void 0;
var forceMoveTrans = __importStar(require("./contract/transaction-creators/force-move"));
var nitroAdjudicatorTrans = __importStar(require("./contract/transaction-creators/nitro-adjudicator"));
var signatures_1 = require("./signatures");
exports.MAX_TX_DATA_SIZE = 128 * 1024;
exports.NITRO_MAX_GAS = 6000000;
function createChallengeTransaction(signedStates, challengePrivateKey) {
    var _a = createSignatureArguments(signedStates), states = _a.states, signatures = _a.signatures, whoSignedWhat = _a.whoSignedWhat;
    return forceMoveTrans.createChallengeTransaction(states, signatures, whoSignedWhat, challengePrivateKey);
}
exports.createChallengeTransaction = createChallengeTransaction;
function createRespondTransaction(challengeState, response) {
    if (!challengeState) {
        throw new Error('No active challenge in challenge state');
    }
    return forceMoveTrans.createRespondTransaction({
        challengeState: challengeState,
        responseState: response.state,
        responseSignature: response.signature,
    });
}
exports.createRespondTransaction = createRespondTransaction;
function createCheckpointTransaction(signedStates) {
    var _a = createSignatureArguments(signedStates), states = _a.states, signatures = _a.signatures, whoSignedWhat = _a.whoSignedWhat;
    return forceMoveTrans.createCheckpointTransaction({
        states: states,
        signatures: signatures,
        whoSignedWhat: whoSignedWhat,
    });
}
exports.createCheckpointTransaction = createCheckpointTransaction;
function createConcludePushOutcomeAndTransferAllTransaction(signedStates) {
    var _a = createSignatureArguments(signedStates), states = _a.states, signatures = _a.signatures, whoSignedWhat = _a.whoSignedWhat;
    return nitroAdjudicatorTrans.createConcludePushOutcomeAndTransferAllTransaction(states, signatures, whoSignedWhat);
}
exports.createConcludePushOutcomeAndTransferAllTransaction = createConcludePushOutcomeAndTransferAllTransaction;
function createConcludeTransaction(conclusionProof) {
    var _a = createSignatureArguments(conclusionProof), states = _a.states, signatures = _a.signatures, whoSignedWhat = _a.whoSignedWhat;
    return forceMoveTrans.createConcludeTransaction(states, signatures, whoSignedWhat);
}
exports.createConcludeTransaction = createConcludeTransaction;
exports.createPushOutcomeTransaction = nitroAdjudicatorTrans.createPushOutcomeTransactionFactory(false);
exports.createPushOutcomeAndTransferAllTransaction = nitroAdjudicatorTrans.createPushOutcomeTransactionFactory(true);
function createSignatureArguments(signedStates) {
    var participants = signedStates[0].state.channel.participants;
    var states = [];
    var whoSignedWhat = new Array(participants.length);
    var uniqueSignedStates = signedStates.filter(function (s, i, a) { return a.indexOf(s) === i; });
    var uniqueStates = uniqueSignedStates.map(function (s) { return s.state; }).filter(function (s, i, a) { return a.indexOf(s) === i; });
    var signatures = new Array(uniqueStates.length);
    var _loop_1 = function (i) {
        var e_1, _a;
        states.push(uniqueStates[i]);
        var signedStatesForUniqueState = uniqueSignedStates.filter(function (s) { return s.state === uniqueStates[i]; });
        try {
            for (var signedStatesForUniqueState_1 = (e_1 = void 0, __values(signedStatesForUniqueState)), signedStatesForUniqueState_1_1 = signedStatesForUniqueState_1.next(); !signedStatesForUniqueState_1_1.done; signedStatesForUniqueState_1_1 = signedStatesForUniqueState_1.next()) {
                var ss = signedStatesForUniqueState_1_1.value;
                var participantIndex = participants.indexOf(signatures_1.getStateSignerAddress(ss));
                signatures[participantIndex] = ss.signature;
                whoSignedWhat[participantIndex] = i;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (signedStatesForUniqueState_1_1 && !signedStatesForUniqueState_1_1.done && (_a = signedStatesForUniqueState_1.return)) _a.call(signedStatesForUniqueState_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    for (var i = 0; i < uniqueStates.length; i++) {
        _loop_1(i);
    }
    return {
        states: states,
        signatures: signatures,
        whoSignedWhat: whoSignedWhat,
    };
}
exports.createSignatureArguments = createSignatureArguments;
//# sourceMappingURL=transactions.js.map