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 import_utils = require("../../../table/Table/utils");
var useTableQuery = (props, currentSettingHash) => {
  var _a, _b, _c, _d;
  const { filterBy, sortBy, tabs, localSettings } = props;
  const tableId = (0, import_react.useMemo)(() => {
    if (props.__designMode === "design") {
      return "designMode-mock-table-id";
    }
    return (props == null ? void 0 : props.currentComponentId) || props.__id;
  }, [props.__id, props.__designMode, props == null ? void 0 : props.currentComponentId]);
  const currentViewMode = (0, import_react.useMemo)(() => (0, import_utils.getCurrentViewModeFromLocalStorage)(tableId) || "grid", [tableId]);
  const [queryPageConfig, setQueryPageConfig] = (0, import_react.useState)({
    page: 1,
    pageSize: 20,
    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 getInitialValues = (0, import_ahooks.useMemoizedFn)(() => {
    try {
      if (!tableId) return {};
      const setting = (0, import_utils.getTableSettingByCurrentViewMode)(tableId, currentViewMode);
      console.log("setting22", setting);
      return (setting == null ? void 0 : setting.filters) || {};
    } catch (e) {
      return {};
    }
  });
  (0, import_react.useEffect)(() => {
    var _a2;
    if (!tableId) return;
    const enable = !!((localSettings == null ? void 0 : localSettings.enable) && ((_a2 = localSettings.scope) == null ? void 0 : _a2.includes("filters")));
    if (!enable) return;
    const setting = (0, import_utils.getTableSettingByCurrentViewMode)(tableId, currentViewMode);
    const persisted = (setting == null ? void 0 : setting.filters) || {};
    if (persisted && Object.keys(persisted).length > 0) {
      setQueryPageConfig((prev) => ({
        ...prev,
        filter: { ...prev.filter, ...persisted }
      }));
    }
  }, [tableId, currentViewMode, JSON.stringify(localSettings)]);
  const handleValuesChange = (0, import_ahooks.useMemoizedFn)(
    (params, allParams) => {
      var _a2;
      const { page, size } = params.pagination || {};
      let nextFilterForSave = null;
      setQueryPageConfig((prev) => {
        var _a3, _b2, _c2;
        let sort = prev.sort || sortBy;
        let filter = prev.filter;
        if ("tabs" in params) {
          sort = ((_a3 = params.tabs) == null ? void 0 : _a3.sortBy) || sortBy || "";
          const tabsFilter = ((_b2 = params.tabs) == null ? void 0 : _b2.filterBy) || {};
          filter = { ...prev.filter, ...tabsFilter };
          nextFilterForSave = filter;
        }
        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 enable = !!((localSettings == null ? void 0 : localSettings.enable) && ((_a2 = localSettings.scope) == null ? void 0 : _a2.includes("filters")));
      if (enable && tableId && nextFilterForSave) {
        const excluded = new Set((localSettings == null ? void 0 : localSettings.excludedFilterFields) || []);
        const filtered = {};
        Object.keys(nextFilterForSave).forEach((k) => {
          if (!excluded.has(k)) {
            filtered[k] = nextFilterForSave[k];
          }
        });
        (0, import_utils.setTableSettingToLocalStorage)({
          tableId,
          tableSettingObj: { filters: filtered },
          currentViewMode,
          currentSettingHash
        });
      }
    }
  );
  const handleFilterChange = (0, import_ahooks.useMemoizedFn)(
    (changedFields, allFields) => {
      var _a2;
      const nextFilter = { ...allFields };
      setQueryPageConfig((prev) => ({
        ...prev,
        page: 1,
        filter: { ...prev.filter, ...nextFilter, ...filterBy || {} }
      }));
      const enable = !!((localSettings == null ? void 0 : localSettings.enable) && ((_a2 = localSettings.scope) == null ? void 0 : _a2.includes("filters")));
      if (enable && tableId) {
        const excluded = new Set((localSettings == null ? void 0 : localSettings.excludedFilterFields) || []);
        const filtered = {};
        Object.keys(nextFilter || {}).forEach((k) => {
          if (!excluded.has(k)) {
            filtered[k] = nextFilter[k];
          }
        });
        (0, import_utils.setTableSettingToLocalStorage)({
          tableId,
          tableSettingObj: { filters: filtered },
          currentViewMode,
          currentSettingHash
        });
      }
    }
  );
  const refreshData = (0, import_ahooks.useMemoizedFn)(() => {
    setQueryPageConfig((prev) => ({
      ...prev,
      page: 1
    }));
  });
  const refreshCurrentPage = (0, import_ahooks.useMemoizedFn)(() => {
    setQueryPageConfig((prev) => ({
      ...prev
    }));
  });
  return {
    queryPageConfig,
    setQueryPageConfig,
    handleValuesChange,
    refreshData,
    handleFilterChange,
    refreshCurrentPage,
    getInitialValues
  };
};
var useTableQuery_default = useTableQuery;
