import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';

var _this = this;

import contract from 'truffle-contract';

import * as ethConstants from '../constants/eth';
import { UNABLE_TO_DEPLOY_CONTRACT } from '../constants/error';

import isRequired from './isRequired';

/**
 * Deploy a contract on the Ethereum network using the contract artifact.
 * @param {string} account - The account to deploy it under.
 * @param {number} value - The value to send.
 * @param {object} artifact - JSON artifact of the contract.
 * @param {object} web3Provider - Web3 Provider object (NOTE NOT Kleros Web3Wrapper)
 * @param {...any} args - Extra arguments.
 * @returns {object} - truffle-contract Object | err The contract object or an error
 */
var deployContractAsync = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    for (var _len = arguments.length, args = Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
      args[_key - 4] = arguments[_key];
    }

    var account = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('account');
    var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : isRequired('value');
    var artifact = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : isRequired('artifact');
    var web3Provider = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : isRequired('web3Provider');
    var MyContract;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            MyContract = contract({
              abi: artifact.abi,
              unlinked_binary: artifact.bytecode ? artifact.bytecode : artifact.unlinked_binary
            });

            MyContract.setProvider(web3Provider);

            return _context.abrupt('return', MyContract.new.apply(MyContract, args.concat([{
              from: account,
              value: value,
              gas: ethConstants.TRANSACTION.GAS
            }])));

          case 6:
            _context.prev = 6;
            _context.t0 = _context['catch'](0);

            console.error(_context.t0);
            throw new Error(UNABLE_TO_DEPLOY_CONTRACT);

          case 10:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this, [[0, 6]]);
  }));

  return function deployContractAsync() {
    return _ref.apply(this, arguments);
  };
}();

export default deployContractAsync;
//# sourceMappingURL=deployContractAsync.js.map