'use strict';

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

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var _promise2 = _interopRequireDefault(_promise);

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

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

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 _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _arbitrator = require('../../constants/arbitrator');

var arbitratorConstants = _interopRequireWildcard(_arbitrator);

var _AbstractContract2 = require('../AbstractContract');

var _AbstractContract3 = _interopRequireDefault(_AbstractContract2);

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

/**
 * Arbitrator Abstract Contarct API. This wraps an arbitrator contract. It provides
 * interaction with both the off chain store as well as the arbitrator instance. All
 * arbitrator methods from the supplied contract implementation can be called from this
 * object.
 */
var Arbitrator = function (_AbstractContract) {
  (0, _inherits3.default)(Arbitrator, _AbstractContract);

  function Arbitrator() {
    var _ref,
        _this2 = this;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Arbitrator);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Arbitrator.__proto__ || (0, _getPrototypeOf2.default)(Arbitrator)).call.apply(_ref, [this].concat(args))), _this), _this.getDisputesForUser = function () {
      var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(account) {
        var _ref3, _ref4, period, currentSession, _getDisputesForUserFromStore, profile, myDisputes;

        return _regenerator2.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return _promise2.default.all([_this._contractImplementation.getPeriod(), _this._contractImplementation.getSession()]);

              case 2:
                _ref3 = _context3.sent;
                _ref4 = (0, _slicedToArray3.default)(_ref3, 2);
                period = _ref4[0];
                currentSession = _ref4[1];

                _getDisputesForUserFromStore = function () {
                  var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(account) {
                    return _regenerator2.default.wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                            _context.t0 = _promise2.default;
                            _context.next = 3;
                            return _this._StoreProvider.getDisputes(account);

                          case 3:
                            _context.t1 = function (dispute) {
                              return _this._contractImplementation.getDispute(dispute.disputeId);
                            };

                            _context.t2 = _context.sent.map(_context.t1);
                            return _context.abrupt('return', _context.t0.all.call(_context.t0, _context.t2));

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

                  return function _getDisputesForUserFromStore(_x2) {
                    return _ref5.apply(this, arguments);
                  };
                }();

                // new jurors have not been chosen yet. don't update


                if (!(period !== arbitratorConstants.PERIOD.VOTE)) {
                  _context3.next = 9;
                  break;
                }

                return _context3.abrupt('return', _getDisputesForUserFromStore(account));

              case 9:
                _context3.next = 11;
                return _this._StoreProvider.setUpUserProfile(account);

              case 11:
                profile = _context3.sent;

                if (!(currentSession !== profile.session)) {
                  _context3.next = 19;
                  break;
                }

                _context3.next = 15;
                return _this._contractImplementation.getDisputesForJuror(account);

              case 15:
                myDisputes = _context3.sent;
                _context3.next = 18;
                return _promise2.default.all(myDisputes.map(function () {
                  var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(dispute) {
                    return _regenerator2.default.wrap(function _callee2$(_context2) {
                      while (1) {
                        switch (_context2.prev = _context2.next) {
                          case 0:
                            _context2.next = 2;
                            return _this._StoreProvider.updateDisputeProfile(account, dispute.arbitratorAddress, dispute.disputeId, {
                              appealDraws: dispute.appealDraws
                            });

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

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

              case 18:

                _this._StoreProvider.updateUserProfile(account, {
                  session: currentSession
                });

              case 19:
                return _context3.abrupt('return', _getDisputesForUserFromStore(account));

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

      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }(), _this.buyPNK = function () {
      var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(amount, account) {
        return _regenerator2.default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return _this._contractImplementation.buyPNK(amount, account);

              case 2:
                return _context4.abrupt('return', _this._contractImplementation.getPNKBalance(account));

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

      return function (_x4, _x5) {
        return _ref7.apply(this, arguments);
      };
    }(), _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }
  /**
   * Get disputes for user with extra data from arbitrated transaction and store
   * @param {string} account address of user
   * @returns {object[]} dispute data objects for user
   */


  /**
   * Buy PNK.
   * @param {number} amount - Number of pinakion to buy.
   * @param {string} account - Address of user.
   * @returns {object[]} - Balance of user.
   */


  return Arbitrator;
}(_AbstractContract3.default);

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