'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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _dictCodes = require('../../../framework/constant/dictCodes');

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

var _routerChange2 = _interopRequireDefault(_routerChange);

var _triggerRequest = require('./triggerRequest');

var _triggerRequest2 = _interopRequireDefault(_triggerRequest);

var _EmptyUtils = require('../../../framework/utils/EmptyUtils');

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /**
                                                                                                                                                                                                                   * 收集数据
                                                                                                                                                                                                                   */


var confirm = _mxa.Modal.confirm;

var genCustomParams = function genCustomParams(props) {
  var record = props.record,
      buttonDescription = props.buttonDescription,
      inject = props.inject;

  var customParams = {};
  if (inject.buttons && inject.buttons.length > 0) {
    var injectAction = inject.buttons.filter(function (button) {
      return button.key === buttonDescription;
    });
    if (injectAction && injectAction.length > 0 && injectAction[0].action) {
      customParams = injectAction[0].action({ props: props, record: record });
    }
  }
  return customParams;
};

var createBindParameters = function createBindParameters(record, props) {
  var params = {};
  if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.SEVERAL) {
    props.bindParameters && props.bindParameters.forEach(function (item) {
      params[item.name] = (0, _EmptyUtils.filterEmptyValue)(item.value, record[item.name]);
    });
  } else if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.ALL) {
    params = Object.assign(params, record);
    // add bind Parameters
    if (props.bindParameters && props.bindParameters.filter) {
      params = props.bindParameters.filter(function (item) {
        return item.value !== null && item.value !== undefined;
      }).reduce(function (res, p) {
        res[p.name] = p.value;
        return res;
      }, params);
    }
  } else if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.CUSTOMIZE) {
    params = Object.assign(params, genCustomParams(props));
  } else if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.KEYS) {
    params = record[props.mainEntityKey];
  } else if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.KEY) {
    params = _defineProperty({}, props.mainEntityKey, record[props.mainEntityKey]);
  } else if (props.bindParameterType === _dictCodes.BUTTON_BINDPARAMETERTYPE.NONE) {
    params = {};
  }
  return params;
};

var createTransmitParameters = function createTransmitParameters(props) {
  var params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0 && props.query) {
    props.transmitParameters.forEach(function (item) {
      params[item.name] = props.query[item.name];
    });
  }
  if (props.isTransmitAllParameters && props.query) {
    return Object.assign(params, props.query);
  }
  return params;
};

var genParams = function genParams(record, props) {
  return Object.assign({}, createTransmitParameters(props), createBindParameters(record, props));
};

var triggerActionWithoutRows = function triggerActionWithoutRows(data, props, next) {
  var params = genParams({}, props);
  if (props.messagePromptType === _dictCodes.BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: _react2.default.createElement(
        'div',
        null,
        '\u786E\u8BA4',
        props.buttonDescription,
        '\u5417\uFF1F'
      ),
      onOk: function onOk() {
        return next(params);
      },
      onCancel: function onCancel() {}
    });
  } else {
    next(params);
  }
};

var triggerActionWithoutForm = function triggerActionWithoutForm(data, props, next) {
  var params = genParams({}, props);
  var cb = function cb() {
    if (props.submitFuc) {
      props.submitFuc(function (values) {
        next(_extends({}, params, values));
      });
    } else {
      next(params);
    }
  };
  if (props.messagePromptType === _dictCodes.BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: _react2.default.createElement(
        'div',
        null,
        '\u786E\u8BA4',
        props.buttonDescription,
        '\u5417\uFF1F'
      ),
      onOk: function onOk() {
        return cb();
      },
      onCancel: function onCancel() {}
    });
  } else {
    cb();
  }
};

var triggerActionSingle = function triggerActionSingle(activeData, props, next) {
  var params = genParams(activeData, props);
  if (props.messagePromptType === _dictCodes.BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: _react2.default.createElement(
        'div',
        null,
        '\u786E\u8BA4',
        props.buttonDescription,
        '\u5417\uFF1F'
      ),
      onOk: function onOk() {
        return next(params);
      },
      onCancel: function onCancel() {}
    });
  } else {
    next(params);
  }
};

var triggerActionMultiple = function triggerActionMultiple(activeData, props, next) {
  var paramPartA = activeData.map(function (record) {
    return createBindParameters(record, props);
  });
  var paramPartB = createTransmitParameters(props);
  var params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0) {
    params = _extends({}, paramPartB, { keyArray: paramPartA });
  } else {
    params = { keyArray: paramPartA };
  }
  if (props.messagePromptType === _dictCodes.BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: _react2.default.createElement(
        'div',
        null,
        '\u786E\u8BA4',
        props.buttonDescription,
        '\u6240\u9009\u6570\u636E\u5417\uFF1F'
      ),
      onOk: function onOk() {
        return next(params);
      },
      onCancel: function onCancel() {}
    });
  } else {
    next(params);
  }
};

var triggerActionAll = function triggerActionAll(activeData, props, next) {
  var paramPartB = createTransmitParameters(props);
  var params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0) {
    params = _extends({}, paramPartB, { dataArray: activeData });
  } else {
    params = { dataArray: activeData.map(function (item) {
        item.key && delete item.key;
        return item;
      }) };
  }
  if (props.messagePromptType === _dictCodes.BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: _react2.default.createElement(
        'div',
        null,
        '\u786E\u8BA4',
        props.buttonDescription,
        '\u7684\u6570\u636E\u5417\uFF1F'
      ),
      onOk: function onOk() {
        return next(params);
      },
      onCancel: function onCancel() {}
    });
  } else {
    next(params);
  }
};

var collectData = function collectData(p, props, next) {
  var record = props.record;

  if (props.relatedData === _dictCodes.BUTTON_RELATEDDATA.NONE) {
    triggerActionWithoutRows({}, props, next);
  } else if (props.relatedData === _dictCodes.BUTTON_RELATEDDATA.FORM) {
    triggerActionWithoutForm({}, props, next);
  } else if (props.inline) {
    triggerActionSingle(record, props, next);
  } else if (props.relatedData === 'ALL') {
    triggerActionAll(record, props, next);
    console.log('aaaaa');
  } else if (props.selectedType !== _dictCodes.LIST_SELECTTYPE.CHECKBOX || props.relatedData === _dictCodes.BUTTON_RELATEDDATA.SINGLE) {
    triggerActionSingle(record[0], props, next);
  } else {
    triggerActionMultiple(record, props, next);
  }
};

var createTriggerRequest = function createTriggerRequest(switchLoading) {
  return {
    next: function next(d, p, _next) {
      _triggerRequest2.default.next({ params: d, url: p.actionLink || p.domainLink }, p, _next, switchLoading);
    },
    ctrl: _triggerRequest2.default.ctrl
  };
};

var createRouterChange = function createRouterChange(switchLoading) {
  return {
    next: function next(d, p, _next2) {
      _routerChange2.default.next(d, p.actionLink || p.domainLink, p, _next2, switchLoading);
    }
  };
};

exports.default = {
  next: collectData,
  ctrl: function ctrl(data, props, processCtrl, switchLoading) {
    if (props.actionType === _dictCodes.BUTTON_ACTIONTYPE.REQUEST) {
      switchLoading && switchLoading(true);
      processCtrl.push(createTriggerRequest(switchLoading));
    } else if (props.actionType === _dictCodes.BUTTON_ACTIONTYPE.ROUTER) {
      processCtrl.push(createRouterChange(switchLoading));
    } else {
      // 默认 ROUTER
      processCtrl.push(createRouterChange(switchLoading));
    }
  }
};