import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "level", "size", "onButtonClick", "onSecondaryButtonClick", "disabled"];

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) { _defineProperty(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; }

import React, { useRef, useState } from 'react';
import Box, { pickBoxProps } from '../box';
import Button from '../button';
import ButtonGroup from '../buttonGroup';
import { Menu } from '../menu';
import Popover from '../popover';
import { IconChevronDownSmallOutline } from '@teamleader/ui-icons';
import isReactElement from '../utils/is-react-element';

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 = _objectWithoutProperties(_ref, _excluded);

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      popoverActive = _useState2[0],
      setPopoverActive = _useState2[1];

  var popoverAnchorEl = 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({}, pickBoxProps(others));

  var buttonLabel;

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

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

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

SplitButton.displayName = 'SplitButton';
export default SplitButton;