import "core-js/modules/es.regexp.exec.js";
import React, { useEffect, useMemo } from 'react';
import { ReloadOutlined, SettingOutlined } from '@ant-design/icons';
import Tooltip from '../tooltip';
import Button from '../button';
import { useIntl } from '../wau-provider';
import ListToolBar from './ListToolBar';
import ColumnSetting from './ColumnSetting';
import FullScreenIcon from './FullscreenIcon';
import DensityIcon from './DensityIcon';
import Container from './container';
import { jsx as _jsx } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
function getButtonText({
  intl
}) {
  return {
    reload: {
      text: intl.getMessage('tableToolBar.reload', '刷新'),
      icon: /*#__PURE__*/_jsx(Button, {
        icon: /*#__PURE__*/_jsx(ReloadOutlined, {})
      })
    },
    density: {
      text: intl.getMessage('tableToolBar.density', '表格密度'),
      icon: /*#__PURE__*/_jsx(DensityIcon, {})
    },
    setting: {
      text: intl.getMessage('tableToolBar.columnSetting', '列设置'),
      icon: /*#__PURE__*/_jsx(SettingOutlined, {})
    },
    fullScreen: {
      text: intl.getMessage('tableToolBar.fullScreen', '全屏'),
      icon: /*#__PURE__*/_jsx(FullScreenIcon, {})
    }
  };
}

/**
 * 渲染默认的 工具栏
 *
 * @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__*/_createElement(ColumnSetting, {
        ...options.setting,
        columns: columns,
        key: key
      });
    }
    if (key === 'fullScreen') {
      return /*#__PURE__*/_jsx("span", {
        onClick: value === true ? defaultOptions[key] : value,
        children: /*#__PURE__*/_jsx(FullScreenIcon, {})
      }, key);
    }
    const optionItem = getButtonText(defaultOptions)[key];
    if (optionItem) {
      return /*#__PURE__*/_jsx("span", {
        onClick: () => {
          if (value && defaultOptions[key] !== true) {
            if (value !== true) {
              value();
              return;
            }
            defaultOptions[key]();
          }
        },
        children: /*#__PURE__*/_jsx(Tooltip, {
          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.useContainer();
  const intl = useIntl();
  const optionDom = useMemo(() => {
    const defaultOptions = {
      reload: () => {
        var _action$current;
        return action === null || action === void 0 ? 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 = 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]);
  useEffect(() => {
    if (counter.keyWords === undefined) {
      onSearch === null || onSearch === void 0 ? void 0 : onSearch('');
    }
  }, [counter.keyWords, onSearch]);
  return /*#__PURE__*/_jsx(ListToolBar, {
    title: headerTitle,
    tooltip: tooltip || rest.tip,
    search: searchConfig,
    onSearch: onSearch,
    actions: actions,
    settings: optionDom,
    ...toolbar
  });
}
export default ToolBar;