'use strict';

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

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

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 _PinakionPOC = require('kleros/build/contracts/PinakionPOC');

var _PinakionPOC2 = _interopRequireDefault(_PinakionPOC);

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);

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 PinakionPOC = function (_ContractImplementati) {
  (0, _inherits3.default)(PinakionPOC, _ContractImplementati);

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

    var _this = (0, _possibleConstructorReturn3.default)(this, (PinakionPOC.__proto__ || (0, _getPrototypeOf2.default)(PinakionPOC)).call(this, web3Provider, _PinakionPOC2.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
   * @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;
}(_ContractImplementation2.default); // FIXME: mock


PinakionPOC.deploy = function () {
  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(account, web3Provider) {
    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, ethConstants.TRANSACTION.VALUE, _PinakionPOC2.default, web3Provider);

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

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

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

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

  this.setKleros = 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.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, _this2, [[2, 6]]);
    }));

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

  this.transferOwnership = function () {
    var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(klerosAddress) {
      var account = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _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:
              _context3.prev = 2;
              return _context3.abrupt('return', _this2.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, _this2, [[2, 6]]);
    }));

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

  this.getData = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
    var _ref5, _ref6, owner, kleros;

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

          case 2:
            _context4.next = 4;
            return _promise2.default.all([_this2.contractInstance.owner(), _this2.contractInstance.kleros()]);

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

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

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