'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _MiniMeTokenERC = require('kleros-interaction/build/contracts/MiniMeTokenERC20');

var _MiniMeTokenERC2 = _interopRequireDefault(_MiniMeTokenERC);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _eth = require('../../../constants/eth');

var ethConstants = _interopRequireWildcard(_eth);

var _error = require('../../../constants/error');

var errorConstants = _interopRequireWildcard(_error);

var _ContractImplementation = require('../../ContractImplementation');

var _ContractImplementation2 = _interopRequireDefault(_ContractImplementation);

var _deployContractAsync = require('../../../utils/deployContractAsync');

var _deployContractAsync2 = _interopRequireDefault(_deployContractAsync);

var _isRequired = require('../../../utils/isRequired');

var _isRequired2 = _interopRequireDefault(_isRequired);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Provides interaction with a PinakionPOC contract deployed on the blockchain.
 */
var MiniMePinakion = function (_ContractImplementati) {
  (0, _inherits3.default)(MiniMePinakion, _ContractImplementati);

  /**
   * Constructor PinakionPOC.
   * @param {object} web3Provider - web3 instance.
   * @param {string} contractAddress - of the contract (optionnal).
   */
  function MiniMePinakion(web3Provider, contractAddress) {
    (0, _classCallCheck3.default)(this, MiniMePinakion);

    var _this = (0, _possibleConstructorReturn3.default)(this, (MiniMePinakion.__proto__ || (0, _getPrototypeOf2.default)(MiniMePinakion)).call(this, web3Provider, // value
    _MiniMeTokenERC2.default, contractAddress));

    _initialiseProps.call(_this);

    return _this;
  }

  /**
   * Deploy a new instance of PinakionPOC.
   * @param {string} account - account of user
   * @param {object} web3Provider - web3 provider object
   * @param {string} tokenFactoryAddress - The address of the MiniMeTokenFactory contract that will create the Clone token contracts
   * @param {string} parentTokenAddress - Address of the parent token, set to 0x0 if it is a new token
   * @param {number} parentSnapshotBlock - Block of the parent token that will determine the initial distribution of the clone token, set to 0 if it is a new token
   * @param {string} tokenName - Name of the token.
   * @param {number} decimalUnits - Number of decimal units token is divisible by.
   * @param {string} tokenSymbol - Abreviated symbol to represent the token.
   * @param {bool} transfersEnabled - If users can transfer tokens.
   * @returns {object} - 'truffle-contract' Object | err The contract object or error deploy.
   */


  /**
   * Transfer ownership of the PNK contract to the kleros POC contract.
   * @param {string} klerosAddress - Address of Kleros POC contract.
   * @param {string} account - Address of user.
   * @returns {object} - The result transaction object.
   */


  /**
   * Approve the arbitrator contract to transfer PNK to the contract and call the arbitrators
   * receiveApproval()
   * @param {string} arbitratorAddress - The address of the arbitrator contract.
   * @param {number} amount - The amount of PNK to transfer.
   * @param {string} account - The users account.
   * @returns {bool} If the transfer succeeded or not
   */


  return MiniMePinakion;
}(_ContractImplementation2.default);

MiniMePinakion.deploy = function () {
  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
    var account = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _isRequired2.default)('account');
    var web3Provider = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _isRequired2.default)('web3Provider');
    var tokenFactoryAddress = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : (0, _isRequired2.default)('tokenFactoryAddress');
    var parentTokenAddress = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '0x0';
    var parentSnapshotBlock = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
    var tokenName = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 'Pinakion';
    var decimalUnits = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 18;
    var tokenSymbol = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 'PNK';
    var transfersEnabled = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : true;
    var contractDeployed;
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, _deployContractAsync2.default)(account, 0, _MiniMeTokenERC2.default, web3Provider, tokenFactoryAddress, // args
            parentTokenAddress, parentSnapshotBlock, tokenName, decimalUnits, tokenSymbol, transfersEnabled);

          case 2:
            contractDeployed = _context.sent;
            return _context.abrupt('return', contractDeployed);

          case 4:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

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

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.changeController = function () {
    var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(klerosAddress) {
      var account = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this2._Web3Wrapper.getAccount(0);
      return _regenerator2.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return _this2.loadContract();

            case 2:
              _context2.prev = 2;
              return _context2.abrupt('return', _this2.contractInstance.changeController(klerosAddress, {
                from: account,
                gas: ethConstants.TRANSACTION.GAS
              }));

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

              console.error(_context2.t0);
              throw new Error(errorConstants.UNABLE_TO_TRANSFER_OWNERSHIP);

            case 10:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, _this2, [[2, 6]]);
    }));

    return function (_x10) {
      return _ref2.apply(this, arguments);
    };
  }();

  this.approveAndCall = function () {
    var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(arbitratorAddress, amount) {
      var account = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _this2._Web3Wrapper.getAccount(0);
      return _regenerator2.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this2.loadContract();

            case 2:
              return _context3.abrupt('return', _this2.contractInstance.approveAndCall(arbitratorAddress, _this2._Web3Wrapper.toWei(amount, 'ether'), '0x0', {
                from: account,
                gas: ethConstants.TRANSACTION.GAS
              }));

            case 3:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this2);
    }));

    return function (_x12, _x13) {
      return _ref3.apply(this, arguments);
    };
  }();
};

exports.default = MiniMePinakion;
//# sourceMappingURL=MiniMePinakion.js.map