"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/block-switcher/block-transformations-menu.js
var block_transformations_menu_exports = {};
__export(block_transformations_menu_exports, {
  default: () => block_transformations_menu_default
});
module.exports = __toCommonJS(block_transformations_menu_exports);
var import_i18n = require("@wordpress/i18n");
var import_components = require("@wordpress/components");
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_block_icon = __toESM(require("../block-icon/index.cjs"));
var import_preview_block_popover = __toESM(require("./preview-block-popover.cjs"));
var import_block_variation_transformations = __toESM(require("./block-variation-transformations.cjs"));
var import_jsx_runtime = require("react/jsx-runtime");
function useGroupedTransforms(possibleBlockTransformations) {
  const priorityContentTransformationBlocks = {
    "core/paragraph": 1,
    "core/heading": 2,
    "core/list": 3,
    "core/quote": 4
  };
  const transformations = (0, import_element.useMemo)(() => {
    const priorityTextTransformsNames = Object.keys(
      priorityContentTransformationBlocks
    );
    const groupedPossibleTransforms = possibleBlockTransformations.reduce(
      (accumulator, item) => {
        const { name } = item;
        if (priorityTextTransformsNames.includes(name)) {
          accumulator.priorityTextTransformations.push(item);
        } else {
          accumulator.restTransformations.push(item);
        }
        return accumulator;
      },
      { priorityTextTransformations: [], restTransformations: [] }
    );
    if (groupedPossibleTransforms.priorityTextTransformations.length === 1 && groupedPossibleTransforms.priorityTextTransformations[0].name === "core/quote") {
      const singleQuote = groupedPossibleTransforms.priorityTextTransformations.pop();
      groupedPossibleTransforms.restTransformations.push(singleQuote);
    }
    return groupedPossibleTransforms;
  }, [possibleBlockTransformations]);
  transformations.priorityTextTransformations.sort(
    ({ name: currentName }, { name: nextName }) => {
      return priorityContentTransformationBlocks[currentName] < priorityContentTransformationBlocks[nextName] ? -1 : 1;
    }
  );
  return transformations;
}
var BlockTransformationsMenu = ({
  className,
  possibleBlockTransformations,
  possibleBlockVariationTransformations,
  onSelect,
  onSelectVariation,
  blocks
}) => {
  const [hoveredTransformItem, setHoveredTransformItem] = (0, import_element.useState)();
  const { priorityTextTransformations, restTransformations } = useGroupedTransforms(possibleBlockTransformations);
  const hasBothContentTransformations = priorityTextTransformations.length && restTransformations.length;
  const restTransformItems = !!restTransformations.length && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    RestTransformationItems,
    {
      restTransformations,
      onSelect,
      setHoveredTransformItem
    }
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.MenuGroup, { label: (0, import_i18n.__)("Transform to"), className, children: [
      hoveredTransformItem && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_preview_block_popover.default,
        {
          blocks: (0, import_blocks.switchToBlockType)(
            blocks,
            hoveredTransformItem.name,
            hoveredTransformItem.variationName
          )
        }
      ),
      !!possibleBlockVariationTransformations?.length && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_block_variation_transformations.default,
        {
          transformations: possibleBlockVariationTransformations,
          blocks,
          onSelect: onSelectVariation
        }
      ),
      priorityTextTransformations.map((item) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        BlockTransformationItem,
        {
          item,
          onSelect,
          setHoveredTransformItem
        },
        item.id || item.name
      )),
      !hasBothContentTransformations && restTransformItems
    ] }),
    !!hasBothContentTransformations && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { className, children: restTransformItems })
  ] });
};
function RestTransformationItems({
  restTransformations,
  onSelect,
  setHoveredTransformItem
}) {
  return restTransformations.map((item) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    BlockTransformationItem,
    {
      item,
      onSelect,
      setHoveredTransformItem
    },
    item.id || item.name
  ));
}
function BlockTransformationItem({
  item,
  onSelect,
  setHoveredTransformItem
}) {
  const { name, icon, title, isDisabled } = item;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_components.MenuItem,
    {
      className: (0, import_blocks.getBlockMenuDefaultClassName)(name),
      onClick: (event) => {
        event.preventDefault();
        onSelect(name, item.variationName);
      },
      disabled: isDisabled,
      onMouseLeave: () => setHoveredTransformItem(null),
      onMouseEnter: () => setHoveredTransformItem(item),
      onFocus: () => setHoveredTransformItem(item),
      onBlur: () => setHoveredTransformItem(null),
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_icon.default, { icon, showColors: true }),
        title
      ]
    }
  );
}
var block_transformations_menu_default = BlockTransformationsMenu;
//# sourceMappingURL=block-transformations-menu.cjs.map
