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 = ["columns", "dataSource", "cellClassName", "comsMap", "checkable", "selectedRowKeys", "onSelectChange", "primaryKey", "otherKey", "onPageChange", "onEmit", "scroll", "size", "fixFirstColumn", "fixLastColumn"],
  _excluded2 = ["name", "label", "width", "uiType", "props", "cell", "render", "help", "fixed"];
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 _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; }
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); }
/* eslint-disable no-unused-expressions */
import React, { useCallback, useMemo, Suspense } from 'react';
import { Table, Tooltip } from 'antd';
import { QuestionCircleOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import Spin from "../loading";
import defaultComsMap, { UnSupport } from "./coms-map";
import "./index.less";

// import React, { Suspense } from 'react';
// import { PropTypes } from './interface';
// import Button from './default';
// import { Spin, Empty } from '@/blank';

var cellRenderer = function cellRenderer(_ref) {
  var comsMap = _ref.comsMap,
    uiType = _ref.uiType,
    tableDataSource = _ref.tableDataSource,
    name = _ref.name,
    _ref$props = _ref.props,
    props = _ref$props === void 0 ? {} : _ref$props,
    _onEmit = _ref.onEmit,
    otherKey = _ref.otherKey,
    _ref$primaryKey = _ref.primaryKey,
    primaryKey = _ref$primaryKey === void 0 ? 'id' : _ref$primaryKey;
  var Com = (comsMap === null || comsMap === void 0 ? void 0 : comsMap[uiType]) || (defaultComsMap === null || defaultComsMap === void 0 ? void 0 : defaultComsMap[uiType]) || UnSupport;
  return function (value, record, index) {
    return /*#__PURE__*/React.createElement(Suspense, {
      fallback: /*#__PURE__*/React.createElement("div", null, "Loading...")
    }, /*#__PURE__*/React.createElement(Com, _extends({
      className: classNames("cf-table-col-".concat(uiType), props.cellClassName)
    }, props, {
      value: value,
      tableProps: {
        primaryKey: primaryKey,
        otherKey: otherKey,
        onEmit: function onEmit(eventName) {
          for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
            args[_key - 1] = arguments[_key];
          }
          _onEmit.apply(void 0, ["".concat(name, ".").concat(eventName), index, record].concat(args));
        },
        onTableEmit: function onTableEmit(eventName) {
          for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
            args[_key2 - 1] = arguments[_key2];
          }
          _onEmit.apply(void 0, [eventName].concat(args));
        },
        tableDataSource: tableDataSource,
        uiType: uiType,
        index: index,
        record: record,
        name: name
      }
    })));
  };
};
export default function CFTable(_ref2) {
  var columns = _ref2.columns,
    dataSource = _ref2.dataSource,
    cellClassName = _ref2.cellClassName,
    comsMap = _ref2.comsMap,
    checkable = _ref2.checkable,
    selectedRowKeys = _ref2.selectedRowKeys,
    onSelectChange = _ref2.onSelectChange,
    primaryKey = _ref2.primaryKey,
    otherKey = _ref2.otherKey,
    onPageChange = _ref2.onPageChange,
    onEmit = _ref2.onEmit,
    scroll = _ref2.scroll,
    _ref2$size = _ref2.size,
    size = _ref2$size === void 0 ? 'small' : _ref2$size,
    _ref2$fixFirstColumn = _ref2.fixFirstColumn,
    fixFirstColumn = _ref2$fixFirstColumn === void 0 ? true : _ref2$fixFirstColumn,
    _ref2$fixLastColumn = _ref2.fixLastColumn,
    fixLastColumn = _ref2$fixLastColumn === void 0 ? true : _ref2$fixLastColumn,
    others = _objectWithoutProperties(_ref2, _excluded);
  console.log(dataSource, 'dataSource');
  console.log('primaryKey:', primaryKey);
  var renderColumn = function renderColumn(_ref3, colIndex, primaryKey) {
    var name = _ref3.name,
      label = _ref3.label,
      width = _ref3.width,
      uiType = _ref3.uiType,
      props = _ref3.props,
      cell = _ref3.cell,
      render = _ref3.render,
      help = _ref3.help,
      fixed = _ref3.fixed,
      others = _objectWithoutProperties(_ref3, _excluded2);
    if (typeof cell !== 'function' && typeof render !== 'function') {
      cell = cellRenderer({
        comsMap: comsMap,
        uiType: uiType || 'text',
        name: name,
        props: props,
        onEmit: onEmit,
        primaryKey: primaryKey,
        otherKey: otherKey,
        tableDataSource: dataSource
      });
    }
    var _fixed = fixed;

    // 如果列数大于 2，则根据配置锁定第一列
    if (!_fixed && columns.length > 2) {
      if (fixFirstColumn && colIndex === 0) {
        _fixed = 'left';
      }
    }

    // 操作列永久固定
    if (name === 'operation' || label === '操作' || uiType === 'buttonList') {
      _fixed = 'right';
    }
    if (!dataSource || !dataSource.length) {
      // 如果数据源为空，则取消 fixed，不然 antd 的 table 的「数据为空」的展示会异常
      _fixed = undefined;
    }
    return /*#__PURE__*/React.createElement(Table.Column, _extends({}, others, {
      dataIndex: name,
      key: name,
      title: /*#__PURE__*/React.createElement("span", null, label || name, help ? /*#__PURE__*/React.createElement(Tooltip, {
        className: "cf-table-header-help",
        placement: "top",
        title: help
      }, /*#__PURE__*/React.createElement(QuestionCircleOutlined, null)) : null),
      fixed: _fixed,
      width: width || '',
      render: cell || render,
      className: classNames('cf-table-cell', {
        '_padding-right-30': !!others._paddingR30
      })
    }));
  };
  var renderLoading = useCallback(function (props) {
    return /*#__PURE__*/React.createElement(Spin, _extends({
      delay: 500
    }, props));
  }, []);
  var _others$rowSelection = others.rowSelection,
    rowSelection = _others$rowSelection === void 0 ? {} : _others$rowSelection,
    _others$pagination = others.pagination,
    pagination = _others$pagination === void 0 ? {} : _others$pagination;
  if (pagination) {
    pagination.onChange = function (pageNo, pageSize) {
      onPageChange && onPageChange(pageNo, pageSize);
    };
    pagination.onShowSizeChange = function (current, size) {
      onPageChange && onPageChange(current, size);
    };
  }
  if (!checkable) {
    rowSelection = null;
  } else {
    rowSelection = _objectSpread(_objectSpread({}, rowSelection), {}, {
      selectedRowKeys: selectedRowKeys || rowSelection.selectedRowKeys,
      onChange: function onChange(nextSelectedRowKeys, nextSelectedRows) {
        onSelectChange && onSelectChange(nextSelectedRowKeys, nextSelectedRows);
      }
    });
  }
  var _scroll = useMemo(function () {
    if (columns.length > 5 && columns.length <= 8) {
      return {
        x: 1000
      };
    } else if (columns.length > 8 && columns.length <= 11) {
      return {
        x: 1300
      };
    } else if (columns.length > 11 && columns.length <= 13) {
      return {
        x: 1600
      };
    } else if (columns.length > 13) {
      return {
        x: 2000
      };
    } else if (columns.length <= 5) {
      return {
        x: 800
      };
    }
  }, [columns]);
  return /*#__PURE__*/React.createElement(Table, _extends({
    scroll: scroll || _scroll,
    dataSource: dataSource,
    loadingComponent: renderLoading,
    rowKey: primaryKey,
    size: size
  }, others, {
    pagination: pagination,
    rowSelection: rowSelection
  }), columns.map(renderColumn));
}
CFTable.defaultProps = {
  dataSource: [],
  hasBorder: false,
  cellClassName: '',
  comsMap: {}
};

// export { TableCellProps, PropTypes, ColumnItemProps };