var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/table/BasicTable/index.tsx
var BasicTable_exports = {};
__export(BasicTable_exports, {
  default: () => BasicTable_default
});
module.exports = __toCommonJS(BasicTable_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_Pagination = require("../../Pagination");
var import_utils = require("../Table/utils");
var import_hooks = require("../../../hooks");
var import_model = require("../model");
var import_utils2 = require("@pisell/utils");
var BasicTable = (props) => {
  var _a;
  const { dataSource, pagination, value, virtual, ...other } = props;
  const [filter, setFilter] = (0, import_react.useState)();
  const [sort, setSort] = (0, import_react.useState)();
  const form = import_antd.Form.useFormInstance();
  const formPagination = import_antd.Form.useWatch("pagination", {
    form,
    preserve: true
  }) || {
    page: 1,
    size: 10
  };
  const group = import_antd.Form.useWatch("group_by", { form, preserve: true });
  const { dispatch } = (0, import_hooks.useSharedState)(import_model.Context);
  const isGroup = (0, import_react.useMemo)(() => (0, import_utils2.isArr)(group) && group.length > 0, [group]);
  const { showTotal, localPagination, showSizeChange } = pagination;
  const _showTotal = (0, import_react.useCallback)(
    (total2, range) => {
      let _size = formPagination.size || 10;
      return showTotal(total2, range, _size);
    },
    [showTotal, formPagination == null ? void 0 : formPagination.page, formPagination == null ? void 0 : formPagination.size]
  );
  (0, import_react.useEffect)(() => {
    let _size = formPagination.size || 100;
    showSizeChange && showSizeChange(formPagination == null ? void 0 : formPagination.page, _size);
  }, [formPagination == null ? void 0 : formPagination.page, formPagination == null ? void 0 : formPagination.size]);
  const _dataSource = (0, import_react.useMemo)(() => {
    let _page = formPagination.page;
    let _size = formPagination.size || 10;
    let list = [];
    let _total = total;
    if (localPagination) {
      list = (0, import_utils.calcFilterSort)({
        dataSource,
        columns: other.columns,
        sort,
        filter
      });
      _total = list.length;
      list = list.slice((_page - 1) * _size, _page * _size);
    } else {
      list = dataSource;
    }
    return {
      list,
      total: _total
    };
  }, [
    localPagination,
    dataSource,
    formPagination == null ? void 0 : formPagination.page,
    formPagination == null ? void 0 : formPagination.size,
    filter,
    sort,
    other.columns
  ]);
  const total = (0, import_react.useMemo)(() => {
    if (localPagination) {
      return (_dataSource == null ? void 0 : _dataSource.total) || 0;
    }
    return (pagination == null ? void 0 : pagination.total) || (_dataSource == null ? void 0 : _dataSource.total) || 0;
  }, [_dataSource, pagination, localPagination]);
  const defaultExpandedRowKeys = (0, import_react.useMemo)(() => {
    var _a2;
    let _expandedRowKeys = [];
    (_a2 = _dataSource == null ? void 0 : _dataSource.list) == null ? void 0 : _a2.forEach((item) => {
      if (item.__is_group) {
        _expandedRowKeys.push(item["__rowKey"]);
      }
    });
    return _expandedRowKeys;
  }, [_dataSource == null ? void 0 : _dataSource.list]);
  (0, import_react.useEffect)(() => {
    dispatch({
      type: "setCurrentDataSource",
      payload: (_dataSource == null ? void 0 : _dataSource.list) || []
    });
  }, [JSON.stringify(_dataSource == null ? void 0 : _dataSource.list)]);
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Table,
    {
      key: defaultExpandedRowKeys.join("-"),
      ...other,
      rowSelection: isGroup ? false : other == null ? void 0 : other.rowSelection,
      virtual,
      onChange: (pagination2, filters, sorter) => {
        form.setFieldsValue({
          pagination: {
            ...formPagination,
            page: 1
          }
        });
        setFilter(filters);
        setSort(sorter);
      },
      pagination: false,
      dataSource: _dataSource.list,
      onRow: (record, index) => {
        var _a2;
        return {
          ...((_a2 = other == null ? void 0 : other.onRow) == null ? void 0 : _a2.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 ? void 0 : pagination.pageSize) || ((_a = pagination == null ? void 0 : pagination.pageSizeOptions) == null ? void 0 : _a[0]) || 10) || (pagination == null ? void 0 : pagination.showPagination) && total > 10 ? /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: "pagination", style: { margin: 0 } }, /* @__PURE__ */ import_react.default.createElement(
    import_Pagination.FormPagination,
    {
      ...pagination,
      showTotal: _showTotal,
      total
    }
  )) : null);
};
var BasicTable_default = BasicTable;
