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/pisellDataSourceContainer/hooks/useHandleAction.ts
var useHandleAction_exports = {};
__export(useHandleAction_exports, {
  default: () => useHandleAction_default
});
module.exports = __toCommonJS(useHandleAction_exports);
var import_react = require("react");
var import_useDataSource = __toESM(require("../../dataSourceComponents/hooks/useDataSource"));
var import_formatData = require("../utils/formatData");
var defaultPagination = {
  pageNumber: 1,
  pageSize: 10
};
var useHandleAction = (initialParams = {}, options, form) => {
  const { list, get, update, create, destroy, getCurrentRecord } = (0, import_useDataSource.default)();
  const normalizedOptions = options === true ? defaultPagination : typeof options === "object" ? options : {};
  const { pageNumber, pageSize } = normalizedOptions;
  const enablePagination = pageNumber !== void 0 && pageSize !== void 0;
  const initialParamsRef = (0, import_react.useRef)(initialParams);
  const [pagination, setPagination] = (0, import_react.useState)({
    pageNumber: pageNumber ?? 1,
    pageSize: pageSize ?? 10
  });
  const { run, params, data: listData } = list;
  const onSearch = (0, import_react.useCallback)(
    (newParams) => {
      var _a;
      let searchParams = {
        ...(params == null ? void 0 : params[0]) || {},
        ...((_a = initialParamsRef.current) == null ? void 0 : _a.list) || {},
        ...newParams
      };
      if (enablePagination) {
        searchParams = {
          ...searchParams,
          pageNumber: 1,
          // 查询时始终重置到第一页
          pageSize: pagination.pageSize
        };
        if (pagination.pageNumber !== 1) {
          setPagination((prev) => ({ ...prev, pageNumber: 1 }));
        }
      }
      run(searchParams);
    },
    [enablePagination, pagination.pageNumber, pagination.pageSize, params, run]
  );
  const onReset = (0, import_react.useCallback)(() => {
    var _a;
    const formValues = (form == null ? void 0 : form.getFieldsValue()) || {};
    let resetParams = {
      ...((_a = initialParamsRef.current) == null ? void 0 : _a.list) || {},
      ...formValues
    };
    if (enablePagination) {
      setPagination({
        pageNumber: pageNumber ?? 1,
        pageSize: pageSize ?? 10
      });
      resetParams = {
        ...resetParams,
        pageNumber: pageNumber ?? 1,
        pageSize: pageSize ?? 10
      };
    }
    run(resetParams);
  }, [run, enablePagination, pagination, pageNumber, pageSize]);
  const onPageChange = (0, import_react.useCallback)(
    (pageNumber2, pageSize2) => {
      var _a;
      if (!enablePagination) {
        console.warn("分页功能未启用");
        return;
      }
      if (pageNumber2 < 1 || pageSize2 < 1) return;
      setPagination({
        pageNumber: pageNumber2,
        pageSize: pageSize2
      });
      const lastParams = (params == null ? void 0 : params[0]) || { ...((_a = initialParamsRef.current) == null ? void 0 : _a.list) || {} };
      run({
        ...lastParams,
        pageNumber: pageNumber2,
        pageSize: pageSize2
      });
    },
    [enablePagination, params, run]
  );
  return (0, import_react.useMemo)(() => {
    return {
      list: {
        ...list,
        data: (0, import_formatData.formatListResult)(listData),
        onSearch,
        onReset,
        onPageChange,
        ...enablePagination ? { pagination } : {}
      },
      get,
      update,
      create,
      destroy,
      getCurrentRecord
    };
  }, [list, get, update, create, destroy, getCurrentRecord, pagination]);
};
var useHandleAction_default = useHandleAction;
