"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _Menu = _interopRequireWildcard(require("../Menu"));
var _ToolbarExpander = _interopRequireDefault(require("./ToolbarExpander"));
var _ToolbarGroup = _interopRequireDefault(require("./ToolbarGroup"));
var _Button = _interopRequireDefault(require("../Button"));
var _Dropdown = _interopRequireDefault(require("../Dropdown"));
var _List = require("../List");
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
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-2023. Plesk International GmbH. All rights reserved.

const toMenuItem = (child, isLast, onItemClick) => {
  switch (child.type) {
    case _Button.default:
      {
        const props = {};
        let other = {};
        ({
          disabled: props.disabled,
          icon: props.icon,
          onClick: props.onClick,
          children: props.children,
          ...other
        } = child.props);
        props.onClick = (0, _utils.wrapFunction)(props.onClick, onItemClick);
        Object.keys(other).forEach(k => {
          if (!(_Button.default.defaultProps && k in _Button.default.defaultProps)) {
            props[k] = other[k];
          }
        });
        if (child.props.component === 'a' || typeof child.props.component === 'function') {
          props.component = child.props.component;
        }
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuItem, {
          ...props,
          children: props.children
        });
        /* eslint-enable react/prop-types */
      }

    case _ToolbarGroup.default:
      {
        const {
          title,
          children
        } = child.props;
        return [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuHeader, {
          children: title
        }, title), toMenuItems(children, onItemClick), isLast ? null : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuDivider, {})];
      }
    case _ToolbarExpander.default:
      return null;
    case _Dropdown.default:
      {
        let onMenuItemClick;
        if ( /*#__PURE__*/(0, _react.isValidElement)(child.props.menu)) {
          ({
            onItemClick: onMenuItemClick
          } = child.props.menu.props);
        }
        return [child.props.children ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuHeader, {
          children: child.props.children
        }) : null, toMenuItems(child.props.menu.props.children, (0, _utils.wrapFunction)(onMenuItemClick, onItemClick)), isLast ? null : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuDivider, {})];
      }
    case _List.ListOperation:
      {
        const menuItem = toMenuItem(child.props.children, true, onItemClick);
        if ( /*#__PURE__*/(0, _react.isValidElement)(menuItem)) {
          return /*#__PURE__*/(0, _react.cloneElement)(child, {
            children: /*#__PURE__*/(0, _react.cloneElement)(menuItem, {
              onClick: e => {
                menuItem.props.onClick(e);
                onItemClick?.(e);
              }
            })
          });
        }
        return null;
      }
    case _Menu.MenuItem:
    case _Menu.MenuBaseItem:
      return /*#__PURE__*/(0, _react.cloneElement)(child, {
        onClick: (0, _utils.wrapFunction)(child.props.onClick, onItemClick)
      });
    case _Menu.MenuDivider:
      return child;
    default:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.MenuBaseItem, {
        onClick: onItemClick,
        children: child
      });
  }
};
const toMenuItems = (children, onItemClick) => {
  const count = _react.Children.count(children);
  return _react.Children.map(children, (child, index) => {
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      return child;
    }
    return toMenuItem(child, index === count - 1, onItemClick);
  });
};
const ToolbarMenu = /*#__PURE__*/(0, _react.forwardRef)(function ToolbarMenu(_ref, ref) {
  let {
    onItemClick,
    children,
    ...props
  } = _ref;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.default, {
    ...props,
    ref: ref,
    children: toMenuItems(children, onItemClick)
  });
});
var _default = exports.default = ToolbarMenu;