import { useCallback, useRef } from "react";
import { getSettingKeyArrByMode } from "../Table/utils";
/**
 * 判断是否触发值变化
 * @param params 参数对象，包含 filter、columnSetting、dataSourceGroup、sort
 * @returns 返回一个函数，用于判断是否触发值变化
 */
var useTriggerValuesChange = function useTriggerValuesChange(params) {
  var filter = params.filter,
    columnSetting = params.columnSetting,
    dataSourceGroup = params.dataSourceGroup,
    sort = params.sort,
    currentViewMode = params.currentViewMode;
  var _ref = filter || {},
    list = _ref.list,
    show = _ref.show;
  var _ref2 = sort || {},
    sortList = _ref2.list,
    sortShow = _ref2.show;
  var lastIsRemoteSort = useRef(false);
  var getIsTriggerValuesChange = useCallback(function (changedValues, values) {
    var keys = getSettingKeyArrByMode({
      filter: filter,
      columnSetting: columnSetting,
      dataSourceGroup: dataSourceGroup,
      sort: sort,
      mode: "remote",
      currentViewMode: currentViewMode
    });
    var changedKey = Object.keys(changedValues)[0];
    if (changedKey === "pagination") {
      return true;
    }
    if (changedKey === "view_mode") {
      return false;
    }
    if (changedKey === "tabs") {
      return true;
    }

    /**
     * 排序变化时，如果排序有远程接口排序，则触发值变化
     */
    if (changedKey === "order_by") {
      if (sortShow && sortList !== null && sortList !== void 0 && sortList.length) {
        var _value$split, _value$split$call;
        var value = changedValues[changedKey];
        var sortField = (value === null || value === void 0 ? void 0 : (_value$split = value.split) === null || _value$split === void 0 ? void 0 : (_value$split$call = _value$split.call(value, ":")) === null || _value$split$call === void 0 ? void 0 : _value$split$call[0]) || "";
        var remoteSortKeys = sortList.filter(function (item) {
          return !item.localFilter;
        }).map(function (item) {
          var _item$name;
          return (_item$name = item.name) === null || _item$name === void 0 ? void 0 : _item$name.toString();
        });
        var result = remoteSortKeys.includes(sortField);
        // sort值清空时 如果上次更新是远程排序 那么触发onValuesChange
        if (sortField === "" && lastIsRemoteSort.current) {
          lastIsRemoteSort.current = false;
          return true;
        }
        // sortField有值时 存入当前是否远程状态
        if (sortField !== "") {
          lastIsRemoteSort.current = result;
        }
        return result;
      }
    }

    /**
     * 过滤器变化时，如果过滤器有远程过滤，则触发值变化
     */
    if (changedKey === "filters") {
      var _Object$keys;
      var filterKey = ((_Object$keys = Object.keys(changedValues.filters)) === null || _Object$keys === void 0 ? void 0 : _Object$keys[0]) || "";
      if (show && list !== null && list !== void 0 && list.length) {
        var remoteFilterKeys = list.filter(function (item) {
          return !item.localFilter;
        }).map(function (item) {
          var _item$name2;
          return (_item$name2 = item.name) === null || _item$name2 === void 0 ? void 0 : _item$name2.toString();
        });
        return remoteFilterKeys.includes(filterKey);
      }
    }

    /**
     * 表格配置变化时，如果表格配置有远程处理，则触发值变化
     */
    if (keys.includes(changedKey)) {
      return true;
    }
  }, [filter, columnSetting, dataSourceGroup, sort]);
  return getIsTriggerValuesChange;
};
export default useTriggerValuesChange;