"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MenuItem = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _element = require("@wordpress/element");
var _shortcut = _interopRequireDefault(require("../shortcut"));
var _button = _interopRequireDefault(require("../button"));
var _icon = _interopRequireDefault(require("../icon"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function UnforwardedMenuItem(props, ref) {
  let {
    children,
    info,
    className,
    icon,
    iconPosition = 'right',
    shortcut,
    isSelected,
    role = 'menuitem',
    suffix,
    ...buttonProps
  } = props;
  className = (0, _clsx.default)('components-menu-item__button', className);
  if (info) {
    children = /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: "components-menu-item__info-wrapper",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "components-menu-item__item",
        children: children
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "components-menu-item__info",
        children: info
      })]
    });
  }
  if (icon && typeof icon !== 'string') {
    icon = (0, _element.cloneElement)(icon, {
      className: (0, _clsx.default)('components-menu-items__item-icon', {
        'has-icon-right': iconPosition === 'right'
      })
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.default, {
    __next40pxDefaultSize: true,
    ref: ref
    // Make sure aria-checked matches spec https://www.w3.org/TR/wai-aria-1.1/#aria-checked
    ,
    "aria-checked": role === 'menuitemcheckbox' || role === 'menuitemradio' ? isSelected : undefined,
    role: role,
    icon: iconPosition === 'left' ? icon : undefined,
    className: className,
    ...buttonProps,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "components-menu-item__item",
      children: children
    }), !suffix && /*#__PURE__*/(0, _jsxRuntime.jsx)(_shortcut.default, {
      className: "components-menu-item__shortcut",
      shortcut: shortcut
    }), !suffix && icon && iconPosition === 'right' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.default, {
      icon: icon
    }), suffix]
  });
}

/**
 * MenuItem is a component which renders a button intended to be used in combination with the `DropdownMenu` component.
 *
 * ```jsx
 * import { MenuItem } from '@wordpress/components';
 * import { useState } from '@wordpress/element';
 *
 * const MyMenuItem = () => {
 * 	const [ isActive, setIsActive ] = useState( true );
 *
 * 	return (
 * 		<MenuItem
 * 			icon={ isActive ? 'yes' : 'no' }
 * 			isSelected={ isActive }
 * 			role="menuitemcheckbox"
 * 			onClick={ () => setIsActive( ( state ) => ! state ) }
 * 		>
 * 			Toggle
 * 		</MenuItem>
 * 	);
 * };
 * ```
 */
const MenuItem = exports.MenuItem = (0, _element.forwardRef)(UnforwardedMenuItem);
var _default = exports.default = MenuItem;
//# sourceMappingURL=index.js.map