"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Contracts_1 = require("../../../../Contracts");
const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory");
const getExchangeIndex_1 = require("../calls/getExchangeIndex");
const FunctionSignatures_1 = require("../utils/FunctionSignatures");
const ensure_1 = require("../../../../utils/guards/ensure");
const emptyAddress_1 = require("../../../../utils/constants/emptyAddress");
const guard = (environment, { tokens }, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    ensure_1.ensure(tokens.length > 0, 'Tokens array cannot be empty');
    ensure_1.ensure(tokens.length <= 6, 'You can only withdraw 6 tokens at once');
});
const prepareArgs = (environment, { tokens }, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    const exchangeIndex = yield getExchangeIndex_1.getExchangeIndex(environment, contractAddress, {
        exchange: Contracts_1.Exchanges.Ethfinex,
    });
    const paddedTokens = tokens.slice(); // Deep copy
    while (paddedTokens.length < 6) {
        paddedTokens.push(emptyAddress_1.emptyAddress);
    }
    const args = [
        exchangeIndex,
        FunctionSignatures_1.FunctionSignatures.withdrawTokens,
        paddedTokens,
        [0, 0, 0, 0, 0, 0, 0, 0],
        '0x0',
        '0x0',
        '0x0',
        '0x0',
    ];
    return args;
});
const withdrawTokensEthfinex = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs, undefined);
exports.withdrawTokensEthfinex = withdrawTokensEthfinex;
