"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 _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = _interopRequireDefault(require("../tooltip"));
var _space = _interopRequireDefault(require("../space"));
var _wauSearch = _interopRequireDefault(require("../wau-search"));
var _tabs = _interopRequireDefault(require("../tabs"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauLabelIconTip = _interopRequireDefault(require("../wau-label-icon-tip"));
var _wauProvider = require("../wau-provider");
var _HeaderMenu = _interopRequireDefault(require("./HeaderMenu"));
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; }
/** Antd 默认直接导出了 rc 组件中的 Tab.Pane 组件。 */

/**
 * 获取配置区域 DOM Item
 *
 * @param setting 配置项
 */
function getSettingItem(setting) {
  if ( /*#__PURE__*/_react.default.isValidElement(setting)) {
    return setting;
  }
  if (setting) {
    const settingConfig = setting;
    const {
      icon,
      tooltip,
      onClick,
      key
    } = settingConfig;
    if (icon && tooltip) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.default, {
        title: tooltip,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          onClick: () => {
            if (onClick) {
              onClick(key);
            }
          },
          children: icon
        }, key)
      });
    }
    return icon;
  }
  return null;
}
const ListToolBar = ({
  prefixCls: customizePrefixCls,
  title,
  subTitle,
  tooltip,
  className,
  style,
  search,
  onSearch,
  multipleLine = false,
  filter,
  actions = [],
  settings = [],
  tabs = {},
  menu
}) => {
  const intl = (0, _wauProvider.useIntl)();

  /**
   * 获取搜索栏 DOM
   *
   * @param search 搜索框相关配置
   */
  const getSearchInput = searchObject => {
    if (!searchObject) {
      return null;
    }
    if ( /*#__PURE__*/_react.default.isValidElement(searchObject)) {
      return searchObject;
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauSearch.default, {
      style: {
        width: 200
      },
      placeholder: intl.getMessage('tableForm.inputPlaceholder', '请输入'),
      onSearch: onSearch,
      ...searchObject
    });
  };
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-table-list-toolbar', customizePrefixCls);

  /** 根据配置自动生成的查询框 */
  const searchNode = getSearchInput(search);
  /** 轻量筛选组件 */
  const filtersNode = filter ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: `${prefixCls}-filter`,
    children: filter
  }) : null;

  /** 有没有 title，判断了多个场景 */
  const hasTitle = menu || title || subTitle || tooltip;
  /** 没有 key 的时候帮忙加一下 key 不加的话很烦人 */
  const renderActionsDom = () => {
    if (!Array.isArray(actions)) {
      return actions;
    }
    if (actions.length < 1) {
      return null;
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_space.default, {
      align: "center",
      children: actions.map((action, index) => {
        if (! /*#__PURE__*/_react.default.isValidElement(action)) {
          // eslint-disable-next-line react/no-array-index-key
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
            children: action
          }, index);
        }
        return /*#__PURE__*/_react.default.cloneElement(action, {
          // eslint-disable-next-line react/no-array-index-key
          key: index,
          ...(action === null || action === void 0 ? void 0 : action.props)
        });
      })
    });
  };
  const actionDom = renderActionsDom();
  const titleNode = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [tooltip || title || subTitle ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixCls}-title`,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauLabelIconTip.default, {
        tooltip: tooltip,
        label: title,
        subTitle: subTitle
      })
    }) : null, menu && /*#__PURE__*/(0, _jsxRuntime.jsx)(_HeaderMenu.default, {
      ...menu,
      prefixCls: prefixCls
    })]
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    style: style,
    className: (0, _classnames.default)(`${prefixCls}`, className),
    children: [hasTitle && actionDom ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixCls}-container`,
      children: titleNode
    }) : null, /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${prefixCls}-container`,
      style: {
        paddingTop: hasTitle && actionDom ? 0 : undefined
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_space.default, {
        className: `${prefixCls}-left`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: hasTitle && !actionDom ? titleNode : actionDom
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_space.default, {
        className: `${prefixCls}-right`,
        size: 16,
        children: [searchNode ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${prefixCls}-search`,
          children: searchNode
        }) : null, !multipleLine ? filtersNode : null, settings !== null && settings !== void 0 && settings.length ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_space.default, {
          size: 12,
          align: "center",
          className: `${prefixCls}-setting-items`,
          children: settings.map((setting, index) => {
            const settingItem = getSettingItem(setting);
            return (
              /*#__PURE__*/
              // eslint-disable-next-line react/no-array-index-key
              (0, _jsxRuntime.jsx)("div", {
                className: `${prefixCls}-setting-item`,
                children: settingItem
              }, index)
            );
          })
        }) : null]
      })]
    }), multipleLine ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixCls}-extra-line`,
      children: tabs.items && tabs.items.length ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_tabs.default, {
        onChange: tabs.onChange,
        tabBarExtraContent: filtersNode,
        items: tabs.items.map(item => {
          var _item$key;
          return {
            ...item,
            key: (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : item.tab,
            label: item.tab
          };
        })
      }) : filtersNode
    }) : null]
  });
};
var _default = ListToolBar;
exports.default = _default;