function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["dataSource", "pagination", "value", "virtual"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Table, Form } from 'antd';
import React, { useState, useMemo, useCallback, useEffect } from 'react';
import { FormPagination } from "../../Pagination";
import { calcFilterSort } from "../Table/utils";
import { useSharedState } from "../../../hooks";
import { Context } from "../model";
import { isArr } from '@pisell/utils';
var BasicTable = function BasicTable(props) {
  var _pagination$pageSizeO;
  var dataSource = props.dataSource,
    pagination = props.pagination,
    value = props.value,
    virtual = props.virtual,
    other = _objectWithoutProperties(props, _excluded);
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    filter = _useState2[0],
    setFilter = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    sort = _useState4[0],
    setSort = _useState4[1];
  var form = Form.useFormInstance();
  var formPagination = Form.useWatch('pagination', {
    form: form,
    preserve: true
  }) || {
    page: 1,
    size: 10
  };
  var group = Form.useWatch('group_by', {
    form: form,
    preserve: true
  });
  var _useSharedState = useSharedState(Context),
    dispatch = _useSharedState.dispatch;
  var isGroup = useMemo(function () {
    return isArr(group) && group.length > 0;
  }, [group]);
  var showTotal = pagination.showTotal,
    localPagination = pagination.localPagination,
    showSizeChange = pagination.showSizeChange;
  var _showTotal = useCallback(function (total, range) {
    var _size = formPagination.size || 10;
    return showTotal(total, range, _size);
  }, [showTotal, formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, formPagination === null || formPagination === void 0 ? void 0 : formPagination.size]);
  useEffect(function () {
    // 默认100条每页
    var _size = formPagination.size || 100;
    showSizeChange && showSizeChange(formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, _size);
  }, [formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, formPagination === null || formPagination === void 0 ? void 0 : formPagination.size]);

  // 分割数据源
  var _dataSource = useMemo(function () {
    var _page = formPagination.page;
    var _size = formPagination.size || 10;
    var list = [];
    var _total = total;
    if (localPagination) {
      // 这里本地前端过滤筛选
      list = calcFilterSort({
        dataSource: dataSource,
        columns: other.columns,
        sort: sort,
        filter: filter
      });
      _total = list.length;
      list = list.slice((_page - 1) * _size, _page * _size);
    } else {
      list = dataSource;
    }
    return {
      list: list,
      total: _total
    };
  }, [localPagination, dataSource, formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, formPagination === null || formPagination === void 0 ? void 0 : formPagination.size, filter, sort, other.columns]);

  // 总数
  var total = useMemo(function () {
    // 如果dataSource.length > 当前分页时为前端分页处理 此时total优先取_dataSource中total
    // 改为通过localPagination判断
    if (localPagination) {
      return (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.total) || 0;
    }
    return (pagination === null || pagination === void 0 ? void 0 : pagination.total) || (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.total) || 0;
  }, [_dataSource, pagination, localPagination]);
  var defaultExpandedRowKeys = useMemo(function () {
    var _dataSource$list;
    var _expandedRowKeys = [];
    _dataSource === null || _dataSource === void 0 || (_dataSource$list = _dataSource.list) === null || _dataSource$list === void 0 || _dataSource$list.forEach(function (item) {
      if (item.__is_group) {
        _expandedRowKeys.push(item['__rowKey']);
      }
    });
    return _expandedRowKeys;
  }, [_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list]);
  useEffect(function () {
    dispatch({
      type: 'setCurrentDataSource',
      payload: (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list) || []
    });
  }, [JSON.stringify(_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list)]);
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Table, _extends({
    key: defaultExpandedRowKeys.join('-')
  }, other, {
    // 开启分组条件后关闭行选择功能
    rowSelection: isGroup ? false : other === null || other === void 0 ? void 0 : other.rowSelection,
    virtual: virtual,
    onChange: function onChange(pagination, filters, sorter) {
      form.setFieldsValue({
        pagination: _objectSpread(_objectSpread({}, formPagination), {}, {
          page: 1
        })
      });
      setFilter(filters);
      setSort(sorter);
    },
    pagination: false,
    dataSource: _dataSource.list,
    onRow: function onRow(record, index) {
      var _other$onRow;
      return _objectSpread(_objectSpread({}, (other === null || other === void 0 || (_other$onRow = other.onRow) === null || _other$onRow === void 0 ? void 0 : _other$onRow.call(other, record, index)) || {}), {}, {
        __is_group: record.__is_group,
        __is_drag_sort: other.dragSort,
        className: record.__is_group ? 'pisell-lowcode-table-group-row-wrap' : ''
      });
    },
    expandable: {
      defaultExpandAllRows: true
    }
  })), total > ((pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) || (pagination === null || pagination === void 0 || (_pagination$pageSizeO = pagination.pageSizeOptions) === null || _pagination$pageSizeO === void 0 ? void 0 : _pagination$pageSizeO[0]) || 10) || pagination !== null && pagination !== void 0 && pagination.showPagination && total > 10 ? /*#__PURE__*/React.createElement(Form.Item, {
    name: "pagination",
    style: {
      margin: 0
    }
  }, /*#__PURE__*/React.createElement(FormPagination, _extends({}, pagination, {
    showTotal: _showTotal,
    total: total
  }))) : null);
};
export default BasicTable;