import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/lib/divider/style";
import _Divider from "choerodon-ui/lib/divider";
var _excluded = ["name", "handler", "permissions", "display", "disabled", "disabledMessage", "actions"],
    _excluded2 = ["data"];

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useMemo, Fragment } from 'react';
import groupBy from 'lodash/groupBy';
import initial from 'lodash/initial';
import flatten from 'lodash/flatten';
import map from 'lodash/map';
import { Permission, Action } from '@choerodon/boot'; // import { Button, Tooltip } from 'choerodon-ui/pro';

import "./index.less";

var HeaderButtons = function HeaderButtons(_ref) {
  var items = _ref.items,
      children = _ref.children,
      _ref$showClassName = _ref.showClassName,
      showClassName = _ref$showClassName === void 0 ? true : _ref$showClassName;
  var displayBtn = useMemo(function () {
    return items.filter(function (_ref2) {
      var display = _ref2.display;
      return display;
    });
  }, [items]);
  var btnNodes = useMemo(function () {
    var btnGroups = map(groupBy(displayBtn, 'group'), function (value) {
      var Split = /*#__PURE__*/React.createElement(_Divider, {
        key: Math.random(),
        type: "vertical",
        className: "c7ncd-header-split"
      });
      var btns = map(value, function (_ref3) {
        var name = _ref3.name,
            handler = _ref3.handler,
            permissions = _ref3.permissions,
            display = _ref3.display,
            disabled = _ref3.disabled,
            disabledMessage = _ref3.disabledMessage,
            actions = _ref3.actions,
            props = _objectWithoutProperties(_ref3, _excluded);

        var btn;

        if (actions) {
          var data = actions.data,
              restActionsProps = _objectWithoutProperties(actions, _excluded2);

          btn = /*#__PURE__*/React.createElement(Action, Object.assign({}, restActionsProps, {
            data: data,
            className: "c7ncd-header-action",
            style: {
              color: '#3f51b5',
              padding: 0
            }
          }));
        } else {
          btn = /*#__PURE__*/React.createElement(_Button, Object.assign({}, props, {
            disabled: disabled,
            onClick: handler,
            type: "primary"
          }), name);
        }

        return /*#__PURE__*/React.createElement(Fragment, {
          key: name
        }, permissions && permissions.length ? /*#__PURE__*/React.createElement(Permission, {
          service: permissions
        }, disabled && disabledMessage ? /*#__PURE__*/React.createElement(_Tooltip, {
          title: disabledMessage || '',
          placement: "bottom"
        }, btn) : btn) : btn);
      });
      return [].concat(_toConsumableArray(btns), [Split]);
    });
    return initial(flatten(btnGroups));
  }, [displayBtn]);
  return displayBtn.length ? /*#__PURE__*/React.createElement("div", {
    className: showClassName ? 'c7ncd-header-btns' : '',
    style: {
      direction: 'rtl'
    }
  }, btnNodes, children) : null;
};

HeaderButtons.defaultProps = {
  items: []
};
export default HeaderButtons;