import _Table from "@alifd/next/es/table";
import _extends from "@babel/runtime/helpers/extends";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from 'react';
import isEqual from 'lodash/isEqual';
import convertData from "../utils/convertData";
import loadMethods from "../mixin/loadMethods";
import commonMethods from "../mixin/commonMethods";
import columnMethods from "../mixin/columnMethods";
import WebToolbar from "../component/webToolbar";
import WebPagination from "../component/webPagination";
import hasRowAction from "../utils/hasRowAction";
import runToolbarActionCallback from "../utils/runToolbarActionCallback";
import buildTableProps from "../utils/buildTableProps";
import editableMethods from "../mixin/editableMethods";
import WebNextTableActionCell from "../component/webNextTableActionCell";
import WebNextTableCell from "../component/webNextTableCell";
import getDataSource from "../utils/getDataSource";
import titleMessageRender from "../utils/render/titleMessageRender";
var WebTable = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(WebTable, _React$Component);
  function WebTable(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    loadMethods(_assertThisInitialized(_this), commonMethods);
    loadMethods(_assertThisInitialized(_this), columnMethods);
    loadMethods(_assertThisInitialized(_this), editableMethods);
    return _this;
  }
  var _proto = WebTable.prototype;
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var data = this.props.data;
    var nextData = getDataSource(nextProps);
    if (!isEqual(nextData, data)) {
      this.setState(convertData(nextData));
    }
    var originalColumns = this.state.originalColumns;
    var nextColumns = nextProps.columns;
    if (!isEqual(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.createElement(WebNextTableCell, {
          value: value,
          rowIndex: rowIndex,
          rowData: rowData,
          column: column,
          isRenderEditMode: true,
          nextTablePrefix: nextTablePrefix,
          onCellDataChange: onCellDataChange
        });
      },
      title: titleMessageRender(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.createElement(_Table.Column, _extends({}, 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.createElement(_Table.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 (!hasRowAction(this.props)) {
      return null;
    }
    var actionColumnProps = {
      title: actionTitle,
      lock: actionFixed !== 'none' ? actionFixed : false,
      width: actionWidth,
      cell: function cell(value, index, rowData) {
        return /*#__PURE__*/React.createElement(WebNextTableActionCell, {
          locale: locale,
          device: device,
          actionColumn: actionColumn,
          actionType: actionType,
          maxWebShownActionCount: maxWebShownActionCount,
          nextTablePrefix: nextTablePrefix,
          primaryKey: primaryKey,
          value: value,
          index: index,
          rowData: rowData
        });
      }
    };
    return /*#__PURE__*/React.createElement(_Table.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.createElement("div", {
      className: nextTablePrefix + "web-table"
    }, /*#__PURE__*/React.createElement(WebToolbar, _extends({}, this.props, {
      onActionClick: function onActionClick(item) {
        runToolbarActionCallback({
          action: item
        });
      },
      onSearch: this.onSearch.bind(this),
      onClickCustomColumn: this.showCustomColumnDrawer.bind(this),
      paginationProps: paginationProps
    })), /*#__PURE__*/React.createElement(_Table, buildTableProps(_extends({}, this.props, {
      dataSource: dataSource,
      onSort: this.onSort.bind(this),
      emptyContent: this.renderEmptyContent()
    })), this.renderColumns(currentColumns), this.renderActionColumns()), isPagination ? /*#__PURE__*/React.createElement(WebPagination, _extends({}, paginationProps, {
      locale: locale
    })) : null, this.renderCustomColumnDrawer());
  };
  return WebTable;
}(React.Component);
WebTable.displayName = 'WebTable';
export default WebTable;