var _excluded = ["dataSource", "tableProps", "value"],
  _excluded2 = ["refreshAfterRequest", "refreshAfterRequestErrorCode", "to", "confirmBeforeClick", "request", "formProps", "onSuccess", "uiType", "batchProps", "buttonProps", "disabledKey", "disabledValue", "disabledExpress"],
  _excluded3 = ["renderClassName", "renderContent", "className", "title", "content"],
  _excluded4 = ["url", "params", "idName"],
  _excluded5 = ["uploadProps", "downloadProps"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { mapProps } from 'recompose';
import get from 'lodash/get';
import { ButtonList } from "../../button";
// 装配单元格
var withTableButtonFeatures = mapProps(function (_ref) {
  var dataSource = _ref.dataSource,
    tableProps = _ref.tableProps,
    value = _ref.value,
    others = _objectWithoutProperties(_ref, _excluded);
  // eslint-disable-next-line @typescript-eslint/unbound-method

  var primaryKey = tableProps.primaryKey,
    onTableEmit = tableProps.onTableEmit,
    index = tableProps.index,
    record = tableProps.record,
    otherKey = tableProps.otherKey;
  var primaryValue = record[primaryKey];
  var otherValue = record[otherKey];

  // 替换文案中的占位符
  var replacePlaceholder = function replacePlaceholder() {
    var originText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    if (typeof originText === 'function') {
      return originText(tableProps);
    }
    return originText.replace(/\$\{([\w.]+)\}/g, function (matched, $1) {
      switch ($1) {
        case otherKey:
          return otherValue;
        // ${id} 替换为主键值
        case 'id':
          return primaryValue;
        // ${value} 替换为单元格的值
        case 'value':
          return value;
        // ${value} 替换为单元格的值
        case 'index':
          return index;
        // case otherKey:
        //   return otherValue;
        default:
          return get(tableProps, $1, matched);
      }
    });
  };
  var nextDataSource = dataSource.map(function (_ref2) {
    var refreshAfterRequest = _ref2.refreshAfterRequest,
      refreshAfterRequestErrorCode = _ref2.refreshAfterRequestErrorCode,
      to = _ref2.to,
      confirmBeforeClick = _ref2.confirmBeforeClick,
      request = _ref2.request,
      formProps = _ref2.formProps,
      onSuccess = _ref2.onSuccess,
      uiType = _ref2.uiType,
      batchProps = _ref2.batchProps,
      buttonProps = _ref2.buttonProps,
      disabledKey = _ref2.disabledKey,
      disabledValue = _ref2.disabledValue,
      disabledExpress = _ref2.disabledExpress,
      btnProps = _objectWithoutProperties(_ref2, _excluded2);
    // 抛出事件，触发 manage 页面刷新
    var nextOnSuccess = onSuccess;
    if (refreshAfterRequest) {
      nextOnSuccess = function nextOnSuccess(result) {
        onTableEmit('refreshTableData', result);
        onSuccess && onSuccess(result);
      };
    }
    // console.log('dataSource:', dataSource)
    var nextOnError = formProps === null || formProps === void 0 ? void 0 : formProps.onError;
    if (typeof refreshAfterRequestErrorCode !== 'undefined') {
      nextOnError = function nextOnError(response) {
        if ((response === null || response === void 0 ? void 0 : response.code) === refreshAfterRequestErrorCode || refreshAfterRequestErrorCode === true) {
          onTableEmit('refreshTableData');
        }
        if (formProps.onError && typeof formProps.onError === 'function') {
          formProps.onError(response);
        }
      };
    }
    // confirm 的占位
    var nextConfirmBeforeClick = confirmBeforeClick;
    if (confirmBeforeClick) {
      var renderClassName = confirmBeforeClick.renderClassName,
        renderContent = confirmBeforeClick.renderContent,
        className = confirmBeforeClick.className,
        title = confirmBeforeClick.title,
        content = confirmBeforeClick.content,
        _others = _objectWithoutProperties(confirmBeforeClick, _excluded3);
      nextConfirmBeforeClick = _objectSpread({
        className: typeof renderContent === 'function' ? renderClassName(tableProps) : className,
        title: replacePlaceholder(title),
        content: typeof renderContent === 'function' ? renderContent(tableProps) : typeof content === 'string' ? replacePlaceholder(content) : content
      }, _others);
    }

    // 装配请求参数，替换 url 中的 ${id}，在 params 中装配 id 和 idList
    function produceRequest(reqConfig) {
      if (!reqConfig) return reqConfig;
      var url = reqConfig.url,
        params = reqConfig.params,
        _reqConfig$idName = reqConfig.idName,
        idName = _reqConfig$idName === void 0 ? 'id' : _reqConfig$idName,
        otherReqConfig = _objectWithoutProperties(reqConfig, _excluded4);

      // 注入的请求参数
      var data = _objectSpread(_defineProperty(_defineProperty(_defineProperty({}, idName, otherValue || primaryValue), "rowData", record), "idList", [otherValue || primaryValue]), params || {});
      return _objectSpread(_objectSpread({}, otherReqConfig), {}, {
        url: replacePlaceholder(url),
        params: data
        // formatter: () => reqConfig.formatter && reqConfig.formatter(record)
      });
    }
    function disabled() {
      if (_typeof(disabledValue) === 'object') {
        return disabledValue.indexOf(record[disabledKey]) > -1;
      }
      if (disabledExpress) {
        return disabledExpress(record);
      }
      return record[disabledKey] === disabledValue;
    }

    // console.log(buttonProps, 'buttonProps');

    var nextButtonProps = _objectSpread(_objectSpread({
      type: 'link'
    }, buttonProps || {}), {}, {
      disabled: disabledKey || disabledExpress ? disabled() : false
    });

    // 导入导出封装，后续考虑换成洋葱模型
    var nextBatchProps = batchProps;
    if (uiType === 'batch') {
      var uploadProps = batchProps.uploadProps,
        downloadProps = batchProps.downloadProps,
        otherBatchProps = _objectWithoutProperties(batchProps, _excluded5);
      nextBatchProps = _objectSpread({
        uploadProps: _objectSpread(_objectSpread({}, uploadProps), {}, {
          request: produceRequest(uploadProps.request)
        }),
        downloadProps: _objectSpread(_objectSpread({}, downloadProps), {}, {
          request: produceRequest(downloadProps.request)
        })
      }, otherBatchProps);
    }
    return _objectSpread(_objectSpread({}, btnProps), {}, {
      uiType: uiType,
      buttonProps: nextButtonProps,
      to: replacePlaceholder(to),
      confirmBeforeClick: nextConfirmBeforeClick,
      onSuccess: nextOnSuccess,
      request: request && produceRequest(request),
      batchProps: nextBatchProps,
      formProps: formProps && _objectSpread(_objectSpread({}, formProps), {}, {
        request: formProps.request && produceRequest(formProps.request),
        initialValuesRequest: formProps.initialValuesRequest && produceRequest(formProps.initialValuesRequest),
        onError: nextOnError
      })
    });
  });
  return _objectSpread(_objectSpread({
    maxItems: 4
  }, others), {}, {
    noMargin: true,
    dataSource: nextDataSource
  });
});
export default withTableButtonFeatures(ButtonList);