'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.longRunExec = exports.ContentRender = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _loadingBar = require('../modules/loading-bar');

var _DispatchService = require('../service/DispatchService');

var _mxa = require('../../components/mxa');

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

/**
 * Created by baoyinghai on 10/18/16.
 */
var ContentRender = exports.ContentRender = function ContentRender(props) {
  return _react2.default.createElement(
    'div',
    null,
    _react2.default.createElement(
      'div',
      { style: { wordWrap: 'break-word' } },
      props.message
    ),
    _react2.default.createElement(
      'div',
      { style: { wordWrap: 'break-word' } },
      props.uuid
    )
  );
};

/* eslint-disable */

// import { forceLogout } from '../actions/global';
var longRunExec = exports.longRunExec = function longRunExec(bizHandler) {
  var needLoding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;


  var self = undefined;
  if (needLoding) {
    (0, _DispatchService.dispatch)((0, _loadingBar.showLoading)());
  }
  return bizHandler().then(function (response) {}).catch(function (errorData) {
    var content = errorData.message ? errorData.message : errorData.msgContent + '(' + errorData.ex + ')';
    // TODO upload error;
    if ('Network request failed' === errorData.message) {
      content = '网络请求异常!';
    }
    console.log('** error ** ', errorData);
    console.log('uudi', errorData && errorData.uuid);
    _mxa.Modal.error({
      title: '异常',
      content: _react2.default.createElement(ContentRender, { message: content, uuid: errorData.uuid }),
      onOk: function onOk() {
        if (errorData.msgCode === 'TOKEN_INVALIDATE' || errorData.ex === 2114 || errorData.ex === 2115) {
          location.reload();
        }
      }
    });
    // if (errorData.msgCode === 'SYSTEM_AUTH_HEADER_INVALID' || errorData.msgCode === 'SYSTEM_AUTH_HEADER_NOTMATCHED') {
    //   dispatch(forceLogout());
    // }
  }).catch(function (e) {
    // TODO upload error; merge
    console.log(e);
    _mxa.Modal.error({
      title: '系统异常',
      content: JSON.stringify(e)
    });
  }).then(function () {
    console.log('need loading: ', needLoding);
    if (needLoding) {
      (0, _DispatchService.dispatch)((0, _loadingBar.hideLoading)());
    }
  });
};