"use strict";

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _Button = _interopRequireDefault(require("../Button"));

var _ButtonGroup = _interopRequireDefault(require("../ButtonGroup"));

var _Dropdown = _interopRequireDefault(require("../Dropdown"));

var _utils = require("../utils");

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

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
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 = (0, _react.useRef)();
  const {
    focusNext,
    focusPrev
  } = (0, _utils.createFocusManager)(rootRef, undefined, focusStrategy);
  const handleKeyDown = (0, _utils.wrapFunction)(onKeyDown, e => {
    e.stopPropagation();

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

      case 'ArrowRight':
        e.preventDefault();
        focusNext();
        break;
    }
  });
  return /*#__PURE__*/_react.default.createElement(_ButtonGroup.default, (0, _extends2.default)({
    className: (0, _classnames.default)(baseClassName, className),
    innerRef: rootRef,
    onKeyDown: handleKeyDown
  }, props), /*#__PURE__*/_react.default.createElement(_Button.default, {
    tabIndex: 0,
    onClick: onClick
  }, children), /*#__PURE__*/_react.default.createElement(_Dropdown.default, {
    className: `${baseClassName}__dropdown-trigger`,
    menu: menu,
    menuPlacement: "bottom-end"
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    caret: true,
    tabIndex: -1
  })));
};

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

  /**
   * @since 0.0.47
   */
  opened: _propTypes.default.bool,

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

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string,

  /**
   * @ignore
   */
  onClick: _propTypes.default.func,

  /**
   * @ignore
   */
  onKeyDown: _propTypes.default.func
};
SplitButton.defaultProps = {
  menu: null,
  opened: false,
  children: null,
  className: null,
  baseClassName: `${_constants.CLS_PREFIX}split-button`,
  onClick: undefined,
  onKeyDown: undefined
};
var _default = SplitButton;
exports.default = _default;