"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _blockTypesList = _interopRequireDefault(require("../block-types-list"));
var _useClipboardBlock = _interopRequireDefault(require("./hooks/use-clipboard-block"));
var _useBlockTypeImpressions = _interopRequireDefault(require("./hooks/use-block-type-impressions"));
var _utils = require("./utils");
var _useBlockTypesState = _interopRequireDefault(require("./hooks/use-block-types-state"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const getBlockNamespace = item => item.name.split('/')[0];
function BlockTypesTab({
  onSelect,
  rootClientId,
  listProps
}) {
  const [rawBlockTypes,, collections] = (0, _useBlockTypesState.default)(rootClientId, onSelect, true);
  const clipboardBlock = (0, _useClipboardBlock.default)(rootClientId);
  const filteredBlockTypes = (0, _utils.filterInserterItems)(rawBlockTypes);
  const blockTypes = clipboardBlock ? [clipboardBlock, ...filteredBlockTypes] : filteredBlockTypes;
  const {
    items,
    trackBlockTypeSelected
  } = (0, _useBlockTypeImpressions.default)(blockTypes);
  const handleSelect = (...args) => {
    trackBlockTypeSelected(...args);
    onSelect(...args);
  };
  const collectionSections = (0, _element.useMemo)(() => {
    const result = [];
    Object.keys(collections).forEach(namespace => {
      const data = items.filter(item => getBlockNamespace(item) === namespace);
      if (data.length > 0) {
        result.push((0, _utils.createInserterSection)({
          key: `collection-${namespace}`,
          metadata: {
            icon: collections[namespace].icon,
            title: collections[namespace].title
          },
          items: data
        }));
      }
    });
    return result;
  }, [items, collections]);
  const sections = [(0, _utils.createInserterSection)({
    key: 'default',
    items
  }), ...collectionSections];
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockTypesList.default, {
    name: "Blocks",
    sections: sections,
    onSelect: handleSelect,
    listProps: listProps,
    label: (0, _i18n.__)('Blocks menu')
  });
}
var _default = exports.default = BlockTypesTab;
//# sourceMappingURL=block-types-tab.native.js.map