'use strict';

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

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

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

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

var _promise2 = _interopRequireDefault(_promise);

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

var _lodash2 = _interopRequireDefault(_lodash);

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

var _ContractImplementation2 = _interopRequireDefault(_ContractImplementation);

var _EventListener = require('../../../utils/EventListener');

var _EventListener2 = _interopRequireDefault(_EventListener);

var _httpRequest = require('../../../utils/httpRequest');

var _httpRequest2 = _interopRequireDefault(_httpRequest);

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

/**
 * Provides interaction with standard Arbitrable contracts
 */
var Arbitrable = function (_ContractImplementati) {
  (0, _inherits3.default)(Arbitrable, _ContractImplementati);

  /**
   * Constructor ArbitrableTransaction.
   * @param {object} web3Provider instance
   * @param {string} contractAddress of the contract
   */
  function Arbitrable(web3Provider, contractArtifact, contractAddress) {
    var _this2 = this;

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

    var _this = (0, _possibleConstructorReturn3.default)(this, (Arbitrable.__proto__ || (0, _getPrototypeOf2.default)(Arbitrable)).call(this, web3Provider, contractArtifact, contractAddress));

    _this.getMetaEvidence = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
      var metaEvidenceLog, metaEvidenceUri, metaEvidenceResponse;
      return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _EventListener2.default.getEventLogs(_this, 'MetaEvidence', 0, 'latest', { _metaEvidenceID: 0 });

            case 2:
              metaEvidenceLog = _context.sent;

              if (metaEvidenceLog[0]) {
                _context.next = 5;
                break;
              }

              return _context.abrupt('return', {});

            case 5:
              // NOTE better to throw errors for missing meta-evidence?

              metaEvidenceUri = metaEvidenceLog[0].args._evidence;
              // FIXME caching issue need a query param to fetch from AWS

              _context.next = 8;
              return (0, _httpRequest2.default)('GET', metaEvidenceUri + '?nocache');

            case 8:
              metaEvidenceResponse = _context.sent;

              if (!(metaEvidenceResponse.status >= 400)) {
                _context.next = 11;
                break;
              }

              throw new Error('Unable to fetch meta-evidence at ' + metaEvidenceUri);

            case 11:
              return _context.abrupt('return', metaEvidenceResponse.body || metaEvidenceResponse);

            case 12:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this2);
    }));
    _this.getEvidence = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
      var arbitratorAddress, disputeId, evidenceLogs;
      return _regenerator2.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this.loadContract();

            case 2:
              _context3.next = 4;
              return _this.contractInstance.arbitrator();

            case 4:
              arbitratorAddress = _context3.sent;
              _context3.next = 7;
              return _this.loadContract();

            case 7:
              _context3.next = 9;
              return _this.contractInstance.disputeID();

            case 9:
              disputeId = _context3.sent.toNumber();

              if (!_lodash2.default.isNull(disputeId)) {
                _context3.next = 12;
                break;
              }

              return _context3.abrupt('return', []);

            case 12:
              _context3.next = 14;
              return _EventListener2.default.getEventLogs(_this, 'Evidence', 0, 'latest', { _disputeID: disputeId, _arbitrator: arbitratorAddress });

            case 14:
              evidenceLogs = _context3.sent;
              return _context3.abrupt('return', _promise2.default.all(evidenceLogs.map(function () {
                var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(evidenceLog) {
                  var evidenceURI, evidence, submittedAt;
                  return _regenerator2.default.wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          evidenceURI = evidenceLog.args._evidence;
                          _context2.next = 3;
                          return (0, _httpRequest2.default)('GET', evidenceURI);

                        case 3:
                          evidence = _context2.sent;
                          _context2.next = 6;
                          return _this._Web3Wrapper.getBlock(evidenceLog.blockNumber);

                        case 6:
                          submittedAt = _context2.sent.timestamp;
                          return _context2.abrupt('return', (0, _extends3.default)({}, evidence.body, { submittedBy: evidenceLog.args._party, submittedAt: submittedAt }));

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

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

            case 16:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this2);
    }));
    _this.getContractData = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
      var _ref5, _ref6, metaEvidence, partyA, partyB;

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

            case 2:
              _context4.next = 4;
              return _promise2.default.all([_this.getMetaEvidence(), _this.contractInstance.partyA(), _this.contractInstance.partyB()]);

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

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

  /**
   * Get the meta evidence for the contract. Arbitrable Transaction can only have
   * one meta-evidence that is submitted on contract creation. Look up meta-evidence event
   * and make an http request to the resource.
   */


  /**
   * Get the evidence submitted in a dispute.
   */


  /**
   * Fetch all standard contract data.
   */


  return Arbitrable;
}(_ContractImplementation2.default);

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