function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { useMemo } from "react";
import { getSettingKeyArrByMode, getTableSettingByCurrentViewMode, getTableSettingFromLocalStorage, mergeColumnSetting, mergeFilterSetting, setFormValuesToLocalStorage } from "../Table/utils";
import { useMemoizedFn } from "ahooks";
var useTableSetting = function useTableSetting(params) {
  var tableId = params.tableId,
    filter = params.filter,
    columnSetting = params.columnSetting,
    dataSourceGroup = params.dataSourceGroup,
    sort = params.sort,
    view = params.view,
    currentViewMode = params.currentViewMode,
    propsColumns = params.columns,
    gallery = params.gallery,
    currentSettingHash = params.currentSettingHash,
    exportImport = params.exportImport;
  var columnsMap = useMemo(function () {
    return new Map(propsColumns.map(function (item) {
      return [item.key, item];
    }));
  }, [propsColumns]);

  // 获取当前视图下的columns配置
  var columns = useMemo(function () {
    if (currentViewMode === "grid") {
      return propsColumns;
    } else if (currentViewMode === "gallery") {
      var _gallery$content;
      return ((gallery === null || gallery === void 0 ? void 0 : (_gallery$content = gallery.content) === null || _gallery$content === void 0 ? void 0 : _gallery$content.fields) || []).filter(function (item) {
        return item.show;
      }).map(function (item) {
        return columnsMap.get(item.columnKey);
      }).filter(Boolean);
    }
    return propsColumns;
  }, [propsColumns, currentViewMode, gallery, columnsMap]);
  var _ref = filter || {},
    list = _ref.list,
    quickFilterMaxLength = _ref.quickFilterMaxLength,
    sortButtonShow = _ref.sortButtonShow;
  var defaultGalleryCoverSetting = useMemo(function () {
    var _gallery$cover, _columns$find;
    return {
      coverObjectFit: gallery === null || gallery === void 0 ? void 0 : (_gallery$cover = gallery.cover) === null || _gallery$cover === void 0 ? void 0 : _gallery$cover.objectFit,
      coverField: (_columns$find = columns.find(function (column) {
        var _gallery$cover2, _gallery$cover2$field;
        return column.key === (gallery === null || gallery === void 0 ? void 0 : (_gallery$cover2 = gallery.cover) === null || _gallery$cover2 === void 0 ? void 0 : (_gallery$cover2$field = _gallery$cover2.fields) === null || _gallery$cover2$field === void 0 ? void 0 : _gallery$cover2$field[0]);
      })) === null || _columns$find === void 0 ? void 0 : _columns$find.dataIndex,
      compactMode: "default",
      isShowLabel: true
    };
  }, [gallery, columns]);

  /**
   * 获取需要本地存储的 keyArr
   */
  var localStorageSettingKeyArr = useMemo(function () {
    return getSettingKeyArrByMode({
      filter: filter,
      columnSetting: columnSetting,
      dataSourceGroup: dataSourceGroup,
      sort: sort,
      mode: "localStorage",
      currentViewMode: currentViewMode
    });
  }, [tableId, filter, columnSetting, dataSourceGroup, sort, currentViewMode]);
  var defaultKeyArr = useMemo(function () {
    return getSettingKeyArrByMode({
      filter: filter,
      columnSetting: columnSetting,
      dataSourceGroup: dataSourceGroup,
      sort: sort,
      mode: "",
      currentViewMode: currentViewMode
    });
  }, [tableId, filter, columnSetting, dataSourceGroup, sort, view, currentViewMode]);

  /**
   * 获取本地存储的 tableSetting 用于刷新后回显
   */
  var tableLocalSetting = useMemo(function () {
    var setting = {};
    /** 当前组件下的所有配置 */
    var allSetting = getTableSettingFromLocalStorage(tableId);
    /** 当前视图下的配置 */
    var currentViewModeSetting = getTableSettingByCurrentViewMode(tableId, currentViewMode);
    var mergedSetting = _objectSpread(_objectSpread({}, allSetting), currentViewModeSetting);
    localStorageSettingKeyArr.forEach(function (item) {
      if (item === "view_mode") {
        /** 多视图模式当前视图从本地存储取， 单视图模式直接取默认视图 */
        setting[item] = view.multiple ? (mergedSetting === null || mergedSetting === void 0 ? void 0 : mergedSetting[item]) || view.defaultViewMode : view.defaultViewMode;
      } else if (item === "filter_setting") {
        setting[item] = mergeFilterSetting({
          list: list,
          quickFilterMaxLength: quickFilterMaxLength,
          tableId: tableId,
          sortButtonShow: sortButtonShow,
          currentViewMode: currentViewMode
        });
      } else if (item === "column_setting") {
        setting[item] = mergeColumnSetting({
          columns: columns,
          tableId: tableId,
          currentViewMode: currentViewMode
        });
      } else if (item === "gallery_setting") {
        var _mergedSetting$item, _mergedSetting$item2, _mergedSetting$item3, _mergedSetting$item$i, _mergedSetting$item4;
        setting[item] = _objectSpread(_objectSpread({}, mergedSetting === null || mergedSetting === void 0 ? void 0 : mergedSetting[item]), {}, {
          // 本地没有默认封面效果时取配置中的
          coverObjectFit: (mergedSetting === null || mergedSetting === void 0 ? void 0 : (_mergedSetting$item = mergedSetting[item]) === null || _mergedSetting$item === void 0 ? void 0 : _mergedSetting$item.coverObjectFit) || defaultGalleryCoverSetting.coverObjectFit,
          coverField: (mergedSetting === null || mergedSetting === void 0 ? void 0 : (_mergedSetting$item2 = mergedSetting[item]) === null || _mergedSetting$item2 === void 0 ? void 0 : _mergedSetting$item2.coverField) || defaultGalleryCoverSetting.coverField,
          compactMode: (mergedSetting === null || mergedSetting === void 0 ? void 0 : (_mergedSetting$item3 = mergedSetting[item]) === null || _mergedSetting$item3 === void 0 ? void 0 : _mergedSetting$item3.compactMode) || defaultGalleryCoverSetting.compactMode,
          isShowLabel: (_mergedSetting$item$i = mergedSetting === null || mergedSetting === void 0 ? void 0 : (_mergedSetting$item4 = mergedSetting[item]) === null || _mergedSetting$item4 === void 0 ? void 0 : _mergedSetting$item4.isShowLabel) !== null && _mergedSetting$item$i !== void 0 ? _mergedSetting$item$i : defaultGalleryCoverSetting.isShowLabel
        });
      } else {
        setting[item] = mergedSetting === null || mergedSetting === void 0 ? void 0 : mergedSetting[item];
      }
    });
    return setting;
  }, [localStorageSettingKeyArr, tableId, view, list, quickFilterMaxLength, tableId, sortButtonShow, columns, currentViewMode, gallery, defaultGalleryCoverSetting]);
  var defaultTableSetting = useMemo(function () {
    var setting = {};
    defaultKeyArr.forEach(function (item) {
      if (item === "column_setting") {
        setting[item] = mergeColumnSetting({
          columns: columns,
          tableId: tableId,
          currentViewMode: currentViewMode
        });
      } else if (item === "filter_setting") {
        setting[item] = mergeFilterSetting({
          list: list,
          quickFilterMaxLength: quickFilterMaxLength,
          tableId: "",
          sortButtonShow: sortButtonShow,
          currentViewMode: 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]);

  /**
   * 更新 tableSetting
   * @param values
   */
  var setTableSetting = useMemoizedFn(function (values) {
    setFormValuesToLocalStorage({
      tableId: tableId,
      allValues: values,
      keys: localStorageSettingKeyArr,
      currentViewMode: currentViewMode,
      currentSettingHash: currentSettingHash
    });
  });
  var tableSetting = useMemo(function () {
    return _objectSpread(_objectSpread({}, tableLocalSetting), defaultTableSetting);
  }, [tableLocalSetting, defaultTableSetting]);
  return {
    setTableSetting: setTableSetting,
    tableSetting: tableSetting
  };
};
export default useTableSetting;