// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { cloneElement, isValidElement, Children, forwardRef } from 'react';
import Menu, { MenuItem, MenuHeader, MenuBaseItem, MenuDivider } from '../Menu';
import ToolbarExpander from './ToolbarExpander';
import ToolbarGroup from './ToolbarGroup';
import Button from '../Button';
import Dropdown from '../Dropdown';
import { ListOperation } from '../List';
import { wrapFunction } from '../utils';
import { jsx as _jsx } from "react/jsx-runtime";
const toMenuItem = (child, isLast, onItemClick) => {
  switch (child.type) {
    case Button:
      {
        const props = {};
        let other = {};
        ({
          disabled: props.disabled,
          icon: props.icon,
          onClick: props.onClick,
          children: props.children,
          ...other
        } = child.props);
        props.onClick = wrapFunction(props.onClick, onItemClick);
        Object.keys(other).forEach(k => {
          if (!(Button.defaultProps && k in Button.defaultProps)) {
            props[k] = other[k];
          }
        });
        if (child.props.component === 'a' || typeof child.props.component === 'function') {
          props.component = child.props.component;
        }
        return /*#__PURE__*/_jsx(MenuItem, {
          ...props,
          children: props.children
        });
        /* eslint-enable react/prop-types */
      }

    case ToolbarGroup:
      {
        const {
          title,
          children
        } = child.props;
        return [/*#__PURE__*/_jsx(MenuHeader, {
          children: title
        }, title), toMenuItems(children, onItemClick), isLast ? null : /*#__PURE__*/_jsx(MenuDivider, {})];
      }
    case ToolbarExpander:
      return null;
    case Dropdown:
      {
        let onMenuItemClick;
        if ( /*#__PURE__*/isValidElement(child.props.menu)) {
          ({
            onItemClick: onMenuItemClick
          } = child.props.menu.props);
        }
        return [child.props.children ? /*#__PURE__*/_jsx(MenuHeader, {
          children: child.props.children
        }) : null, toMenuItems(child.props.menu.props.children, wrapFunction(onMenuItemClick, onItemClick)), isLast ? null : /*#__PURE__*/_jsx(MenuDivider, {})];
      }
    case ListOperation:
      {
        const menuItem = toMenuItem(child.props.children, true, onItemClick);
        if ( /*#__PURE__*/isValidElement(menuItem)) {
          return /*#__PURE__*/cloneElement(child, {
            children: /*#__PURE__*/cloneElement(menuItem, {
              onClick: e => {
                menuItem.props.onClick(e);
                onItemClick?.(e);
              }
            })
          });
        }
        return null;
      }
    case MenuItem:
    case MenuBaseItem:
      return /*#__PURE__*/cloneElement(child, {
        onClick: wrapFunction(child.props.onClick, onItemClick)
      });
    case MenuDivider:
      return child;
    default:
      return /*#__PURE__*/_jsx(MenuBaseItem, {
        onClick: onItemClick,
        children: child
      });
  }
};
const toMenuItems = (children, onItemClick) => {
  const count = Children.count(children);
  return Children.map(children, (child, index) => {
    if (! /*#__PURE__*/isValidElement(child)) {
      return child;
    }
    return toMenuItem(child, index === count - 1, onItemClick);
  });
};
const ToolbarMenu = /*#__PURE__*/forwardRef(function ToolbarMenu(_ref, ref) {
  let {
    onItemClick,
    children,
    ...props
  } = _ref;
  return /*#__PURE__*/_jsx(Menu, {
    ...props,
    ref: ref,
    children: toMenuItems(children, onItemClick)
  });
});
export default ToolbarMenu;