"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _compose = require("@wordpress/compose");
var _i18n = require("@wordpress/i18n");
var _convertToGroupButtons = require("../convert-to-group-buttons");
var _blockLock = require("../block-lock");
var _store = require("../../store");
var _blockModeToggle = _interopRequireDefault(require("../block-settings-menu/block-mode-toggle"));
var _contentLock = require("../content-lock");
var _blockRename = require("../block-rename");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  Fill,
  Slot
} = (0, _components.createSlotFill)('BlockSettingsMenuControls');
const BlockSettingsMenuControlsSlot = ({
  fillProps,
  clientIds = null
}) => {
  const {
    selectedBlocks,
    selectedClientIds,
    isContentOnly
  } = (0, _data.useSelect)(select => {
    const {
      getBlockNamesByClientId,
      getSelectedBlockClientIds,
      getBlockEditingMode
    } = select(_store.store);
    const ids = clientIds !== null ? clientIds : getSelectedBlockClientIds();
    return {
      selectedBlocks: getBlockNamesByClientId(ids),
      selectedClientIds: ids,
      isContentOnly: getBlockEditingMode(ids[0]) === 'contentOnly'
    };
  }, [clientIds]);
  const {
    canLock
  } = (0, _blockLock.useBlockLock)(selectedClientIds[0]);
  const {
    canRename
  } = (0, _blockRename.useBlockRename)(selectedBlocks[0]);
  const showLockButton = selectedClientIds.length === 1 && canLock && !isContentOnly;
  const showRenameButton = selectedClientIds.length === 1 && canRename && !isContentOnly;

  // Check if current selection of blocks is Groupable or Ungroupable
  // and pass this props down to ConvertToGroupButton.
  const convertToGroupButtonProps = (0, _convertToGroupButtons.useConvertToGroupButtonProps)(selectedClientIds);
  const {
    isGroupable,
    isUngroupable
  } = convertToGroupButtonProps;
  const showConvertToGroupButton = isGroupable || isUngroupable;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Slot, {
    fillProps: {
      ...fillProps,
      selectedBlocks,
      selectedClientIds
    },
    children: fills => {
      if (!fills?.length > 0 && !showConvertToGroupButton && !showLockButton) {
        return null;
      }
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.MenuGroup, {
        children: [showConvertToGroupButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_convertToGroupButtons.ConvertToGroupButton, {
          ...convertToGroupButtonProps,
          onClose: fillProps?.onClose
        }), showLockButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockLock.BlockLockMenuItem, {
          clientId: selectedClientIds[0]
        }), showRenameButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockRename.BlockRenameControl, {
          clientId: selectedClientIds[0]
        }), fills, fillProps?.canMove && !fillProps?.onlyBlock && !isContentOnly && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
          onClick: (0, _compose.pipe)(fillProps?.onClose, fillProps?.onMoveTo),
          children: (0, _i18n.__)('Move to')
        }), selectedClientIds.length === 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_contentLock.ModifyContentLockMenuItem, {
          clientId: selectedClientIds[0],
          onClose: fillProps?.onClose
        }), fillProps?.count === 1 && !isContentOnly && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockModeToggle.default, {
          clientId: fillProps?.firstBlockClientId,
          onToggle: fillProps?.onClose
        })]
      });
    }
  });
};

/**
 * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/block-settings-menu-controls/README.md
 *
 * @param {Object} props Fill props.
 * @return {Element} Element.
 */
function BlockSettingsMenuControls({
  ...props
}) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalStyleProvider, {
    document: document,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Fill, {
      ...props
    })
  });
}
BlockSettingsMenuControls.Slot = BlockSettingsMenuControlsSlot;
var _default = exports.default = BlockSettingsMenuControls;
//# sourceMappingURL=index.js.map