import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Button from '../Button';
import ButtonGroup from '../ButtonGroup';
import Dropdown from '../Dropdown';
import { wrapFunction, createFocusManager } from '../utils';
const focusStrategy = {
  getNext(container, element) {
    const elements = container.children;
    const index = Array.prototype.indexOf.call(elements, element);

    if (index === 0) {
      return elements[1].firstElementChild;
    }

    return undefined;
  },

  getPrev(container, element) {
    if (element === container.children[1].querySelector('button')) {
      return container.children[0];
    }

    return undefined;
  },

  getFirst(container) {
    return container.children[0];
  },

  getLast(container) {
    return container.children[1].querySelector('button');
  }

};
/**
 * `Split Button` is a combination of a standard [Button](#!/Button) with a [Dropdown](#!/Dropdown) menu.
 * It is used for grouping several related actions when one of the actions should be accessible immediately
 * because it is used more often than others.
 * @since 0.0.40
 */

const SplitButton = ({
  baseClassName,
  className,
  menu,
  children,
  opened,
  onClick,
  onKeyDown,
  ...props
}) => {
  const rootRef = useRef();
  const {
    focusNext,
    focusPrev
  } = createFocusManager(rootRef, undefined, focusStrategy);
  const handleKeyDown = wrapFunction(onKeyDown, e => {
    e.stopPropagation();

    switch (e.key) {
      case 'ArrowLeft':
        e.preventDefault();
        focusPrev();
        break;

      case 'ArrowRight':
        e.preventDefault();
        focusNext();
        break;
    }
  });
  return /*#__PURE__*/React.createElement(ButtonGroup, _extends({
    className: classNames(baseClassName, className),
    innerRef: rootRef,
    onKeyDown: handleKeyDown
  }, props), /*#__PURE__*/React.createElement(Button, {
    tabIndex: 0,
    onClick: onClick
  }, children), /*#__PURE__*/React.createElement(Dropdown, {
    className: "".concat(baseClassName, "__dropdown-trigger"),
    menu: menu,
    menuPlacement: "bottom-end"
  }, /*#__PURE__*/React.createElement(Button, {
    caret: true,
    tabIndex: -1
  })));
};

SplitButton.propTypes = {
  /**
   * @since 0.0.47
   */
  menu: PropTypes.any,

  /**
   * @since 0.0.47
   */
  opened: PropTypes.bool,

  /**
   * Content of the button group
   * @since 0.0.40
   */
  children: PropTypes.node,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string,

  /**
   * @ignore
   */
  onClick: PropTypes.func,

  /**
   * @ignore
   */
  onKeyDown: PropTypes.func
};
SplitButton.defaultProps = {
  menu: null,
  opened: false,
  children: null,
  className: null,
  baseClassName: "".concat(CLS_PREFIX, "split-button"),
  onClick: undefined,
  onKeyDown: undefined
};
export default SplitButton;