import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["children", "level", "size", "disabled"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
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 pick from 'lodash.pick';
/** @type {React.ComponentType<any>} */

var SplitButton = /*#__PURE__*/function (_PureComponent) {
  _inherits(SplitButton, _PureComponent);

  var _super = _createSuper(SplitButton);

  function SplitButton() {
    var _this;

    _classCallCheck(this, SplitButton);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "firstChild", pick(_this.props.children[0].props, ['label']));

    _defineProperty(_assertThisInitialized(_this), "state", {
      buttonLabel: _this.firstChild.label,
      popoverActive: false,
      popoverAnchorEl: null
    });

    _defineProperty(_assertThisInitialized(_this), "handleMainButtonClick", function (event) {
      _this.props.onButtonClick(event);
    });

    _defineProperty(_assertThisInitialized(_this), "handleSecondaryButtonClick", function (event) {
      var onSecondaryButtonClick = _this.props.onSecondaryButtonClick;

      _this.setState({
        popoverActive: true,
        popoverAnchorEl: event.currentTarget
      });

      if (onSecondaryButtonClick) {
        onSecondaryButtonClick(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleMenuItemClick", function (child, event) {
      var childProps = child.props;

      _this.setState({
        popoverActive: false
      });

      childProps.onClick(event);
    });

    _defineProperty(_assertThisInitialized(_this), "handleCloseClick", function () {
      _this.setState({
        popoverActive: false
      });
    });

    return _this;
  }

  _createClass(SplitButton, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          children = _this$props.children,
          level = _this$props.level,
          size = _this$props.size,
          disabled = _this$props.disabled,
          others = _objectWithoutProperties(_this$props, _excluded);

      var _this$state = this.state,
          buttonLabel = _this$state.buttonLabel,
          popoverActive = _this$state.popoverActive,
          popoverAnchorEl = _this$state.popoverAnchorEl;
      var boxProps = pickBoxProps(others);
      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: this.handleMainButtonClick
      }), /*#__PURE__*/React.createElement(Button, {
        icon: /*#__PURE__*/React.createElement(IconChevronDownSmallOutline, null),
        level: level,
        size: size,
        disabled: disabled,
        onClick: this.handleSecondaryButtonClick
      })), /*#__PURE__*/React.createElement(Popover, {
        active: popoverActive,
        anchorEl: popoverAnchorEl,
        backdrop: "transparent",
        lockScroll: false,
        onEscKeyDown: this.handleCloseClick,
        onOverlayClick: this.handleCloseClick,
        position: "start"
      }, /*#__PURE__*/React.createElement(Menu, null, React.Children.map(children, function (child) {
        if (child.props.label !== buttonLabel) {
          return /*#__PURE__*/React.cloneElement(child, {
            onClick: function onClick(event) {
              return _this2.handleMenuItemClick(child, event);
            }
          });
        }
      }))));
    }
  }]);

  return SplitButton;
}(PureComponent);

SplitButton.defaultProps = {
  level: 'primary',
  size: 'medium'
};
export default SplitButton;