"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConcludePushOutcomeAndTransferAllTransaction = exports.concludePushOutcomeAndTransferAllArgs = exports.createPushOutcomeTransactionFactory = void 0;
var ethers_1 = require("ethers");
var NitroAdjudicator_json_1 = __importDefault(require("../../../artifacts/contracts/NitroAdjudicator.sol/NitroAdjudicator.json"));
var channel_1 = require("../channel");
var outcome_1 = require("../outcome");
var state_1 = require("../state");
var NitroAdjudicatorContractInterface = new ethers_1.utils.Interface(NitroAdjudicator_json_1.default.abi);
var createPushOutcomeTransactionFactory = function (transferAll) { return function (arg) {
    var defaults = { channelWasConcluded: false, challengerAddress: ethers_1.constants.AddressZero };
    var _a = __assign(__assign({}, defaults), arg), turnNumRecord = _a.turnNumRecord, finalizesAt = _a.finalizesAt, state = _a.state, outcome = _a.outcome, channelWasConcluded = _a.channelWasConcluded, challengerAddress = _a.challengerAddress;
    if (channelWasConcluded && turnNumRecord !== 0)
        throw Error('If the channel was concluded, you should use 0 for turnNumRecord');
    var channelId = channel_1.getChannelId(state.channel);
    var stateHash = channelWasConcluded ? ethers_1.constants.HashZero : state_1.hashState(state);
    var encodedOutcome = outcome_1.encodeOutcome(outcome);
    var funcName = transferAll ? 'pushOutcomeAndTransferAll' : 'pushOutcome';
    var data = NitroAdjudicatorContractInterface.encodeFunctionData(funcName, [
        channelId,
        turnNumRecord,
        finalizesAt,
        stateHash,
        challengerAddress,
        encodedOutcome,
    ]);
    return { data: data };
}; };
exports.createPushOutcomeTransactionFactory = createPushOutcomeTransactionFactory;
function concludePushOutcomeAndTransferAllArgs(states, signatures, whoSignedWhat) {
    if (states.length === 0) {
        throw new Error('No states provided');
    }
    var participants = states[0].channel.participants;
    if (participants.length !== signatures.length) {
        throw new Error("Participants (length:" + participants.length + ") and signatures (length:" + signatures.length + ") need to be the same length");
    }
    var lastState = states.reduce(function (s1, s2) { return (s1.turnNum >= s2.turnNum ? s1 : s2); }, states[0]);
    var largestTurnNum = lastState.turnNum;
    var fixedPart = state_1.getFixedPart(lastState);
    var appPartHash = state_1.hashAppPart(lastState);
    var outcomeBytes = outcome_1.encodeOutcome(lastState.outcome);
    var numStates = states.length;
    return [
        largestTurnNum,
        fixedPart,
        appPartHash,
        outcomeBytes,
        numStates,
        whoSignedWhat,
        signatures,
    ];
}
exports.concludePushOutcomeAndTransferAllArgs = concludePushOutcomeAndTransferAllArgs;
function createConcludePushOutcomeAndTransferAllTransaction(states, signatures, whoSignedWhat) {
    return {
        data: NitroAdjudicatorContractInterface.encodeFunctionData('concludePushOutcomeAndTransferAll', concludePushOutcomeAndTransferAllArgs(states, signatures, whoSignedWhat)),
    };
}
exports.createConcludePushOutcomeAndTransferAllTransaction = createConcludePushOutcomeAndTransferAllTransaction;
//# sourceMappingURL=nitro-adjudicator.js.map