'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /**
                                                                                                                                                                                                                                                                               * Created by baoyinghai on 12/23/16.
                                                                                                                                                                                                                                                                               */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _fetch = require('../../../framework/system/fetch');

var _longRunOpt = require('../../../framework/system/longRunOpt');

var _routerChange = require('./routerChange');

var _routerChange2 = _interopRequireDefault(_routerChange);

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

var _StringUtils = require('../../../framework/utils/StringUtils');

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

var createObject = function createObject() {
  var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') {
    return obj;
  }
  return { $MXKEY: obj };
};

var processAction = function processAction(data, props, next, stopLoading, mappings) {
  if (props.beforeRequest) {
    data.params = props.beforeRequest(data.params);
  }
  (0, _longRunOpt.longRunExec)(function () {
    return (0, _fetch.PFetch)((0, _StringUtils.trimStart)((0, _StringUtils.convertTemplate)(data.url, data.params, mappings), '/'), data.params).then(function (response) {
      if (response.needConfirm) {
        _mxa.Modal.confirm({
          title: '提示',
          content: _react2.default.createElement(
            'div',
            null,
            response.msgContent
          ),
          onOk: function onOk() {
            (0, _longRunOpt.longRunExec)(function () {
              return (0, _fetch.PFetch)((0, _StringUtils.trimStart)((0, _StringUtils.convertTemplate)(data.url, data.params, mappings), '/'), _extends({}, data.params, { needConfirm: true })).then(function (res) {
                var r = res;
                if (props.afterRequest) {
                  r = props.afterRequest(res);
                }
                _mxa.Modal.info({
                  title: '提示',
                  content: _react2.default.createElement(
                    'div',
                    null,
                    props.requestSuccessDesc || props.buttonDescription + '成功！'
                  ),
                  onOk: function onOk() {
                    return next && next(_extends({}, data.params, createObject(r)));
                  }
                });
              });
            });
          },
          onCancel: function onCancel() {
            return stopLoading && stopLoading(false);
          }
        });
      } else {
        (function () {
          var newResponse = response;
          // 主要有nextLink的情况
          if (props.afterRequest) {
            newResponse = props.afterRequest(response);
          }
          if (props.hiddenResultPrompt) {
            next && next(_extends({}, data.params, createObject(newResponse)));
          } else {
            _mxa.Modal.info({
              title: '提示',
              content: _react2.default.createElement(
                'div',
                null,
                props.requestSuccessDesc || props.buttonDescription + '成功！'
              ),
              onOk: function onOk() {
                return next && next(_extends({}, data.params, createObject(newResponse)));
              }
            });
          }
        })();
      }
    }).catch(function (errorData) {
      // console.log(errorData);
      _mxa.Modal.error({
        title: '提示',
        content: errorData && errorData.msgContent ? _react2.default.createElement(
          'div',
          null,
          errorData.msgContent
        ) : _react2.default.createElement(
          'div',
          null,
          props.buttonDescription,
          '\u5931\u8D25\uFF01'
        ),
        onOk: function onOk() {
          return stopLoading && stopLoading(false);
        }
      });
    });
  });
};

var routerChangeDecorator = function routerChangeDecorator(url, mappings, switchLoading) {
  return {
    next: function next(data, props, _next) {
      _routerChange2.default.next(data, url, props, _next, switchLoading, mappings);
    }
  };
};

exports.default = {
  next: processAction,
  ctrl: function ctrl(data, props, processCtrl, switchLoading) {
    if (props.nextActionLink) {
      processCtrl.push(routerChangeDecorator(props.nextActionLink, props.nextActionLinkMappings, switchLoading));
    } else {
      processCtrl.push(routerChangeDecorator(props.actionLink || props.domainLink, props.actionLinkMappings, switchLoading));
    }
  }
};