"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
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");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. 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 = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}split-button`,
    className,
    menu,
    children,
    onClick,
    onKeyDown,
    icon,
    size,
    ...props
  } = _ref;
  const rootRef = (0, _react.useRef)(null);
  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__*/(0, _jsxRuntime.jsxs)(_ButtonGroup.default, {
    className: (0, _classnames.default)(baseClassName, className),
    innerRef: rootRef,
    onKeyDown: handleKeyDown,
    ...props,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
      icon: icon,
      size: size,
      tabIndex: 0,
      onClick: onClick,
      children: children
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.default, {
      className: `${baseClassName}__dropdown-trigger`,
      menu: menu,
      menuPlacement: "bottom-end",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        size: size,
        caret: true,
        tabIndex: -1
      })
    })]
  });
};
var _default = exports.default = SplitButton;