import "core-js/modules/es.regexp.exec.js";
import React, { useContext } from 'react';
import classNames from 'classnames';
import Tooltip from '../tooltip';
import Space from '../space';
import WauSearch from '../wau-search';
import Tabs from '../tabs';
import ConfigProvider from '../config-provider';
import LabelIconTip from '../wau-label-icon-tip';
import { useIntl } from '../wau-provider';
import HeaderMenu from './HeaderMenu';

/** Antd 默认直接导出了 rc 组件中的 Tab.Pane 组件。 */
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * 获取配置区域 DOM Item
 *
 * @param setting 配置项
 */
function getSettingItem(setting) {
  if ( /*#__PURE__*/React.isValidElement(setting)) {
    return setting;
  }
  if (setting) {
    const settingConfig = setting;
    const {
      icon,
      tooltip,
      onClick,
      key
    } = settingConfig;
    if (icon && tooltip) {
      return /*#__PURE__*/_jsx(Tooltip, {
        title: tooltip,
        children: /*#__PURE__*/_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 = useIntl();

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

  /** 根据配置自动生成的查询框 */
  const searchNode = getSearchInput(search);
  /** 轻量筛选组件 */
  const filtersNode = filter ? /*#__PURE__*/_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__*/_jsx(Space, {
      align: "center",
      children: actions.map((action, index) => {
        if (! /*#__PURE__*/React.isValidElement(action)) {
          // eslint-disable-next-line react/no-array-index-key
          return /*#__PURE__*/_jsx(React.Fragment, {
            children: action
          }, index);
        }
        return /*#__PURE__*/React.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__*/_jsxs(_Fragment, {
    children: [tooltip || title || subTitle ? /*#__PURE__*/_jsx("div", {
      className: `${prefixCls}-title`,
      children: /*#__PURE__*/_jsx(LabelIconTip, {
        tooltip: tooltip,
        label: title,
        subTitle: subTitle
      })
    }) : null, menu && /*#__PURE__*/_jsx(HeaderMenu, {
      ...menu,
      prefixCls: prefixCls
    })]
  });
  return /*#__PURE__*/_jsxs("div", {
    style: style,
    className: classNames(`${prefixCls}`, className),
    children: [hasTitle && actionDom ? /*#__PURE__*/_jsx("div", {
      className: `${prefixCls}-container`,
      children: titleNode
    }) : null, /*#__PURE__*/_jsxs("div", {
      className: `${prefixCls}-container`,
      style: {
        paddingTop: hasTitle && actionDom ? 0 : undefined
      },
      children: [/*#__PURE__*/_jsx(Space, {
        className: `${prefixCls}-left`,
        children: /*#__PURE__*/_jsx("div", {
          children: hasTitle && !actionDom ? titleNode : actionDom
        })
      }), /*#__PURE__*/_jsxs(Space, {
        className: `${prefixCls}-right`,
        size: 16,
        children: [searchNode ? /*#__PURE__*/_jsx("div", {
          className: `${prefixCls}-search`,
          children: searchNode
        }) : null, !multipleLine ? filtersNode : null, settings !== null && settings !== void 0 && settings.length ? /*#__PURE__*/_jsx(Space, {
          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
              _jsx("div", {
                className: `${prefixCls}-setting-item`,
                children: settingItem
              }, index)
            );
          })
        }) : null]
      })]
    }), multipleLine ? /*#__PURE__*/_jsx("div", {
      className: `${prefixCls}-extra-line`,
      children: tabs.items && tabs.items.length ? /*#__PURE__*/_jsx(Tabs, {
        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]
  });
};
export default ListToolBar;