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/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 useTableSetting = (params) => {
  const {
    tableId,
    filter,
    columnSetting,
    dataSourceGroup,
    sort,
    view,
    currentViewMode,
    columns: propsColumns,
    gallery,
    currentSettingHash,
    exportImport
  } = 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)(() => {
    return (0, import_utils.getSettingKeyArrByMode)({
      filter,
      columnSetting,
      dataSourceGroup,
      sort,
      mode: "localStorage",
      currentViewMode
    });
  }, [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 {
        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) => {
    (0, import_utils.setFormValuesToLocalStorage)({
      tableId,
      allValues: values,
      keys: localStorageSettingKeyArr,
      currentViewMode,
      currentSettingHash
    });
  });
  const tableSetting = (0, import_react.useMemo)(() => {
    return {
      ...tableLocalSetting,
      ...defaultTableSetting
    };
  }, [tableLocalSetting, defaultTableSetting]);
  return {
    setTableSetting,
    tableSetting
  };
};
var useTableSetting_default = useTableSetting;
