"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.exec.js");
var _react = _interopRequireWildcard(require("react"));
var _icons = require("@ant-design/icons");
var _tooltip = _interopRequireDefault(require("../tooltip"));
var _button = _interopRequireDefault(require("../button"));
var _wauProvider = require("../wau-provider");
var _ListToolBar = _interopRequireDefault(require("./ListToolBar"));
var _ColumnSetting = _interopRequireDefault(require("./ColumnSetting"));
var _FullscreenIcon = _interopRequireDefault(require("./FullscreenIcon"));
var _DensityIcon = _interopRequireDefault(require("./DensityIcon"));
var _container = _interopRequireDefault(require("./container"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function getButtonText({
  intl
}) {
  return {
    reload: {
      text: intl.getMessage('tableToolBar.reload', '刷新'),
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.default, {
        icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.ReloadOutlined, {})
      })
    },
    density: {
      text: intl.getMessage('tableToolBar.density', '表格密度'),
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_DensityIcon.default, {})
    },
    setting: {
      text: intl.getMessage('tableToolBar.columnSetting', '列设置'),
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.SettingOutlined, {})
    },
    fullScreen: {
      text: intl.getMessage('tableToolBar.fullScreen', '全屏'),
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FullscreenIcon.default, {})
    }
  };
}

/**
 * 渲染默认的 工具栏
 *
 * @param options
 * @param className
 */
function renderDefaultOption(options, defaultOptions, columns) {
  return Object.keys(options).filter(item => item).map(key => {
    const value = options[key];
    if (!value) {
      return null;
    }
    if (key === 'setting') {
      return /*#__PURE__*/(0, _react.createElement)(_ColumnSetting.default, {
        ...options.setting,
        columns: columns,
        key: key
      });
    }
    if (key === 'fullScreen') {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        onClick: value === true ? defaultOptions[key] : value,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FullscreenIcon.default, {})
      }, key);
    }
    const optionItem = getButtonText(defaultOptions)[key];
    if (optionItem) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        onClick: () => {
          if (value && defaultOptions[key] !== true) {
            if (value !== true) {
              value();
              return;
            }
            defaultOptions[key]();
          }
        },
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.default, {
          title: optionItem.text,
          children: optionItem.icon
        })
      }, key);
    }
    return null;
  }).filter(item => item);
}
function ToolBar({
  headerTitle,
  tooltip,
  toolBarRender,
  action,
  options: propsOptions,
  selectedRowKeys,
  selectedRows,
  toolbar,
  onSearch,
  columns,
  ...rest
}) {
  // eslint-disable-next-line
  const counter = _container.default.useContainer();
  const intl = (0, _wauProvider.useIntl)();
  const optionDom = (0, _react.useMemo)(() => {
    const defaultOptions = {
      reload: () => {
        var _action$current;
        return action === null || action === void 0 || (_action$current = action.current) === null || _action$current === void 0 ? void 0 : _action$current.reload();
      },
      density: true,
      setting: true,
      search: false,
      fullScreen: false
    };
    if (propsOptions === false) {
      return [];
    }
    const options = {
      ...defaultOptions,
      ...propsOptions
    };
    return renderDefaultOption(options, {
      ...defaultOptions,
      intl
    }, columns);
  }, [action, columns, intl, propsOptions]);
  // 操作列表
  const actions = toolBarRender ? toolBarRender(action === null || action === void 0 ? void 0 : action.current, {
    selectedRowKeys,
    selectedRows
  }) : [];
  const searchConfig = (0, _react.useMemo)(() => {
    if (!propsOptions) {
      return false;
    }
    if (!propsOptions.search) return false;

    /** 受控的value 和 onChange */
    const defaultSearchConfig = {
      value: counter.keyWords,
      onChange: e => counter.setKeyWords(e.target.value)
    };
    if (propsOptions.search === true) return defaultSearchConfig;
    return {
      ...defaultSearchConfig,
      ...propsOptions.search
    };
  }, [counter, propsOptions]);
  (0, _react.useEffect)(() => {
    if (counter.keyWords === undefined) {
      onSearch === null || onSearch === void 0 ? void 0 : onSearch('');
    }
  }, [counter.keyWords, onSearch]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ListToolBar.default, {
    title: headerTitle,
    tooltip: tooltip || rest.tip,
    search: searchConfig,
    onSearch: onSearch,
    actions: actions,
    settings: optionDom,
    ...toolbar
  });
}
var _default = ToolBar;
exports.default = _default;