"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _useAvailableAlignments = _interopRequireDefault(require("./use-available-alignments"));
var _constants = require("./constants");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function BlockAlignmentUI({
  value,
  onChange,
  controls,
  isToolbar,
  isCollapsed = true
}) {
  const enabledControls = (0, _useAvailableAlignments.default)(controls);
  const hasEnabledControls = !!enabledControls.length;
  if (!hasEnabledControls) {
    return null;
  }
  function onChangeAlignment(align) {
    onChange([value, 'none'].includes(align) ? undefined : align);
  }
  const activeAlignmentControl = _constants.BLOCK_ALIGNMENTS_CONTROLS[value];
  const defaultAlignmentControl = _constants.BLOCK_ALIGNMENTS_CONTROLS[_constants.DEFAULT_CONTROL];
  const UIComponent = isToolbar ? _components.ToolbarGroup : _components.ToolbarDropdownMenu;
  const commonProps = {
    icon: activeAlignmentControl ? activeAlignmentControl.icon : defaultAlignmentControl.icon,
    label: (0, _i18n.__)('Align')
  };
  const extraProps = isToolbar ? {
    isCollapsed,
    controls: enabledControls.map(({
      name: controlName
    }) => {
      return {
        ..._constants.BLOCK_ALIGNMENTS_CONTROLS[controlName],
        isActive: value === controlName || !value && controlName === 'none',
        role: isCollapsed ? 'menuitemradio' : undefined,
        onClick: () => onChangeAlignment(controlName)
      };
    })
  } : {
    toggleProps: {
      description: (0, _i18n.__)('Change alignment')
    },
    children: ({
      onClose
    }) => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuGroup, {
          className: "block-editor-block-alignment-control__menu-group",
          children: enabledControls.map(({
            name: controlName,
            info
          }) => {
            const {
              icon,
              title
            } = _constants.BLOCK_ALIGNMENTS_CONTROLS[controlName];
            // If no value is provided, mark as selected the `none` option.
            const isSelected = controlName === value || !value && controlName === 'none';
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
              icon: icon,
              iconPosition: "left",
              className: (0, _clsx.default)('components-dropdown-menu__menu-item', {
                'is-active': isSelected
              }),
              isSelected: isSelected,
              onClick: () => {
                onChangeAlignment(controlName);
                onClose();
              },
              role: "menuitemradio",
              info: info,
              children: title
            }, controlName);
          })
        })
      });
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(UIComponent, {
    ...commonProps,
    ...extraProps
  });
}
var _default = exports.default = BlockAlignmentUI;
//# sourceMappingURL=ui.js.map