"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/navigation/edit/leaf-more-menu.js
var leaf_more_menu_exports = {};
__export(leaf_more_menu_exports, {
  default: () => LeafMoreMenu
});
module.exports = __toCommonJS(leaf_more_menu_exports);
var import_blocks = require("@wordpress/blocks");
var import_icons = require("@wordpress/icons");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_i18n = require("@wordpress/i18n");
var import_block_editor = require("@wordpress/block-editor");
var import_constants = require("../constants");
var import_jsx_runtime = require("react/jsx-runtime");
var POPOVER_PROPS = {
  className: "block-editor-block-settings-menu__popover",
  placement: "bottom-start"
};
var BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [
  "core/navigation-link",
  "core/navigation-submenu"
];
function AddSubmenuItem({
  block,
  onClose,
  expandedState,
  expand,
  setInsertedBlock
}) {
  const { insertBlock, replaceBlock, replaceInnerBlocks } = (0, import_data.useDispatch)(import_block_editor.store);
  const clientId = block.clientId;
  const isDisabled = !BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes(
    block.name
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.MenuItem,
    {
      icon: import_icons.addSubmenu,
      disabled: isDisabled,
      onClick: () => {
        const updateSelectionOnInsert = false;
        const newLink = (0, import_blocks.createBlock)(
          import_constants.DEFAULT_BLOCK.name,
          import_constants.DEFAULT_BLOCK.attributes
        );
        if (block.name === "core/navigation-submenu") {
          insertBlock(
            newLink,
            block.innerBlocks.length,
            clientId,
            updateSelectionOnInsert
          );
        } else {
          const newSubmenu = (0, import_blocks.createBlock)(
            "core/navigation-submenu",
            block.attributes,
            block.innerBlocks
          );
          replaceBlock(clientId, newSubmenu);
          replaceInnerBlocks(
            newSubmenu.clientId,
            [newLink],
            updateSelectionOnInsert
          );
        }
        setInsertedBlock(newLink);
        if (!expandedState[block.clientId]) {
          expand(block.clientId);
        }
        onClose();
      },
      children: (0, import_i18n.__)("Add submenu link")
    }
  );
}
function LeafMoreMenu(props) {
  const { block } = props;
  const { clientId } = block;
  const { moveBlocksDown, moveBlocksUp, removeBlocks } = (0, import_data.useDispatch)(import_block_editor.store);
  const removeLabel = (0, import_i18n.sprintf)(
    /* translators: %s: block name */
    (0, import_i18n.__)("Remove %s"),
    (0, import_block_editor.BlockTitle)({ clientId, maximumLength: 25 })
  );
  const rootClientId = (0, import_data.useSelect)(
    (select) => {
      const { getBlockRootClientId } = select(import_block_editor.store);
      return getBlockRootClientId(clientId);
    },
    [clientId]
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.DropdownMenu,
    {
      icon: import_icons.moreVertical,
      label: (0, import_i18n.__)("Options"),
      className: "block-editor-block-settings-menu",
      popoverProps: POPOVER_PROPS,
      noIcons: true,
      ...props,
      children: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.MenuGroup, { children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.MenuItem,
            {
              icon: import_icons.chevronUp,
              onClick: () => {
                moveBlocksUp([clientId], rootClientId);
                onClose();
              },
              children: (0, import_i18n.__)("Move up")
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.MenuItem,
            {
              icon: import_icons.chevronDown,
              onClick: () => {
                moveBlocksDown([clientId], rootClientId);
                onClose();
              },
              children: (0, import_i18n.__)("Move down")
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            AddSubmenuItem,
            {
              block,
              onClose,
              expandedState: props.expandedState,
              expand: props.expand,
              setInsertedBlock: props.setInsertedBlock
            }
          )
        ] }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.MenuItem,
          {
            onClick: () => {
              removeBlocks([clientId], false);
              onClose();
            },
            children: removeLabel
          }
        ) })
      ] })
    }
  );
}
//# sourceMappingURL=leaf-more-menu.js.map
