import _extends from "@babel/runtime/helpers/extends";
import assign from "lodash/assign";
import deepcopy from "lodash/cloneDeep";
import { isValidElement } from "react";
import convertData from "../utils/convertData";
import getDataSource from "../utils/getDataSource";
var methods = {
  init: function init() {
    var _this$props$paginatio = this.props.pagination,
      pagination = _this$props$paginatio === void 0 ? {} : _this$props$paginatio;
    var pageSize = pagination.pageSize;
    var result = convertData(getDataSource(this.props));
    var mergeState = {
      searchKey: '',
      orderColumn: '',
      orderType: '',
      dataSource: result.dataSource || [],
      totalCount: result.totalCount || 0,
      pageSize: pageSize,
      currentPage: result.currentPage || 1
    };
    this.state = this.state || {};
    assign(this.state, mergeState);
  },
  loadData: function loadData(from) {
    var _this$props = this.props,
      onFetchData = _this$props.onFetchData,
      onLoadData = _this$props.onLoadData;
    var _this$state = this.state,
      currentPage = _this$state.currentPage,
      pageSize = _this$state.pageSize,
      searchKey = _this$state.searchKey,
      orderColumn = _this$state.orderColumn,
      orderType = _this$state.orderType;
    if (onLoadData) {
      onLoadData(currentPage, pageSize, searchKey, orderColumn, orderType, from);
    } else {
      onFetchData && onFetchData({
        currentPage: currentPage,
        pageSize: pageSize,
        searchKey: searchKey,
        orderColumn: orderColumn,
        orderType: orderType,
        from: from
      });
    }
  },
  getDataSource: function getDataSource() {
    var dataSource = this.state.dataSource;
    var list = deepcopy(dataSource);
    list.forEach(function (item) {
      delete item.__mode__;
      delete item.__fields__;
      delete item.__expand__;
    });
    return list;
  },
  getDataItem: function getDataItem(value) {
    var primaryKey = this.props.primaryKey;
    var dataSource = this.state.dataSource;
    return dataSource.find(function (item) {
      return item[primaryKey] === value;
    });
  },
  onSort: function onSort(dataIndex, order) {
    var _this = this;
    this.setState({
      orderColumn: dataIndex,
      orderType: order
    }, function () {
      _this.loadData('order');
    });
  },
  onSearch: function onSearch(searchKey) {
    var _this2 = this;
    this.setState({
      searchKey: searchKey
    }, function () {
      _this2.loadData('search');
    });
  },
  renderEmptyContent: function renderEmptyContent() {
    var _this$props2 = this.props,
      setEmptyContent = _this$props2.setEmptyContent,
      emptyContent = _this$props2.emptyContent;
    if (!setEmptyContent) {
      return null;
    }
    return typeof emptyContent === 'function' ? emptyContent() : /*#__PURE__*/isValidElement(emptyContent) ? emptyContent : null;
  },
  onPageSizeChange: function onPageSizeChange(pageSize) {
    var _this3 = this;
    this.setState({
      pageSize: pageSize
    }, function () {
      _this3.loadData('pagination');
    });
  },
  onPageNumChange: function onPageNumChange(current) {
    var _this4 = this;
    this.setState({
      currentPage: current
    }, function () {
      _this4.loadData('pagination');
    });
  },
  getPaginationProps: function getPaginationProps() {
    var _this$props3 = this.props,
      nextTablePrefix = _this$props3.nextTablePrefix,
      pagination = _this$props3.pagination,
      locale = _this$props3.locale,
      noPadding = _this$props3.noPadding;
    var _this$state2 = this.state,
      currentPage = _this$state2.currentPage,
      pageSize = _this$state2.pageSize,
      totalCount = _this$state2.totalCount;
    return _extends({
      nextTablePrefix: nextTablePrefix,
      currentPage: currentPage,
      totalCount: totalCount
    }, pagination, {
      noPadding: noPadding,
      pageSize: pageSize,
      locale: locale,
      onChange: this.onPageNumChange.bind(this),
      onPageSizeChange: this.onPageSizeChange.bind(this)
    });
  }
};
export default methods;