"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 transactionFactory_1 = require("../../../utils/solidity/transactionFactory");
const managersToHubs_1 = require("../calls/managersToHubs");
const Contracts_1 = require("../../../Contracts");
const stringToBytes32_1 = require("../../../utils/helpers/stringToBytes32");
const guard = (environment, contractAddress, params) => __awaiter(this, void 0, void 0, function* () { });
const prepareArgs = (_, { fundName, fees, exchangeConfigs, quoteToken, nativeToken, defaultTokens, priceSource, }) => __awaiter(this, void 0, void 0, function* () {
    const exchangeAddresses = Object.values(exchangeConfigs).map(e => e.exchange.toString());
    const adapterAddresses = Object.values(exchangeConfigs).map(e => e.adapter.toString());
    const takesCustody = Object.values(exchangeConfigs).map(e => e.takesCustody);
    const defaultTokenAddresses = defaultTokens.map(t => t.address);
    const quoteTokenAddress = quoteToken.address;
    const nativeTokenAddress = nativeToken.address;
    const feeAddresses = fees.map(f => f.feeAddress);
    // TODO: Hacky fix. Could be some problem with BN.js
    const feeRates = fees.map(f => `${f.feeRate}`);
    const feePeriods = fees.map(f => `${f.feePeriod}`);
    const args = [
        stringToBytes32_1.stringToBytes32(fundName),
        feeAddresses,
        feeRates,
        feePeriods,
        exchangeAddresses,
        adapterAddresses,
        quoteTokenAddress,
        nativeTokenAddress,
        defaultTokenAddresses,
        takesCustody,
        priceSource.toString(),
    ];
    return args;
});
const postProcess = (environment, receipt, params, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    return managersToHubs_1.managersToHubs(environment, contractAddress, environment.wallet.address);
});
exports.beginSetup = transactionFactory_1.transactionFactory('beginSetup', Contracts_1.Contracts.FundFactory, guard, prepareArgs, postProcess);
