"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderColumnParam = exports["default"] = exports.TableTabsPane = exports.TableTabs = exports.ExportBtn = void 0;
var _tslib = require("tslib");
var _icons = require("@ant-design/icons");
var _ahooks = require("ahooks");
var _antd = require("antd");
var _zh_CN = _interopRequireDefault(require("antd/es/locale/zh_CN"));
var _react = _interopRequireWildcard(require("react"));
var _useSettings4 = _interopRequireDefault(require("../vm-hooks/useSettings"));
require("./style");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
var VMTable = function VMTable(props) {
  var _props$rowKey = props.rowKey,
    rowKey = _props$rowKey === void 0 ? 'id' : _props$rowKey,
    _props$isIndex = props.isIndex,
    isIndex = _props$isIndex === void 0 ? true : _props$isIndex,
    columns = props.columns,
    _props$pagination = props.pagination,
    pagination = _props$pagination === void 0 ? false : _props$pagination,
    _props$height = props.height,
    height = _props$height === void 0 ? 120 : _props$height,
    scroll = props.scroll,
    _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
    onRowCallBack = props.onRowCallBack,
    handleTableChange = props.handleTableChange,
    _props$currentLayer = props.currentLayer,
    currentLayer = _props$currentLayer === void 0 ? {} : _props$currentLayer,
    ext = (0, _tslib.__rest)(props, ["rowKey", "isIndex", "columns", "pagination", "height", "scroll", "dataSource", "onRowCallBack", "handleTableChange", "currentLayer"]);
  var _useSetState = (0, _ahooks.useSetState)({
      selectedRowId: ''
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var newColumns = (0, _react.useMemo)(function () {
    var result = isIndex ? columns ? [{
      title: '序号',
      render: function render(_, __, index) {
        if (pagination && typeof (pagination === null || pagination === void 0 ? void 0 : pagination.current) === 'number' && typeof (pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) === 'number') {
          return ((pagination === null || pagination === void 0 ? void 0 : pagination.current) - 1) * (pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) + index + 1;
        } else {
          return index + 1;
        }
      },
      width: 60
    }].concat(_toConsumableArray(columns)) : undefined : columns;
    if (result) {
      var _iterator = _createForOfIteratorHelper(result),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var col = _step.value;
          if (!col.width) col.width = 70;
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
    return result;
  }, [columns, pagination]);
  var _useSettings = (0, _useSettings4["default"])(),
    theme = _useSettings.theme;
  (0, _react.useEffect)(function () {
    setState({
      selectedRowId: (currentLayer === null || currentLayer === void 0 ? void 0 : currentLayer.id) || ''
    });
  }, [JSON.stringify(currentLayer)]);
  (0, _react.useEffect)(function () {
    if ((currentLayer === null || currentLayer === void 0 ? void 0 : currentLayer.id) && dataSource) {
      onRowCallBack && onRowCallBack(currentLayer);
      var tableDom = document.getElementsByClassName('ant-table-body')[0];
      tableDom.scrollTop = 38 * (dataSource === null || dataSource === void 0 ? void 0 : dataSource.findIndex(function (item) {
        return item.id === currentLayer.id;
      }));
    }
  }, [JSON.stringify(dataSource), JSON.stringify(currentLayer)]);
  return /*#__PURE__*/_react["default"].createElement(_antd.ConfigProvider, {
    locale: _zh_CN["default"]
  }, /*#__PURE__*/_react["default"].createElement(_antd.Table, Object.assign({
    className: "vm-table ".concat(theme),
    rowKey: rowKey,
    size: "small",
    scroll: scroll || {
      y: height,
      scrollToFirstRowOnChange: true
    },
    columns: newColumns,
    pagination: pagination ? Object.assign({
      size: 'default',
      showSizeChanger: false,
      showTotal: function showTotal(total) {
        return "\u5171".concat(total, "\u6761");
      }
    }, pagination) : false,
    dataSource: dataSource,
    rowClassName: function rowClassName(record) {
      return state.selectedRowId === record.id ? "vm-table-row ".concat(theme) : '';
    },
    onRow: function onRow(record) {
      return {
        onClick: function onClick() {
          if (!onRowCallBack) return;
          setState({
            selectedRowId: record.id
          });
          onRowCallBack(record);
        } // click row
      };
    },
    onChange: function onChange(_pagination, _filters, sorter) {
      return handleTableChange && handleTableChange(sorter);
    }
  }, ext)));
};
var ExportBtn = exports.ExportBtn = function ExportBtn(props) {
  var _onClick = props.onClick,
    items = props.items;
  var _useSettings2 = (0, _useSettings4["default"])(),
    theme = _useSettings2.theme;
  var menu = /*#__PURE__*/_react["default"].createElement(_antd.Menu, {
    onClick: function onClick(menuItem) {
      return _onClick && _onClick(menuItem.key);
    },
    items: items || [{
      label: '导出选中行',
      key: 'select-rows'
    }, {
      label: '导出当前页',
      key: 'current-page'
    }, {
      label: '导出全部',
      key: 'all'
    }]
  });
  return /*#__PURE__*/_react["default"].createElement(_antd.Dropdown, {
    overlay: menu,
    trigger: ['click'],
    className: "vtx-export-btn ".concat(theme),
    overlayClassName: "vtx-export-dropdown ".concat(theme)
  }, /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    type: "primary",
    style: {
      padding: '0 10px'
    }
  }, /*#__PURE__*/_react["default"].createElement(_icons.ExportOutlined, null), "\u5BFC\u51FA", /*#__PURE__*/_react["default"].createElement(_icons.DownOutlined, null)));
};
var renderColumnParam = exports.renderColumnParam = function renderColumnParam(cols) {
  var colsParam = [];
  cols.forEach(function (item) {
    colsParam.push({
      title: item.title,
      field: item.dataIndex
    });
  });
  return JSON.stringify(colsParam);
};
var TabPane = _antd.Tabs.TabPane;
var TableTabs = exports.TableTabs = function TableTabs(props) {
  var _useSettings3 = (0, _useSettings4["default"])(),
    theme = _useSettings3.theme;
  return /*#__PURE__*/_react["default"].createElement(_antd.Tabs, Object.assign({}, props, {
    className: "vtx-table-tabs ".concat(theme)
  }), props.children);
};
var TableTabsPane = exports.TableTabsPane = function TableTabsPane(props) {
  return /*#__PURE__*/_react["default"].createElement(TabPane, Object.assign({}, props, {
    style: {
      display: 'none'
    }
  }));
};
var _default = exports["default"] = VMTable;
//# sourceMappingURL=index.js.map
