function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useCallback } from 'react';
import { Button } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
import { locales as utilsLocales } from '@pisell/utils';
import { useRecordBoardContext } from "../../hooks/useRecordBoardContext";
import { getText } from "../../../../locales";
import "./index.less";

/** 与 Quick Filter 同源：优先 utils，若 utils 返回 key（未命中）则用 materials 再回退 */
function getResetButtonText() {
  var key = 'pisell2.recordBoard.toolbar.reset';
  var u = utilsLocales.getText(key);
  return u && u !== key ? u : getText(key) || getText('record-board-toolbar-reset');
}

/** 筛选在 searchParams 中的字段名，与 ToolBar 一致 */
var FILTER_PARAMS_FIELD = 'filter';
/** 排序在 searchParams 中的字段名，与 ToolBar 一致 */
var SORT_PARAMS_FIELD = 'sort';
/**
 * RecordBoard 工具栏重置按钮：清空筛选与排序并刷新表格。
 * 点击后以空 filter.values、清空 sort 调用 onSearch，并调用 onReset（若提供）。
 */
var ToolBarReset = function ToolBarReset(_ref) {
  var _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'large' : _ref$size,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'default' : _ref$type,
    children = _ref.children;
  var defaultResetText = getResetButtonText();
  var _useRecordBoardContex = useRecordBoardContext(),
    searchParams = _useRecordBoardContex.searchParams,
    onSearch = _useRecordBoardContex.onSearch,
    onReset = _useRecordBoardContex.onReset,
    setFilterResetKey = _useRecordBoardContex.setFilterResetKey;
  var handleReset = useCallback(function () {
    var _currentFilter$otherF;
    setFilterResetKey === null || setFilterResetKey === void 0 || setFilterResetKey(function (prev) {
      return prev + 1;
    });
    var currentFilter = searchParams === null || searchParams === void 0 ? void 0 : searchParams[FILTER_PARAMS_FIELD];
    var nextParams = _objectSpread(_objectSpread({}, searchParams), {}, _defineProperty(_defineProperty({}, FILTER_PARAMS_FIELD, _objectSpread(_objectSpread({}, currentFilter), {}, {
      quickFilter: [],
      otherFilter: (_currentFilter$otherF = currentFilter === null || currentFilter === void 0 ? void 0 : currentFilter.otherFilter) !== null && _currentFilter$otherF !== void 0 ? _currentFilter$otherF : [],
      values: {}
    })), SORT_PARAMS_FIELD, undefined));
    onSearch(nextParams);
    onReset === null || onReset === void 0 || onReset();
  }, [searchParams, onSearch, onReset, setFilterResetKey]);
  return /*#__PURE__*/React.createElement(Button, {
    className: "record-board-toolbar-reset",
    type: type,
    icon: /*#__PURE__*/React.createElement(ReloadOutlined, null),
    onClick: handleReset,
    size: size
  }, children !== null && children !== void 0 ? children : defaultResetText);
};
ToolBarReset.displayName = 'RecordBoard.ToolBarReset';
export default ToolBarReset;