// packages/block-editor/src/components/use-block-commands/index.js
import { __, sprintf } from "@wordpress/i18n";
import {
  hasBlockSupport,
  store as blocksStore,
  switchToBlockType,
  isTemplatePart
} from "@wordpress/blocks";
import { useSelect, useDispatch } from "@wordpress/data";
import { useCommandLoader } from "@wordpress/commands";
import {
  copy,
  trash as remove,
  plus as add,
  group,
  ungroup,
  seen,
  unseen,
  blockDefault as blockDefaultIcon
} from "@wordpress/icons";
import { store as blockEditorStore } from "../../store/index.mjs";
import { unlock } from "../../lock-unlock.mjs";
var getTransformCommands = () => function useTransformCommands() {
  const { replaceBlocks, multiSelect } = useDispatch(blockEditorStore);
  const {
    blocks,
    clientIds,
    canRemove,
    possibleBlockTransformations,
    invalidSelection
  } = useSelect((select) => {
    const {
      getBlockRootClientId,
      getBlockTransformItems,
      getSelectedBlockClientIds,
      getBlocksByClientId,
      canRemoveBlocks
    } = select(blockEditorStore);
    const selectedBlockClientIds = getSelectedBlockClientIds();
    const selectedBlocks = getBlocksByClientId(
      selectedBlockClientIds
    );
    if (selectedBlocks.filter((block) => !block).length > 0) {
      return {
        invalidSelection: true
      };
    }
    const rootClientId = getBlockRootClientId(
      selectedBlockClientIds[0]
    );
    return {
      blocks: selectedBlocks,
      clientIds: selectedBlockClientIds,
      possibleBlockTransformations: getBlockTransformItems(
        selectedBlocks,
        rootClientId
      ),
      canRemove: canRemoveBlocks(selectedBlockClientIds),
      invalidSelection: false
    };
  }, []);
  if (invalidSelection) {
    return {
      isLoading: false,
      commands: []
    };
  }
  const isTemplate = blocks.length === 1 && isTemplatePart(blocks[0]);
  function selectForMultipleBlocks(insertedBlocks) {
    if (insertedBlocks.length > 1) {
      multiSelect(
        insertedBlocks[0].clientId,
        insertedBlocks[insertedBlocks.length - 1].clientId
      );
    }
  }
  function onBlockTransform(name, variationName) {
    const newBlocks = switchToBlockType(blocks, name, variationName);
    replaceBlocks(clientIds, newBlocks);
    selectForMultipleBlocks(newBlocks);
  }
  const hasPossibleBlockTransformations = !!possibleBlockTransformations.length && canRemove && !isTemplate;
  if (!clientIds || clientIds.length < 1 || !hasPossibleBlockTransformations) {
    return { isLoading: false, commands: [] };
  }
  const commands = possibleBlockTransformations.map(
    (transformation) => {
      const { id, name, title, icon, variationName } = transformation;
      const blockIcon = !icon?.src || icon?.src === "block-default" ? {
        src: blockDefaultIcon
      } : icon;
      return {
        name: "core/block-editor/transform-to-" + (id || name).replace(/\//g, "-"),
        /* translators: %s: Block or block variation name. */
        label: sprintf(__("Transform to %s"), title),
        icon: blockIcon?.src,
        category: "command",
        callback: ({ close }) => {
          onBlockTransform(name, variationName);
          close();
        }
      };
    }
  );
  return { isLoading: false, commands };
};
var getQuickActionsCommands = () => function useQuickActionsCommands() {
  const { clientIds, isUngroupable, isGroupable } = useSelect(
    (select) => {
      const {
        getSelectedBlockClientIds,
        isUngroupable: _isUngroupable,
        isGroupable: _isGroupable
      } = select(blockEditorStore);
      const selectedBlockClientIds = getSelectedBlockClientIds();
      return {
        clientIds: selectedBlockClientIds,
        isUngroupable: _isUngroupable(),
        isGroupable: _isGroupable()
      };
    },
    []
  );
  const {
    canInsertBlockType,
    getBlockRootClientId,
    getBlocksByClientId,
    canRemoveBlocks,
    isBlockHiddenAnywhere
  } = unlock(useSelect(blockEditorStore));
  const { getBlockEditingMode } = useSelect(blockEditorStore);
  const { getDefaultBlockName, getGroupingBlockName } = useSelect(blocksStore);
  const blocks = getBlocksByClientId(clientIds);
  const blockEditorDispatch = useDispatch(blockEditorStore);
  const {
    removeBlocks,
    replaceBlocks,
    duplicateBlocks,
    insertAfterBlock,
    insertBeforeBlock
  } = blockEditorDispatch;
  const onGroup = () => {
    if (!blocks.length) {
      return;
    }
    const groupingBlockName = getGroupingBlockName();
    const newBlocks = switchToBlockType(blocks, groupingBlockName);
    if (!newBlocks) {
      return;
    }
    replaceBlocks(clientIds, newBlocks);
  };
  const onUngroup = () => {
    if (!blocks.length) {
      return;
    }
    const innerBlocks = blocks[0].innerBlocks;
    if (!innerBlocks.length) {
      return;
    }
    replaceBlocks(clientIds, innerBlocks);
  };
  if (!clientIds || clientIds.length < 1) {
    return { isLoading: false, commands: [] };
  }
  const { showViewportModal } = unlock(blockEditorDispatch);
  const rootClientId = getBlockRootClientId(clientIds[0]);
  const canInsertDefaultBlock = canInsertBlockType(
    getDefaultBlockName(),
    rootClientId
  );
  const canDuplicate = blocks.every((block) => {
    return !!block && hasBlockSupport(block.name, "multiple", true) && canInsertBlockType(block.name, rootClientId);
  });
  const canRemove = canRemoveBlocks(clientIds);
  const commands = [];
  if (canDuplicate) {
    commands.push({
      name: "duplicate",
      label: __("Duplicate"),
      callback: () => duplicateBlocks(clientIds, true),
      icon: copy
    });
  }
  if (canInsertDefaultBlock) {
    commands.push(
      {
        name: "add-before",
        label: __("Add before"),
        callback: () => {
          const clientId = Array.isArray(clientIds) ? clientIds[0] : clientId;
          insertBeforeBlock(clientId);
        },
        icon: add
      },
      {
        name: "add-after",
        label: __("Add after"),
        callback: () => {
          const clientId = Array.isArray(clientIds) ? clientIds[clientIds.length - 1] : clientId;
          insertAfterBlock(clientId);
        },
        icon: add
      }
    );
  }
  if (isGroupable) {
    commands.push({
      name: "Group",
      label: __("Group"),
      callback: onGroup,
      icon: group
    });
  }
  if (isUngroupable) {
    commands.push({
      name: "ungroup",
      label: __("Ungroup"),
      callback: onUngroup,
      icon: ungroup
    });
  }
  if (canRemove) {
    commands.push({
      name: "remove",
      label: __("Delete"),
      callback: () => removeBlocks(clientIds, true),
      icon: remove
    });
  }
  const supportsVisibility = blocks.every(
    (block) => !!block && hasBlockSupport(block.name, "visibility", true)
  );
  const allBlocksDefaultMode = clientIds.every(
    (id) => getBlockEditingMode(id) === "default"
  );
  if (supportsVisibility && allBlocksDefaultMode) {
    const hasHiddenBlock = clientIds.some(
      (id) => isBlockHiddenAnywhere(id)
    );
    commands.push({
      name: "toggle-visibility",
      label: hasHiddenBlock ? __("Show") : __("Hide"),
      callback: () => showViewportModal(clientIds),
      icon: hasHiddenBlock ? seen : unseen
    });
  }
  return {
    isLoading: false,
    commands: commands.map((command) => ({
      ...command,
      name: "core/block-editor/action-" + command.name,
      category: "command",
      callback: ({ close }) => {
        command.callback();
        close();
      }
    }))
  };
};
var useBlockCommands = () => {
  useCommandLoader({
    name: "core/block-editor/blockTransforms",
    hook: getTransformCommands()
  });
  useCommandLoader({
    name: "core/block-editor/blockQuickActions",
    hook: getQuickActionsCommands(),
    context: "block-selection-edit"
  });
};
export {
  useBlockCommands
};
//# sourceMappingURL=index.mjs.map
