import _extends from "@babel/runtime/helpers/extends";
import _Tag from "antd/es/tag";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
import { Component, createRef } from 'react';
import { default as OriginalProTable } from '@ant-design/pro-table';
import { defineGetterProperties, isPlainObj } from "../../shared/index";

var ProTable = /*#__PURE__*/function (_Component) {
  _inheritsLoose(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__*/createRef();
    _this.formRef = /*#__PURE__*/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.$ 调用
    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 (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, {
            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(OriginalProTable, _extends({}, this.props, {
      rowSelection: rowSelection ? _extends({}, 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;
}(Component);

export default ProTable;