// packages/block-editor/src/components/block-switcher/block-transformations-menu.js
import { __ } from "@wordpress/i18n";
import { MenuGroup, MenuItem } from "@wordpress/components";
import {
  getBlockMenuDefaultClassName,
  switchToBlockType
} from "@wordpress/blocks";
import { useState, useMemo } from "@wordpress/element";
import BlockIcon from "../block-icon/index.mjs";
import PreviewBlockPopover from "./preview-block-popover.mjs";
import BlockVariationTransformations from "./block-variation-transformations.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function useGroupedTransforms(possibleBlockTransformations) {
  const priorityContentTransformationBlocks = {
    "core/paragraph": 1,
    "core/heading": 2,
    "core/list": 3,
    "core/quote": 4
  };
  const transformations = 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] = useState();
  const { priorityTextTransformations, restTransformations } = useGroupedTransforms(possibleBlockTransformations);
  const hasBothContentTransformations = priorityTextTransformations.length && restTransformations.length;
  const restTransformItems = !!restTransformations.length && /* @__PURE__ */ jsx(
    RestTransformationItems,
    {
      restTransformations,
      onSelect,
      setHoveredTransformItem
    }
  );
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs(MenuGroup, { label: __("Transform to"), className, children: [
      hoveredTransformItem && /* @__PURE__ */ jsx(
        PreviewBlockPopover,
        {
          blocks: switchToBlockType(
            blocks,
            hoveredTransformItem.name,
            hoveredTransformItem.variationName
          )
        }
      ),
      !!possibleBlockVariationTransformations?.length && /* @__PURE__ */ jsx(
        BlockVariationTransformations,
        {
          transformations: possibleBlockVariationTransformations,
          blocks,
          onSelect: onSelectVariation
        }
      ),
      priorityTextTransformations.map((item) => /* @__PURE__ */ jsx(
        BlockTransformationItem,
        {
          item,
          onSelect,
          setHoveredTransformItem
        },
        item.id || item.name
      )),
      !hasBothContentTransformations && restTransformItems
    ] }),
    !!hasBothContentTransformations && /* @__PURE__ */ jsx(MenuGroup, { className, children: restTransformItems })
  ] });
};
function RestTransformationItems({
  restTransformations,
  onSelect,
  setHoveredTransformItem
}) {
  return restTransformations.map((item) => /* @__PURE__ */ jsx(
    BlockTransformationItem,
    {
      item,
      onSelect,
      setHoveredTransformItem
    },
    item.id || item.name
  ));
}
function BlockTransformationItem({
  item,
  onSelect,
  setHoveredTransformItem
}) {
  const { name, icon, title, isDisabled } = item;
  return /* @__PURE__ */ jsxs(
    MenuItem,
    {
      className: 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__ */ jsx(BlockIcon, { icon, showColors: true }),
        title
      ]
    }
  );
}
var block_transformations_menu_default = BlockTransformationsMenu;
export {
  block_transformations_menu_default as default
};
//# sourceMappingURL=block-transformations-menu.mjs.map
