// packages/block-editor/src/components/inserter-draggable-blocks/index.js
import { Draggable } from "@wordpress/components";
import { createBlock, store as blocksStore } from "@wordpress/blocks";
import { useDispatch, useSelect } from "@wordpress/data";
import { useMemo } from "@wordpress/element";
import BlockDraggableChip from "../block-draggable/draggable-chip";
import { INSERTER_PATTERN_TYPES } from "../inserter/block-patterns-tab/utils";
import { store as blockEditorStore } from "../../store";
import { unlock } from "../../lock-unlock";
import { jsx } from "react/jsx-runtime";
var InserterDraggableBlocks = ({
  isEnabled,
  blocks,
  icon,
  children,
  pattern
}) => {
  const blockTypeIcon = useSelect(
    (select) => {
      const { getBlockType } = select(blocksStore);
      return blocks.length === 1 && getBlockType(blocks[0].name)?.icon;
    },
    [blocks]
  );
  const { startDragging, stopDragging } = unlock(
    useDispatch(blockEditorStore)
  );
  const patternBlock = useMemo(() => {
    return pattern?.type === INSERTER_PATTERN_TYPES.user && pattern?.syncStatus !== "unsynced" ? [createBlock("core/block", { ref: pattern.id })] : void 0;
  }, [pattern?.type, pattern?.syncStatus, pattern?.id]);
  if (!isEnabled) {
    return children({
      draggable: false,
      onDragStart: void 0,
      onDragEnd: void 0
    });
  }
  const draggableBlocks = patternBlock ?? blocks;
  return /* @__PURE__ */ jsx(
    Draggable,
    {
      __experimentalTransferDataType: "wp-blocks",
      transferData: { type: "inserter", blocks: draggableBlocks },
      onDragStart: (event) => {
        startDragging();
        for (const block of draggableBlocks) {
          const type = `wp-block:${block.name}`;
          event.dataTransfer.items.add("", type);
        }
      },
      onDragEnd: () => {
        stopDragging();
      },
      __experimentalDragComponent: /* @__PURE__ */ jsx(
        BlockDraggableChip,
        {
          count: blocks.length,
          icon: icon || !pattern && blockTypeIcon,
          isPattern: !!pattern
        }
      ),
      children: ({ onDraggableStart, onDraggableEnd }) => {
        return children({
          draggable: true,
          onDragStart: onDraggableStart,
          onDragEnd: onDraggableEnd
        });
      }
    }
  );
};
var inserter_draggable_blocks_default = InserterDraggableBlocks;
export {
  inserter_draggable_blocks_default as default
};
//# sourceMappingURL=index.js.map
