"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = LeafMoreMenu;
var _icons = require("@wordpress/icons");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _blockEditor = require("@wordpress/block-editor");
var _router = require("@wordpress/router");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

const POPOVER_PROPS = {
  className: 'block-editor-block-settings-menu__popover',
  placement: 'bottom-start'
};

/**
 * Internal dependencies
 */

const {
  useHistory,
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
function LeafMoreMenu(props) {
  const history = useHistory();
  const {
    path
  } = useLocation();
  const {
    block
  } = props;
  const {
    clientId
  } = block;
  const {
    moveBlocksDown,
    moveBlocksUp,
    removeBlocks
  } = (0, _data.useDispatch)(_blockEditor.store);
  const removeLabel = (0, _i18n.sprintf)(/* translators: %s: block name */
  (0, _i18n.__)('Remove %s'), (0, _blockEditor.BlockTitle)({
    clientId,
    maximumLength: 25
  }));
  const goToLabel = (0, _i18n.sprintf)(/* translators: %s: block name */
  (0, _i18n.__)('Go to %s'), (0, _blockEditor.BlockTitle)({
    clientId,
    maximumLength: 25
  }));
  const rootClientId = (0, _data.useSelect)(select => {
    const {
      getBlockRootClientId
    } = select(_blockEditor.store);
    return getBlockRootClientId(clientId);
  }, [clientId]);
  const onGoToPage = (0, _element.useCallback)(selectedBlock => {
    const {
      attributes,
      name
    } = selectedBlock;
    if (attributes.kind === 'post-type' && attributes.id && attributes.type && history) {
      history.navigate(`/${attributes.type}/${attributes.id}?canvas=edit`, {
        state: {
          backPath: path
        }
      });
    }
    if (name === 'core/page-list-item' && attributes.id && history) {
      history.navigate(`/page/${attributes.id}?canvas=edit`, {
        state: {
          backPath: path
        }
      });
    }
  }, [path, history]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.DropdownMenu, {
    icon: _icons.moreVertical,
    label: (0, _i18n.__)('Options'),
    className: "block-editor-block-settings-menu",
    popoverProps: POPOVER_PROPS,
    noIcons: true,
    ...props,
    children: ({
      onClose
    }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.MenuGroup, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
          icon: _icons.chevronUp,
          onClick: () => {
            moveBlocksUp([clientId], rootClientId);
            onClose();
          },
          children: (0, _i18n.__)('Move up')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
          icon: _icons.chevronDown,
          onClick: () => {
            moveBlocksDown([clientId], rootClientId);
            onClose();
          },
          children: (0, _i18n.__)('Move down')
        }), block.attributes?.type === 'page' && block.attributes?.id && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
          onClick: () => {
            onGoToPage(block);
            onClose();
          },
          children: goToLabel
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuGroup, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
          onClick: () => {
            removeBlocks([clientId], false);
            onClose();
          },
          children: removeLabel
        })
      })]
    })
  });
}
//# sourceMappingURL=leaf-more-menu.js.map