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) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import cloneDeepLodash from 'lodash.clonedeep';
// 所有 ProTable 组件公用一套组件内事件转发机制
var tableCallback = {};
// 组件内对全局状态的更改
var getMutations = function getMutations(actionRef) {
  var _actionRef$current2, _actionRef$current2$g, _actionRef$current2$g2;
  return {
    setFullScreenState: function setFullScreenState(fullScreenState) {
      var _actionRef$current, _actionRef$current$se;
      (_actionRef$current = actionRef.current) === null || _actionRef$current === void 0 ? void 0 : (_actionRef$current$se = _actionRef$current.setState) === null || _actionRef$current$se === void 0 ? void 0 : _actionRef$current$se.call(_actionRef$current, 'fullScreenState', fullScreenState);
    },
    filterColumns: (_actionRef$current2 = actionRef.current) === null || _actionRef$current2 === void 0 ? void 0 : (_actionRef$current2$g = _actionRef$current2.getState) === null || _actionRef$current2$g === void 0 ? void 0 : (_actionRef$current2$g2 = _actionRef$current2$g.call(_actionRef$current2)) === null || _actionRef$current2$g2 === void 0 ? void 0 : _actionRef$current2$g2.filterColumns,
    /**
     * 设置筛选列规则
     * @param rules 规则
     * @param dataIndex column.dataIndex
     */
    setFilterRules: function setFilterRules(rules) {
      var _actionRef$current3, _actionRef$current3$s;
      (_actionRef$current3 = actionRef.current) === null || _actionRef$current3 === void 0 ? void 0 : (_actionRef$current3$s = _actionRef$current3.setState) === null || _actionRef$current3$s === void 0 ? void 0 : _actionRef$current3$s.call(_actionRef$current3, 'filterRules', rules);
    },
    /**
     * 获取筛选规则。为params
     */
    getFilterRules: function getFilterRules() {
      var _actionRef$current4, _actionRef$current4$g;
      var result = {};
      var filterRules = (_actionRef$current4 = actionRef.current) === null || _actionRef$current4 === void 0 ? void 0 : (_actionRef$current4$g = _actionRef$current4.getState) === null || _actionRef$current4$g === void 0 ? void 0 : _actionRef$current4$g.call(_actionRef$current4).filterRules;
      if (filterRules) {
        result = Object.entries(filterRules).map(function (_ref) {
          var _ref2 = _slicedToArray(_ref, 2),
            k = _ref2[0],
            v = _ref2[1];
          return _defineProperty({}, k, v.params);
        }).reduce(function (acc, cur) {
          acc = _objectSpread(_objectSpread({}, acc), cur);
          return acc;
        }, {});
      }
      return result;
    }
  };
};
// 调用绑定的监听函数
export function actionRefUseStateOn(callback, state) {
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
    args[_key - 2] = arguments[_key];
  }
  if (callback[state]) {
    Object.entries(callback[state]).forEach(function (_ref4) {
      var _ref5 = _slicedToArray(_ref4, 2),
        k = _ref5[0],
        v = _ref5[1];
      if (v) {
        v === null || v === void 0 ? void 0 : v.apply(void 0, args);
      }
    });
  }
}
/** table 组件内监听 不对外暴露 */
export function on(fun, key) {
  tableCallback[key] = fun;
}
/** table 组件内销毁监听 不对外暴露 */
export function off(key) {
  if (tableCallback[key]) {
    delete tableCallback[key];
  }
}
/** table 组件内触发事件 不对外暴露 */
export function emit(key) {
  var fun = tableCallback[key];
  if (fun) {
    for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
      args[_key2 - 1] = arguments[_key2];
    }
    fun.apply(void 0, args);
  }
}
export function initActionRef(ref, action) {
  var userAction = _objectSpread(_objectSpread({}, action), getMutations(ref));
  ref.current = userAction;
}
export function useActionType(ref, action) {
  ref.current = _objectSpread(_objectSpread({}, ref.current), action);
  return _objectSpread(_objectSpread({}, ref.current), action);
}
export function cloneDeep(obj) {
  return cloneDeepLodash(obj);
}
/**
 * 处理默认过滤参数
 * @param columns
 * @param ref
 */
export function processDefaultFilter(columns, ref, defaultFilterParams) {
  columns.forEach(function (column) {
    var _column$dataIndex$toS, _column$dataIndex;
    var defaultRules = defaultFilterParams === null || defaultFilterParams === void 0 ? void 0 : defaultFilterParams[(_column$dataIndex$toS = column === null || column === void 0 ? void 0 : (_column$dataIndex = column.dataIndex) === null || _column$dataIndex === void 0 ? void 0 : _column$dataIndex.toString()) !== null && _column$dataIndex$toS !== void 0 ? _column$dataIndex$toS : ''];
    if (defaultRules) {
      var _ref$current$getState, _ref$current, _ref$current$getState2, _ref$current$getState3, _ref$current2, _ref$current2$setFilt;
      var params = '';
      var rules = [];
      if (Array.isArray(defaultRules)) {
        params = defaultRules.join(',');
        rules = defaultRules;
      } else {
        params = defaultRules;
        rules = [defaultRules];
      }
      // 需要带上其他所有的信息
      var otherRules = (_ref$current$getState = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : (_ref$current$getState2 = _ref$current.getState) === null || _ref$current$getState2 === void 0 ? void 0 : (_ref$current$getState3 = _ref$current$getState2.call(_ref$current)) === null || _ref$current$getState3 === void 0 ? void 0 : _ref$current$getState3.filterRules) !== null && _ref$current$getState !== void 0 ? _ref$current$getState : {};
      // 再设置所有的列筛选状态
      (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : (_ref$current2$setFilt = _ref$current2.setFilterRules) === null || _ref$current2$setFilt === void 0 ? void 0 : _ref$current2$setFilt.call(_ref$current2, _objectSpread(_objectSpread({}, otherRules), {}, _defineProperty({}, column.dataIndex, {
        rules: rules,
        params: params
      })));
    }
  });
}