"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@wordpress/components");
var _blocks = require("@wordpress/blocks");
var _data = require("@wordpress/data");
var _draggableChip = _interopRequireDefault(require("../block-draggable/draggable-chip"));
var _utils = require("../inserter/block-patterns-tab/utils");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const InserterDraggableBlocks = ({
  isEnabled,
  blocks,
  icon,
  children,
  pattern
}) => {
  const transferData = {
    type: 'inserter',
    blocks
  };
  const blocksContainMedia = blocks.filter(block => (block.name === 'core/image' || block.name === 'core/audio' || block.name === 'core/video') && (block.attributes.url || block.attributes.src)).length > 0;
  const blockTypeIcon = (0, _data.useSelect)(select => {
    const {
      getBlockType
    } = select(_blocks.store);
    return blocks.length === 1 && getBlockType(blocks[0].name)?.icon;
  }, [blocks]);
  const {
    startDragging,
    stopDragging
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  if (!isEnabled) {
    return children({
      draggable: false,
      onDragStart: undefined,
      onDragEnd: undefined
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Draggable, {
    __experimentalTransferDataType: "wp-blocks",
    transferData: transferData,
    onDragStart: event => {
      startDragging();
      const parsedBlocks = pattern?.type === _utils.INSERTER_PATTERN_TYPES.user && pattern?.syncStatus !== 'unsynced' ? [(0, _blocks.createBlock)('core/block', {
        ref: pattern.id
      })] : blocks;
      event.dataTransfer.setData(blocksContainMedia ? 'default' : 'text/html', (0, _blocks.serialize)(parsedBlocks));
    },
    onDragEnd: () => {
      stopDragging();
    },
    __experimentalDragComponent: /*#__PURE__*/(0, _jsxRuntime.jsx)(_draggableChip.default, {
      count: blocks.length,
      icon: icon || !pattern && blockTypeIcon,
      isPattern: !!pattern
    }),
    children: ({
      onDraggableStart,
      onDraggableEnd
    }) => {
      return children({
        draggable: true,
        onDragStart: onDraggableStart,
        onDragEnd: onDraggableEnd
      });
    }
  });
};
var _default = exports.default = InserterDraggableBlocks;
//# sourceMappingURL=index.js.map