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/Table/index.tsx
var Table_exports = {};
__export(Table_exports, {
  default: () => Table_default
});
module.exports = __toCommonJS(Table_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_model = require("../model");
var import_BasicTable = __toESM(require("../BasicTable"));
var import_SortRow = __toESM(require("./tableConfig/SortRow"));
var import_Pagination = require("../../Pagination");
var import_locales = require("../../../locales");
var import_useTransDataSource = __toESM(require("../hooks/useTransDataSource"));
var import_useTransColumns = __toESM(require("../hooks/useTransColumns"));
var import_useGenTableComponents = __toESM(require("../hooks/useGenTableComponents"));
var import_useGenScroll = __toESM(require("../hooks/useGenScroll"));
var import_hooks = require("../../../hooks");
var import_index = require("./index.less");
var GridViewTable = ({
  tableProps,
  filter,
  setTableSetting,
  onDataSourceChange
}) => {
  var _a, _b;
  const { state, dispatch } = (0, import_hooks.useSharedState)(import_model.Context);
  const form = import_antd.Form.useFormInstance();
  const column_setting = import_antd.Form.useWatch("column_setting", { form, preserve: true }) || [];
  const tableWrapRef = (0, import_react.useRef)(null);
  const { dataSource: defaultDataSource, originDataSource, isTerminal } = state;
  const handleSave = (0, import_ahooks.useMemoizedFn)((row) => {
    const newData = [...defaultDataSource];
    const index = newData.findIndex((item2) => row.key === item2.key);
    const item = newData[index];
    newData.splice(index, 1, {
      ...item,
      ...row
    });
    dispatch({
      type: "setDataSource",
      payload: newData
    });
  });
  const handleResize = (index) => (e, { size }) => {
    const nextColumns = [...columns];
    nextColumns[index] = {
      ...nextColumns[index],
      width: size.width
    };
    form.setFieldValue("column_setting", nextColumns);
    setTableSetting(form.getFieldsValue());
  };
  const { columns } = (0, import_useTransColumns.default)({
    columns: column_setting,
    form,
    localPagination: !!((_a = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _a.localPagination),
    handleResize,
    handleSave,
    dragSort: tableProps == null ? void 0 : tableProps.dragSort,
    bordered: tableProps.bordered
  });
  const dataSource = (0, import_useTransDataSource.default)({
    dataSource: originDataSource,
    form,
    localPagination: !!((_b = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _b.localPagination),
    rowKey: tableProps.rowKey,
    filter
  });
  (0, import_react.useEffect)(() => {
    dispatch({
      type: "setOriginResultDataSource",
      payload: dataSource || []
    });
  }, [dataSource]);
  (0, import_react.useEffect)(() => {
    onDataSourceChange == null ? void 0 : onDataSourceChange(dataSource);
  }, [JSON.stringify(dataSource)]);
  const components = (0, import_useGenTableComponents.default)({
    form
  });
  const showTotal = (0, import_react.useCallback)(
    (total, range, pageSize) => {
      let start = range[0] - 1;
      let _current = start / pageSize + 1;
      return (0, import_locales.getText)("table-pagination-total")(
        _current,
        Math.ceil(total / pageSize)
      );
    },
    []
  );
  const _paginationConfig = (0, import_react.useMemo)(() => {
    if (tableProps.pagination === false) {
      return false;
    }
    return {
      ...import_Pagination.paginationConfig,
      ...tableProps.pagination,
      showTotal
    };
  }, [tableProps.pagination]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "materials-grid-table-wrap", ref: tableWrapRef }, /* @__PURE__ */ import_react.default.createElement(
    AutoSizerContent,
    {
      isTerminal,
      tableWrapRef,
      dataSource,
      tableProps,
      columns,
      paginationConfig: _paginationConfig,
      components
    }
  ));
};
var AutoSizerContent = (props) => {
  var _a, _b, _c, _d;
  const {
    tableProps,
    dataSource,
    paginationConfig: paginationConfig2,
    columns,
    components,
    tableWrapRef,
    isTerminal
  } = props;
  const { height } = (0, import_ahooks.useSize)(tableWrapRef) || {};
  const debouncedHeight = (0, import_ahooks.useDebounce)(height, { wait: 500 });
  const isShowPagination = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2, _d2, _e, _f;
    let total = ((_a2 = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _a2.total) || (dataSource == null ? void 0 : dataSource.length) || 0;
    if ((_b2 = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _b2.localPagination) {
      total = (dataSource == null ? void 0 : dataSource.length) || 0;
    }
    return total > (((_c2 = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _c2.pageSize) || ((_e = (_d2 = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _d2.pageSizeOptions) == null ? void 0 : _e[0]) || 10) || ((_f = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _f.showPagination) && total > 10;
  }, [
    (_a = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _a.localPagination,
    (_b = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _b.total,
    dataSource.length,
    (_d = (_c = tableProps == null ? void 0 : tableProps.pagination) == null ? void 0 : _c.pageSizeOptions) == null ? void 0 : _d[0]
  ]);
  const scroll = (0, import_useGenScroll.default)({
    wrapHeight: height || 500,
    scroll: tableProps.scroll,
    columns,
    isShowPagination
  });
  const debouncedScroll = (0, import_ahooks.useDebounce)(scroll, { wait: 200 });
  const platformScroll = (0, import_react.useMemo)(() => {
    if (isTerminal) {
      return debouncedScroll;
    }
    return scroll;
  }, [debouncedScroll, scroll, isTerminal]);
  const basicTable = (0, import_react.useMemo)(() => {
    if (!height) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_BasicTable.default,
      {
        dataSource,
        columns,
        className: "grid-view-table",
        components,
        ...tableProps,
        scroll: platformScroll,
        pagination: paginationConfig2
      }
    );
  }, [
    height,
    platformScroll,
    dataSource,
    columns,
    components,
    paginationConfig2,
    tableProps
  ]);
  return /* @__PURE__ */ import_react.default.createElement(import_SortRow.default, { rowKey: tableProps.rowKey, onSortChange: tableProps.onSortChange }, basicTable);
};
var Table_default = (0, import_react.memo)(GridViewTable);
