var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/dataSourceTable/hooks/useTableQuery.ts
var useTableQuery_exports = {};
__export(useTableQuery_exports, {
  default: () => useTableQuery_default
});
module.exports = __toCommonJS(useTableQuery_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var useTableQuery = (props) => {
  var _a, _b, _c, _d;
  const { filterBy, sortBy, tabs } = props;
  const [queryPageConfig, setQueryPageConfig] = (0, import_react.useState)({
    page: 1,
    pageSize: 10,
    filter: ((_b = (_a = tabs == null ? void 0 : tabs.items) == null ? void 0 : _a[0]) == null ? void 0 : _b.filterBy) || filterBy || {},
    sort: ((_d = (_c = tabs == null ? void 0 : tabs.items) == null ? void 0 : _c[0]) == null ? void 0 : _d.sortBy) || sortBy
  });
  const handleValuesChange = (0, import_ahooks.useMemoizedFn)(
    (params, allParams) => {
      const { page, size } = params.pagination || {};
      setQueryPageConfig((prev) => {
        var _a2, _b2, _c2;
        let sort = prev.sort || sortBy;
        let filter = prev.filter;
        if ("tabs" in params) {
          sort = ((_a2 = params.tabs) == null ? void 0 : _a2.sortBy) || sortBy || "";
          filter = ((_b2 = params.tabs) == null ? void 0 : _b2.filterBy) || {};
        }
        if ("order_by" in params) {
          sort = params.order_by || ((_c2 = allParams.tabs) == null ? void 0 : _c2.sortBy) || sortBy;
        }
        return {
          ...prev,
          page: size !== prev.pageSize ? 1 : page ?? prev.page,
          pageSize: size ?? prev.pageSize,
          sort,
          filter
        };
      });
    }
  );
  const handleFilterChange = (0, import_ahooks.useMemoizedFn)(
    (changedFields, allFields) => {
      setQueryPageConfig((prev) => ({
        ...prev,
        page: 1,
        filter: { ...allFields, ...filterBy || {} }
      }));
    }
  );
  const refreshData = (0, import_ahooks.useMemoizedFn)(() => {
    setQueryPageConfig((prev) => ({
      ...prev,
      page: 1
    }));
  });
  return {
    queryPageConfig,
    setQueryPageConfig,
    handleValuesChange,
    refreshData,
    handleFilterChange
  };
};
var useTableQuery_default = useTableQuery;
