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/hooks/useTableSetting.ts
var useTableSetting_exports = {};
__export(useTableSetting_exports, {
  default: () => useTableSetting_default
});
module.exports = __toCommonJS(useTableSetting_exports);
var import_react = require("react");
var import_utils = require("../Table/utils");
var import_ahooks = require("ahooks");
var import_react2 = __toESM(require("react"));
var useTableSetting = (params) => {
  const {
    tableId,
    filter,
    columnSetting,
    dataSourceGroup,
    sort,
    view,
    currentViewMode,
    columns: propsColumns,
    gallery,
    currentSettingHash,
    exportImport,
    localSettings
  } = params;
  const columnsMap = (0, import_react.useMemo)(() => {
    return new Map(propsColumns.map((item) => [item.key, item]));
  }, [propsColumns]);
  const columns = (0, import_react.useMemo)(() => {
    var _a;
    if (currentViewMode === "grid") {
      return propsColumns;
    } else if (currentViewMode === "gallery") {
      return (((_a = gallery == null ? void 0 : gallery.content) == null ? void 0 : _a.fields) || []).filter((item) => item.show).map((item) => columnsMap.get(item.columnKey)).filter(Boolean);
    }
    return propsColumns;
  }, [propsColumns, currentViewMode, gallery, columnsMap]);
  const { list, quickFilterMaxLength, sortButtonShow } = filter || {};
  const defaultGalleryCoverSetting = (0, import_react.useMemo)(() => {
    var _a, _b;
    return {
      coverObjectFit: (_a = gallery == null ? void 0 : gallery.cover) == null ? void 0 : _a.objectFit,
      coverField: (_b = columns.find(
        (column) => {
          var _a2, _b2;
          return column.key === ((_b2 = (_a2 = gallery == null ? void 0 : gallery.cover) == null ? void 0 : _a2.fields) == null ? void 0 : _b2[0]);
        }
      )) == null ? void 0 : _b.dataIndex,
      compactMode: "default",
      isShowLabel: true
    };
  }, [gallery, columns]);
  const localStorageSettingKeyArr = (0, import_react.useMemo)(() => {
    var _a;
    const keys = (0, import_utils.getSettingKeyArrByMode)({
      filter,
      columnSetting,
      dataSourceGroup,
      sort,
      mode: "localStorage",
      currentViewMode
    });
    if ((localSettings == null ? void 0 : localSettings.enable) && ((_a = localSettings.scope) == null ? void 0 : _a.includes("filters"))) {
      if (!keys.includes("filters")) {
        keys.push("filters");
      }
    }
    return keys;
  }, [tableId, filter, columnSetting, dataSourceGroup, sort, currentViewMode]);
  const defaultKeyArr = (0, import_react.useMemo)(() => {
    return (0, import_utils.getSettingKeyArrByMode)({
      filter,
      columnSetting,
      dataSourceGroup,
      sort,
      mode: "",
      currentViewMode
    });
  }, [
    tableId,
    filter,
    columnSetting,
    dataSourceGroup,
    sort,
    view,
    currentViewMode
  ]);
  const tableLocalSetting = (0, import_react.useMemo)(() => {
    const setting = {};
    const allSetting = (0, import_utils.getTableSettingFromLocalStorage)(tableId);
    const currentViewModeSetting = (0, import_utils.getTableSettingByCurrentViewMode)(
      tableId,
      currentViewMode
    );
    const mergedSetting = { ...allSetting, ...currentViewModeSetting };
    localStorageSettingKeyArr.forEach((item) => {
      var _a, _b, _c, _d;
      if (item === "view_mode") {
        setting[item] = view.multiple ? (mergedSetting == null ? void 0 : mergedSetting[item]) || view.defaultViewMode : view.defaultViewMode;
      } else if (item === "filter_setting") {
        setting[item] = (0, import_utils.mergeFilterSetting)({
          list,
          quickFilterMaxLength,
          tableId,
          sortButtonShow,
          currentViewMode
        });
      } else if (item === "column_setting") {
        setting[item] = (0, import_utils.mergeColumnSetting)({
          columns,
          tableId,
          currentViewMode
        });
      } else if (item === "gallery_setting") {
        setting[item] = {
          ...mergedSetting == null ? void 0 : mergedSetting[item],
          // 本地没有默认封面效果时取配置中的
          coverObjectFit: ((_a = mergedSetting == null ? void 0 : mergedSetting[item]) == null ? void 0 : _a.coverObjectFit) || defaultGalleryCoverSetting.coverObjectFit,
          coverField: ((_b = mergedSetting == null ? void 0 : mergedSetting[item]) == null ? void 0 : _b.coverField) || defaultGalleryCoverSetting.coverField,
          compactMode: ((_c = mergedSetting == null ? void 0 : mergedSetting[item]) == null ? void 0 : _c.compactMode) || defaultGalleryCoverSetting.compactMode,
          isShowLabel: ((_d = mergedSetting == null ? void 0 : mergedSetting[item]) == null ? void 0 : _d.isShowLabel) ?? defaultGalleryCoverSetting.isShowLabel
        };
      } else if (item === "filters") {
        setting[item] = (mergedSetting == null ? void 0 : mergedSetting[item]) || {};
      } else {
        setting[item] = mergedSetting == null ? void 0 : mergedSetting[item];
      }
    });
    return setting;
  }, [
    localStorageSettingKeyArr,
    tableId,
    view,
    list,
    quickFilterMaxLength,
    tableId,
    sortButtonShow,
    columns,
    currentViewMode,
    gallery,
    defaultGalleryCoverSetting
  ]);
  const defaultTableSetting = (0, import_react.useMemo)(() => {
    const setting = {};
    defaultKeyArr.forEach((item) => {
      if (item === "column_setting") {
        setting[item] = (0, import_utils.mergeColumnSetting)({
          columns,
          tableId,
          currentViewMode
        });
      } else if (item === "filter_setting") {
        setting[item] = (0, import_utils.mergeFilterSetting)({
          list,
          quickFilterMaxLength,
          tableId: "",
          sortButtonShow,
          currentViewMode
        });
      } else if (item === "gallery_setting") {
        setting[item] = {
          // 取配置中的设置
          coverObjectFit: defaultGalleryCoverSetting.coverObjectFit,
          coverField: defaultGalleryCoverSetting.coverField,
          compactMode: defaultGalleryCoverSetting.compactMode,
          isShowLabel: defaultGalleryCoverSetting.isShowLabel
        };
      }
    });
    return setting;
  }, [
    defaultKeyArr,
    columns,
    defaultGalleryCoverSetting,
    columns,
    tableId,
    currentViewMode,
    list,
    quickFilterMaxLength,
    sortButtonShow
  ]);
  const setTableSetting = (0, import_ahooks.useMemoizedFn)((values) => {
    var _a;
    let valuesForSave = values;
    if ((localSettings == null ? void 0 : localSettings.enable) && ((_a = localSettings.scope) == null ? void 0 : _a.includes("filters"))) {
      const excluded = new Set((localSettings == null ? void 0 : localSettings.excludedFilterFields) || []);
      const filterValue = {};
      ((filter == null ? void 0 : filter.list) || []).forEach((item) => {
        const rawName = item == null ? void 0 : item.name;
        if (!rawName) return;
        const key = Array.isArray(rawName) ? rawName.join(",") : String(rawName);
        if (excluded.has(key)) return;
        if (values.hasOwnProperty(key)) {
          filterValue[key] = values[key];
        }
      });
      valuesForSave = {
        ...values,
        filters: filterValue
      };
    }
    (0, import_utils.setFormValuesToLocalStorage)({
      tableId,
      allValues: valuesForSave,
      keys: localStorageSettingKeyArr,
      currentViewMode,
      currentSettingHash
    });
  });
  const tableSetting = (0, import_react.useMemo)(() => {
    var _a;
    const merged = {
      ...tableLocalSetting,
      ...defaultTableSetting
    };
    if ((localSettings == null ? void 0 : localSettings.enable) && ((_a = localSettings.scope) == null ? void 0 : _a.includes("filters"))) {
      const persistedFilter = tableLocalSetting == null ? void 0 : tableLocalSetting.filters;
      if (persistedFilter && typeof persistedFilter === "object") {
        return {
          ...merged,
          ...persistedFilter
        };
      }
    }
    return merged;
  }, [tableLocalSetting, defaultTableSetting]);
  (0, import_react.useMemo)(() => {
    var _a;
    try {
      if (!tableId) return;
      const current = (0, import_utils.getTableSettingByCurrentViewMode)(tableId, currentViewMode) || {};
      const persistedFilters = (current == null ? void 0 : current.filters) || {};
      let nextFilters = { ...persistedFilters };
      let shouldUpdate = false;
      const enableFiltersMemory = !!((localSettings == null ? void 0 : localSettings.enable) && ((_a = localSettings.scope) == null ? void 0 : _a.includes("filters")));
      if (!enableFiltersMemory) {
        if (Object.keys(persistedFilters || {}).length > 0) {
          nextFilters = {};
          shouldUpdate = true;
        }
      } else {
        let names = /* @__PURE__ */ new Set();
        if (import_react2.default.isValidElement(filter) || import_react2.default.isValidElement(filter == null ? void 0 : filter.dom)) {
          names = new Set(
            ((filter == null ? void 0 : filter.items) || []).filter((item) => !item.isHidden).map((item) => {
              const rawName = item == null ? void 0 : item.value;
              if (!rawName) return void 0;
              return Array.isArray(rawName) ? rawName.join(",") : String(rawName);
            })
          );
        } else {
          names = new Set(
            ((filter == null ? void 0 : filter.list) || []).map((item) => {
              const rawName = item == null ? void 0 : item.name;
              if (!rawName) return void 0;
              return Array.isArray(rawName) ? rawName.join(",") : String(rawName);
            }).filter(Boolean)
          );
        }
        const excluded = new Set((localSettings == null ? void 0 : localSettings.excludedFilterFields) || []);
        const allowed = new Set(Array.from(names).filter((k) => !excluded.has(k)));
        const filtered = {};
        Object.keys(persistedFilters || {}).forEach((k) => {
          if (allowed.has(k)) {
            filtered[k] = persistedFilters[k];
          } else {
            shouldUpdate = true;
          }
        });
        if (Object.keys(filtered).length !== Object.keys(persistedFilters || {}).length) {
          shouldUpdate = true;
        }
        nextFilters = filtered;
      }
      if (shouldUpdate) {
        (0, import_utils.setTableSettingToLocalStorage)({
          tableId,
          tableSettingObj: { filters: nextFilters },
          currentViewMode,
          currentSettingHash
        });
      }
    } catch (e) {
    }
  }, []);
  return {
    setTableSetting,
    tableSetting
  };
};
var useTableSetting_default = useTableSetting;
