import _Promise from 'babel-runtime/core-js/promise';
import _slicedToArray from 'babel-runtime/helpers/slicedToArray';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';

var _this2 = this;

import PinakionPOCArtifact from 'kleros/build/contracts/PinakionPOC'; // FIXME: mock
import _ from 'lodash';

import * as ethConstants from '../../../constants/eth';
import * as errorConstants from '../../../constants/error';
import ContractImplementation from '../../ContractImplementation';
import deployContractAsync from '../../../utils/deployContractAsync';

/**
 * Provides interaction with a PinakionPOC contract deployed on the blockchain.
 */

var PinakionPOC = function (_ContractImplementati) {
  _inherits(PinakionPOC, _ContractImplementati);

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

    var _this = _possibleConstructorReturn(this, (PinakionPOC.__proto__ || _Object$getPrototypeOf(PinakionPOC)).call(this, web3Provider, PinakionPOCArtifact, contractAddress));

    _initialiseProps.call(_this);

    return _this;
  }

  /**
   * Deploy a new instance of PinakionPOC.
   * @param {string} account - account of user
   * @param {object} web3Provider - web3 provider object
   * @returns {object} - 'truffle-contract' Object | err The contract object or error deploy.
   */


  /**
   * Change the kleros contract variable in instance of PinakionPOC.
   * @param {string} klerosAddress - Address of Kleros POC contract.
   * @param {string} account - Address of user.
   * @returns {object} - The result transaction object.
   */


  /**
   * 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.
   */


  /**
   * Get data from PNK contract.
   * @returns {object} - Data from PNK contract.
   */


  return PinakionPOC;
}(ContractImplementation);

PinakionPOC.deploy = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(account, web3Provider) {
    var contractDeployed;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return deployContractAsync(account, ethConstants.TRANSACTION.VALUE, PinakionPOCArtifact, web3Provider);

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

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

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

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

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

            case 2:
              _context2.prev = 2;
              return _context2.abrupt('return', _this3.contractInstance.setKleros(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_SET_KLEROS);

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

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

  this.transferOwnership = function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(klerosAddress) {
      var account = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this3._Web3Wrapper.getAccount(0);
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this3.loadContract();

            case 2:
              _context3.prev = 2;
              return _context3.abrupt('return', _this3.contractInstance.transferOwnership(klerosAddress, {
                from: account,
                gas: ethConstants.TRANSACTION.GAS
              }));

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

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

            case 10:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this3, [[2, 6]]);
    }));

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

  this.getData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
    var _ref5, _ref6, owner, kleros;

    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.next = 2;
            return _this3.loadContract();

          case 2:
            _context4.next = 4;
            return _Promise.all([_this3.contractInstance.owner(), _this3.contractInstance.kleros()]);

          case 4:
            _ref5 = _context4.sent;
            _ref6 = _slicedToArray(_ref5, 2);
            owner = _ref6[0];
            kleros = _ref6[1];
            return _context4.abrupt('return', {
              owner: owner,
              kleros: kleros
            });

          case 9:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, _this3);
  }));
};

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