"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _box = _interopRequireWildcard(require("../box"));

var _button = _interopRequireDefault(require("../button"));

var _buttonGroup = _interopRequireDefault(require("../buttonGroup"));

var _menu = require("../menu");

var _popover = _interopRequireDefault(require("../popover"));

var _uiIcons = require("@teamleader/ui-icons");

var _isReactElement = _interopRequireDefault(require("../utils/is-react-element"));

var _excluded = ["children", "level", "size", "onButtonClick", "onSecondaryButtonClick", "disabled"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var SplitButton = function SplitButton(_ref) {
  var children = _ref.children,
      _ref$level = _ref.level,
      level = _ref$level === void 0 ? 'primary' : _ref$level,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      onButtonClick = _ref.onButtonClick,
      onSecondaryButtonClick = _ref.onSecondaryButtonClick,
      disabled = _ref.disabled,
      others = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      popoverActive = _useState2[0],
      setPopoverActive = _useState2[1];

  var popoverAnchorEl = (0, _react.useRef)(null);

  var handleMainButtonClick = function handleMainButtonClick(event) {
    onButtonClick(event);
  };

  var handleSecondaryButtonClick = function handleSecondaryButtonClick(event) {
    setPopoverActive(true);
    popoverAnchorEl.current = event.currentTarget;
    onSecondaryButtonClick && onSecondaryButtonClick(event);
  };

  var handleMenuItemClick = function handleMenuItemClick(child, event) {
    var childProps = child.props;
    setPopoverActive(false);
    childProps.onClick(event);
  };

  var handleCloseClick = function handleCloseClick() {
    setPopoverActive(false);
  };

  var boxProps = _objectSpread({}, (0, _box.pickBoxProps)(others));

  var buttonLabel;

  if (Array.isArray(children)) {
    var childrenArray = children;

    if (childrenArray.length && (0, _isReactElement.default)(childrenArray[0])) {
      buttonLabel = childrenArray[0].props.label;
    }
  }

  return /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({
    display: "flex",
    justifyContent: "center"
  }, boxProps, {
    "data-teamleader-ui": "split-menu"
  }), /*#__PURE__*/_react.default.createElement(_buttonGroup.default, {
    segmented: true
  }, /*#__PURE__*/_react.default.createElement(_button.default, {
    label: buttonLabel,
    level: level,
    size: size,
    disabled: disabled,
    onClick: handleMainButtonClick
  }), /*#__PURE__*/_react.default.createElement(_button.default, {
    icon: /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronDownSmallOutline, null),
    level: level,
    size: size,
    disabled: disabled,
    onClick: handleSecondaryButtonClick
  })), /*#__PURE__*/_react.default.createElement(_popover.default, {
    active: popoverActive,
    anchorEl: popoverAnchorEl === null || popoverAnchorEl === void 0 ? void 0 : popoverAnchorEl.current,
    backdrop: "transparent",
    lockScroll: false,
    onEscKeyDown: handleCloseClick,
    onOverlayClick: handleCloseClick,
    position: "start"
  }, /*#__PURE__*/_react.default.createElement(_menu.Menu, null, _react.default.Children.map(children, function (child) {
    if ( /*#__PURE__*/_react.default.isValidElement(child) && child.props.label !== buttonLabel) {
      return /*#__PURE__*/_react.default.cloneElement(child, {
        onClick: function onClick(event) {
          return handleMenuItemClick(child, event);
        }
      });
    }
  }))));
};

SplitButton.displayName = 'SplitButton';
var _default = SplitButton;
exports.default = _default;