import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _Promise from 'babel-runtime/core-js/promise';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _ from 'lodash';
import Web3 from 'web3';

var Web3Wrapper =
/**
 * Constructor Web3 wrapper.
 * @param {object} web3Provider - The web3 instance.
 */
function Web3Wrapper(web3Provider) {
  var _this = this;

  _classCallCheck(this, Web3Wrapper);

  this.isAddress = function (address) {
    return _this._web3.isAddress(address);
  };

  this.getAccount = function (index) {
    return _this._web3.eth.accounts[index];
  };

  this.getProvider = function () {
    return _this._web3.currentProvider;
  };

  this.getCoinbase = function () {
    return _this._web3.eth.coinbase;
  };

  this.toWei = function (amount, unit) {
    var newAmount = _this._web3.toWei(amount, unit);
    return newAmount.toNumber ? newAmount.toNumber() : Number(newAmount);
  };

  this.fromWei = function (amount, unit) {
    var newAmount = _this._web3.fromWei(amount, unit);
    return newAmount.toNumber ? newAmount.toNumber() : Number(newAmount);
  };

  this.toBigNumber = function (number) {
    return _this._web3.toBigNumber(number);
  };

  this.blockNumber = function () {
    return _this._web3.eth.blockNumber;
  };

  this.sign = function (userAddress, data) {
    return new _Promise(function (resolve, reject) {
      _this._web3.eth.sign(userAddress, data, function (error, result) {
        if (error) reject(error);

        resolve(result);
      });
    });
  };

  this.getBlock = function (blockNumber) {
    return new _Promise(function (resolve, reject) {
      _this._web3.eth.getBlock(blockNumber, function (error, result) {
        if (error) reject(error);

        resolve(result);
      });
    });
  };

  this.doesContractExistAtAddressAsync = function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(address) {
      var code, codeIsEmpty;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this._web3.eth.getCode(address);

            case 2:
              code = _context.sent;

              // Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients
              codeIsEmpty = /^0x0{0,40}$/i.test(code);
              return _context.abrupt('return', !codeIsEmpty);

            case 5:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }));

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

  this._getNetworkIdIfExistsAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var networkId;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (_.isUndefined(_this.networkIdIfExists)) {
              _context2.next = 2;
              break;
            }

            return _context2.abrupt('return', _this.networkIdIfExists);

          case 2:
            _context2.prev = 2;
            _context2.next = 5;
            return _this._getNetworkAsync();

          case 5:
            networkId = _context2.sent;


            _this.networkIdIfExists = Number(networkId);
            return _context2.abrupt('return', _this.networkIdIfExists);

          case 10:
            _context2.prev = 10;
            _context2.t0 = _context2['catch'](2);

            console.log(_context2.t0);
            return _context2.abrupt('return', undefined);

          case 14:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, _this, [[2, 10]]);
  }));
  this._getNetworkAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var networkId;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.next = 2;
            return _this._web3.version.network;

          case 2:
            networkId = _context3.sent;
            return _context3.abrupt('return', networkId);

          case 4:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, _this);
  }));

  this.getBalanceInWeiAsync = function (owner) {
    if (_.isUndefined(owner)) {
      owner = _this._web3.eth.accounts[0];
    }

    var balanceInWei = _this._web3.eth.getBalance(owner);

    return balanceInWei.toString();
  };

  this._web3 = new Web3(web3Provider);
};

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