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/dataSourceComponents/dataSourceTable/BaseTable.tsx
var BaseTable_exports = {};
__export(BaseTable_exports, {
  default: () => BaseTable_default
});
module.exports = __toCommonJS(BaseTable_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_table = __toESM(require("../../table"));
var import_useVariables = __toESM(require("../hooks/useVariables"));
var import_ActionsProvider = __toESM(require("../provider/actions/ActionsProvider"));
var import_useDrawerState = __toESM(require("./hooks/useDrawerState"));
var import_useDesignMode = __toESM(require("./hooks/useDesignMode"));
var import_useComponentId = __toESM(require("../hooks/useComponentId"));
var import_useTableQuery = __toESM(require("./hooks/useTableQuery"));
var import_useDataSource = __toESM(require("../hooks/useDataSource"));
var import_TableSettingProvider = __toESM(require("./provider/tableSetting/TableSettingProvider"));
var import_useDataSourceKey = __toESM(require("./hooks/useDataSourceKey"));
var import_useTableProps = __toESM(require("./hooks/useTableProps"));
var mockData = {
  count: 1,
  data: [{}],
  page: 1,
  pageSize: 10
};
var BaseTable = (props, ref) => {
  const {
    columns: propsColumns,
    dataSource,
    operationContent,
    currentSettingKey,
    ...others
  } = props;
  const tableRef = (0, import_react.useRef)(null);
  const componentId = (0, import_useComponentId.default)(props);
  const { registerValueVariable } = (0, import_useVariables.default)();
  const { list, __designMode } = (0, import_useDataSource.default)();
  const { data: originData } = list;
  const data = (0, import_react.useMemo)(() => {
    var _a;
    if (__designMode === "design" && !((_a = originData == null ? void 0 : originData.data) == null ? void 0 : _a.length)) {
      return mockData;
    }
    return originData;
  }, [originData, __designMode]);
  const formatCurrentSettingKey = (0, import_react.useMemo)(() => {
    return currentSettingKey || props.__id || "";
  }, [currentSettingKey]);
  const { dataSourceKey, dataSourceKeyRef } = (0, import_useDataSourceKey.default)();
  const beforeClose = (0, import_react.useRef)();
  const setBeforeClose = (0, import_react.useCallback)((fn) => {
    beforeClose.current = fn;
  }, []);
  const {
    drawerVisible,
    setDrawerVisible,
    openDrawer,
    closeDrawer,
    containerProps,
    Container,
    drawerContent
  } = (0, import_useDrawerState.default)(operationContent);
  const { queryPageConfig, handleValuesChange, refreshData, handleFilterChange, refreshCurrentPage, getInitialValues } = (0, import_useTableQuery.default)(props, formatCurrentSettingKey);
  const handleAllValuesChange = (changedValues, values) => {
    var _a;
    (_a = props == null ? void 0 : props.onValuesChange) == null ? void 0 : _a.call(props, changedValues, values);
    handleValuesChange(changedValues, values);
  };
  (0, import_useDesignMode.default)(props, {
    registerValueVariable,
    openDrawer,
    closeDrawer,
    data: data == null ? void 0 : data.data
  });
  (0, import_ahooks.useUpdateEffect)(() => {
    refreshData();
  }, [dataSourceKey]);
  (0, import_react.useEffect)(() => {
    if (dataSourceKeyRef.current) {
      if (dataSource == null ? void 0 : dataSource.fromSettingMeta) return;
      list == null ? void 0 : list.run(queryPageConfig);
    } else {
      list == null ? void 0 : list.mutate([]);
    }
  }, [queryPageConfig]);
  (0, import_react.useEffect)(() => {
    registerValueVariable == null ? void 0 : registerValueVariable(componentId, {
      currentDataSource: dataSource
    });
  }, [dataSource]);
  const handleCloseDrawer = async () => {
    if (beforeClose.current) {
      const canClose = await beforeClose.current();
      if (!canClose) {
        return;
      }
    }
    closeDrawer();
    registerValueVariable == null ? void 0 : registerValueVariable(componentId, {
      currentRecord: null
    });
  };
  const transformProps = (0, import_useTableProps.default)({
    data,
    originProps: props,
    openDrawer,
    closeDrawer,
    refreshData,
    handleFilterChange,
    tableRef,
    getInitialValues
  });
  (0, import_react.useImperativeHandle)(ref, () => {
    if (tableRef.current) {
      tableRef.current.dataSourceTable = {
        refreshCurrentPage,
        refreshData
      };
    }
    return tableRef.current;
  });
  return /* @__PURE__ */ import_react.default.createElement(import_TableSettingProvider.default, null, /* @__PURE__ */ import_react.default.createElement(
    import_table.default,
    {
      ref: tableRef,
      ...others,
      ...transformProps,
      currentSettingKey: formatCurrentSettingKey,
      dataSource: (data == null ? void 0 : data.data) || [],
      loading: list == null ? void 0 : list.loading,
      onValuesChange: handleAllValuesChange
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_ActionsProvider.default,
    {
      visible: drawerVisible,
      setVisible: setDrawerVisible,
      refreshTableData: refreshData,
      setBeforeClose,
      beforeClose: beforeClose.current
    },
    /* @__PURE__ */ import_react.default.createElement(
      Container,
      {
        ...containerProps,
        open: drawerVisible,
        onClose: handleCloseDrawer,
        onCancel: handleCloseDrawer,
        destroyOnClose: true
      },
      drawerContent
    )
  ));
};
var BaseTable_default = (0, import_react.forwardRef)(BaseTable);
