"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _tag = _interopRequireDefault(require("antd/lib/tag"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var React = _interopRequireWildcard(require("react"));

var _proTable = _interopRequireDefault(require("@ant-design/pro-table"));

var _index = require("../../shared/index");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var ProTable = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2["default"])(ProTable, _Component);

  function ProTable() {
    var _selectedRowKeys, _this$props$rowSelect;

    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _Component.call.apply(_Component, [this].concat(args)) || this;
    _this.state = {
      selectedRowKeys: (_selectedRowKeys = (_this$props$rowSelect = _this.props.rowSelection) === null || _this$props$rowSelect === void 0 ? void 0 : _this$props$rowSelect.selectedRowKeys) !== null && _selectedRowKeys !== void 0 ? _selectedRowKeys : []
    };
    _this.actionRef = /*#__PURE__*/(0, React.createRef)();
    _this.formRef = /*#__PURE__*/(0, React.createRef)();

    _this.onSelectRowsChange = function (selectedRowKeys, selectedRows) {
      _this.setState({
        selectedRowKeys: selectedRowKeys
      });
    };

    return _this;
  }

  var _proto = ProTable.prototype;

  _proto.getSelectedRowKeys = function getSelectedRowKeys() {
    return this.state.selectedRowKeys;
  };

  _proto.setSelectedRowKeys = function setSelectedRowKeys(selectedRowKeys) {
    this.setState({
      selectedRowKeys: Array.isArray(selectedRowKeys) ? selectedRowKeys : [selectedRowKeys]
    });
  };

  _proto.componentDidMount = function componentDidMount() {
    // 把操作方法挂载到 class instance 上，可通过 this.$ 调用
    (0, _index.defineGetterProperties)(this, [this.actionRef, this.formRef]);
  };

  _proto.render = function render() {
    var _this2 = this;

    var _this$props = this.props,
        columns = _this$props.columns,
        rowSelection = _this$props.rowSelection;
    var selectedRowKeys = this.state.selectedRowKeys; // 劫持渲染标签类型的列

    columns === null || columns === void 0 ? void 0 : columns.map(function (item) {
      if ((0, _index.isPlainObj)(item.valueEnum) && item.renderTag === true) {
        item.render = function (_, record) {
          var _target$status;

          var colValue = record[item.dataIndex];
          var target = item.valueEnum[colValue];
          return target !== null && target !== void 0 && target.text ? /*#__PURE__*/React.createElement(_tag["default"], {
            color: target === null || target === void 0 ? void 0 : (_target$status = target.status) === null || _target$status === void 0 ? void 0 : _target$status.toLowerCase()
          }, target === null || target === void 0 ? void 0 : target.text) : '-';
        };
      }
    });
    return /*#__PURE__*/React.createElement(_proTable["default"], (0, _extends2["default"])({}, this.props, {
      rowSelection: rowSelection ? (0, _extends2["default"])({}, rowSelection, {
        defaultSelectedRowKeys: selectedRowKeys,
        selectedRowKeys: selectedRowKeys,
        onChange: function onChange() {
          var _rowSelection$onChang;

          for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
            args[_key2] = arguments[_key2];
          }

          rowSelection === null || rowSelection === void 0 ? void 0 : (_rowSelection$onChang = rowSelection.onChange) === null || _rowSelection$onChang === void 0 ? void 0 : _rowSelection$onChang.call.apply(_rowSelection$onChang, [rowSelection].concat(args));

          _this2.onSelectRowsChange.apply(_this2, args);
        }
      }) : false,
      columns: columns,
      actionRef: this.actionRef,
      formRef: this.formRef
    }));
  };

  return ProTable;
}(React.Component);

var _default = ProTable;
exports["default"] = _default;