"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _table = _interopRequireDefault(require("@alifd/next/lib/table"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
var _convertData = _interopRequireDefault(require("../utils/convertData"));
var _loadMethods = _interopRequireDefault(require("../mixin/loadMethods"));
var _commonMethods = _interopRequireDefault(require("../mixin/commonMethods"));
var _columnMethods = _interopRequireDefault(require("../mixin/columnMethods"));
var _webToolbar = _interopRequireDefault(require("../component/webToolbar"));
var _webPagination = _interopRequireDefault(require("../component/webPagination"));
var _hasRowAction = _interopRequireDefault(require("../utils/hasRowAction"));
var _runToolbarActionCallback = _interopRequireDefault(require("../utils/runToolbarActionCallback"));
var _buildTableProps = _interopRequireDefault(require("../utils/buildTableProps"));
var _editableMethods = _interopRequireDefault(require("../mixin/editableMethods"));
var _webNextTableActionCell = _interopRequireDefault(require("../component/webNextTableActionCell"));
var _webNextTableCell = _interopRequireDefault(require("../component/webNextTableCell"));
var _getDataSource = _interopRequireDefault(require("../utils/getDataSource"));
var _titleMessageRender = _interopRequireDefault(require("../utils/render/titleMessageRender"));
var WebTable = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(WebTable, _React$Component);
  function WebTable(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    (0, _loadMethods["default"])((0, _assertThisInitialized2["default"])(_this), _commonMethods["default"]);
    (0, _loadMethods["default"])((0, _assertThisInitialized2["default"])(_this), _columnMethods["default"]);
    (0, _loadMethods["default"])((0, _assertThisInitialized2["default"])(_this), _editableMethods["default"]);
    return _this;
  }
  var _proto = WebTable.prototype;
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var data = this.props.data;
    var nextData = (0, _getDataSource["default"])(nextProps);
    if (!(0, _isEqual["default"])(nextData, data)) {
      this.setState((0, _convertData["default"])(nextData));
    }
    var originalColumns = this.state.originalColumns;
    var nextColumns = nextProps.columns;
    if (!(0, _isEqual["default"])(originalColumns, nextColumns)) {
      this.setOriginalColumns(nextColumns);
      this.changeCurrentColumns(nextColumns);
    }
  };
  _proto.renderColumn = function renderColumn(column) {
    var _this$props = this.props,
      nextTablePrefix = _this$props.nextTablePrefix,
      onCellDataChange = _this$props.onCellDataChange;
    var lock = column.lock,
      dataKey = column.dataKey,
      width = column.width,
      title = column.title,
      titleRender = column.titleRender,
      message = column.message,
      sortable = column.sortable,
      align = column.align,
      resizable = column.resizable,
      filters = column.filters,
      filterMode = column.filterMode;
    var props = {
      dataIndex: dataKey,
      cell: function cell(value, rowIndex, rowData) {
        return /*#__PURE__*/_react["default"].createElement(_webNextTableCell["default"], {
          value: value,
          rowIndex: rowIndex,
          rowData: rowData,
          column: column,
          isRenderEditMode: true,
          nextTablePrefix: nextTablePrefix,
          onCellDataChange: onCellDataChange
        });
      },
      title: (0, _titleMessageRender["default"])(title, message, nextTablePrefix),
      width: width,
      sortable: sortable,
      align: align,
      resizable: resizable,
      filters: filters,
      filterMode: filterMode
    };
    if (lock && lock !== 'none') {
      props.lock = lock;
    }
    if (titleRender) {
      props.title = titleRender(column.title);
    }
    return /*#__PURE__*/_react["default"].createElement(_table["default"].Column, (0, _extends2["default"])({}, props, {
      key: dataKey
    }));
  };
  _proto.renderColumns = function renderColumns(columns) {
    var _this2 = this;
    return columns ? columns.map(function (column) {
      if (!column.isGroup) {
        return _this2.renderColumn(column);
      }
      return column.children && column.children.length ? /*#__PURE__*/_react["default"].createElement(_table["default"].ColumnGroup, {
        key: column.dataKey || "_" + Math.random(),
        title: column.title || 'group'
      }, _this2.renderColumns(column.children)) : null;
    }) : null;
  };
  _proto.renderActionColumns = function renderActionColumns() {
    var _this$props2 = this.props,
      actionTitle = _this$props2.actionTitle,
      actionFixed = _this$props2.actionFixed,
      actionWidth = _this$props2.actionWidth,
      actionColumn = _this$props2.actionColumn,
      actionType = _this$props2.actionType,
      maxWebShownActionCount = _this$props2.maxWebShownActionCount,
      nextTablePrefix = _this$props2.nextTablePrefix,
      device = _this$props2.device,
      primaryKey = _this$props2.primaryKey,
      locale = _this$props2.locale;
    if (!(0, _hasRowAction["default"])(this.props)) {
      return null;
    }
    var actionColumnProps = {
      title: actionTitle,
      lock: actionFixed !== 'none' ? actionFixed : false,
      width: actionWidth,
      cell: function cell(value, index, rowData) {
        return /*#__PURE__*/_react["default"].createElement(_webNextTableActionCell["default"], {
          locale: locale,
          device: device,
          actionColumn: actionColumn,
          actionType: actionType,
          maxWebShownActionCount: maxWebShownActionCount,
          nextTablePrefix: nextTablePrefix,
          primaryKey: primaryKey,
          value: value,
          index: index,
          rowData: rowData
        });
      }
    };
    return /*#__PURE__*/_react["default"].createElement(_table["default"].Column, actionColumnProps);
  };
  _proto.render = function render() {
    var _this$props3 = this.props,
      nextTablePrefix = _this$props3.nextTablePrefix,
      isPagination = _this$props3.isPagination,
      locale = _this$props3.locale;
    var _this$state = this.state,
      dataSource = _this$state.dataSource,
      currentColumns = _this$state.currentColumns;
    var paginationProps = this.getPaginationProps();
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "web-table"
    }, /*#__PURE__*/_react["default"].createElement(_webToolbar["default"], (0, _extends2["default"])({}, this.props, {
      onActionClick: function onActionClick(item) {
        (0, _runToolbarActionCallback["default"])({
          action: item
        });
      },
      onSearch: this.onSearch.bind(this),
      onClickCustomColumn: this.showCustomColumnDrawer.bind(this),
      paginationProps: paginationProps
    })), /*#__PURE__*/_react["default"].createElement(_table["default"], (0, _buildTableProps["default"])((0, _extends2["default"])({}, this.props, {
      dataSource: dataSource,
      onSort: this.onSort.bind(this),
      emptyContent: this.renderEmptyContent()
    })), this.renderColumns(currentColumns), this.renderActionColumns()), isPagination ? /*#__PURE__*/_react["default"].createElement(_webPagination["default"], (0, _extends2["default"])({}, paginationProps, {
      locale: locale
    })) : null, this.renderCustomColumnDrawer());
  };
  return WebTable;
}(_react["default"].Component);
WebTable.displayName = 'WebTable';
var _default = WebTable;
exports["default"] = _default;