"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _isString = _interopRequireDefault(require("lodash/isString"));

var _tooltip = _interopRequireDefault(require("../../tooltip"));

var _TableContext = _interopRequireDefault(require("../TableContext"));

var _ColumnFilter = _interopRequireDefault(require("./ColumnFilter"));

/**
 * 获取配置区域 DOM Item
 *
 * @param setting 配置项
 * @param prefixCls
 */
function getSettingItem(setting, prefixCls) {
  if ((0, _isString["default"])(setting) && setting === 'columnFilter') {
    return /*#__PURE__*/_react["default"].createElement(_ColumnFilter["default"], {
      prefixCls: prefixCls
    });
  }

  if ( /*#__PURE__*/_react["default"].isValidElement(setting)) {
    return setting;
  }

  if (setting) {
    var settingConfig = setting;
    var icon = settingConfig.icon,
        tooltip = settingConfig.tooltip,
        _onClick = settingConfig.onClick,
        key = settingConfig.key;

    if (icon && tooltip) {
      return /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
        title: tooltip
      }, /*#__PURE__*/_react["default"].createElement("span", {
        key: key,
        onClick: function onClick() {
          if (_onClick) {
            _onClick(key);
          }
        }
      }, icon));
    }

    return icon;
  }

  return null;
}

var ToolBar = function ToolBar(_ref) {
  var header = _ref.header,
      hideToolbar = _ref.hideToolbar,
      className = _ref.className,
      style = _ref.style,
      _ref$buttons = _ref.buttons,
      buttons = _ref$buttons === void 0 ? [] : _ref$buttons,
      _ref$settings = _ref.settings,
      settings = _ref$settings === void 0 ? [] : _ref$settings;

  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      prefixCls = _useContext.tableStore.prefixCls;

  var toolbarPrefixCls = "".concat(prefixCls, "-toolbar");
  var buttonsDom = (0, _react.useMemo)(function () {
    if (!Array.isArray(buttons)) {
      return buttons;
    }

    if (buttons.length < 1) {
      return null;
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(toolbarPrefixCls, "-buttons")
    }, buttons.map(function (btn, index) {
      if (! /*#__PURE__*/_react["default"].isValidElement(btn)) {
        // eslint-disable-next-line react/no-array-index-key
        return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, {
          key: index
        }, btn);
      }

      return /*#__PURE__*/_react["default"].cloneElement(btn, (0, _objectSpread2["default"])({
        // eslint-disable-next-line react/no-array-index-key
        key: index
      }, btn.props));
    }));
  }, [buttons]);
  var headerNode = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(toolbarPrefixCls, "-header")
    }, header);
  }, [header]);
  var settingsDom = (0, _react.useMemo)(function () {
    if (!Array.isArray(settings)) {
      return settings;
    }

    if (settings.length < 1) {
      return null;
    }

    return settings.length ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(toolbarPrefixCls, "-setting-items")
    }, settings.map(function (setting, index) {
      var settingItem = getSettingItem(setting, prefixCls);
      return (
        /*#__PURE__*/
        // eslint-disable-next-line react/no-array-index-key
        _react["default"].createElement("div", {
          key: index,
          className: "".concat(toolbarPrefixCls, "-setting-item")
        }, settingItem)
      );
    })) : null;
  }, [settings]); // 不展示 toolbar

  if (hideToolbar) {
    return null;
  }

  return /*#__PURE__*/_react["default"].createElement("div", {
    style: style,
    className: (0, _classnames["default"])("".concat(toolbarPrefixCls), className)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(toolbarPrefixCls, "-container")
  }, headerNode, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(toolbarPrefixCls, "-right")
  }, buttonsDom, settingsDom)));
};

var _default = ToolBar;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
