"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = BlockManager;
var _blocks = require("@wordpress/blocks");
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _compose = require("@wordpress/compose");
var _a11y = require("@wordpress/a11y");
var _preferences = require("@wordpress/preferences");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
var _category = _interopRequireDefault(require("./category"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function BlockManager() {
  const debouncedSpeak = (0, _compose.useDebounce)(_a11y.speak, 500);
  const [search, setSearch] = (0, _element.useState)('');
  const {
    showBlockTypes
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const {
    blockTypes,
    categories,
    hasBlockSupport,
    isMatchingSearchTerm,
    numberOfHiddenBlocks
  } = (0, _data.useSelect)(select => {
    var _select$get;
    // Some hidden blocks become unregistered
    // by removing for instance the plugin that registered them, yet
    // they're still remain as hidden by the user's action.
    // We consider "hidden", blocks which were hidden and
    // are still registered.
    const _blockTypes = select(_blocks.store).getBlockTypes();
    const hiddenBlockTypes = ((_select$get = select(_preferences.store).get('core', 'hiddenBlockTypes')) !== null && _select$get !== void 0 ? _select$get : []).filter(hiddenBlock => {
      return _blockTypes.some(registeredBlock => registeredBlock.name === hiddenBlock);
    });
    return {
      blockTypes: _blockTypes,
      categories: select(_blocks.store).getCategories(),
      hasBlockSupport: select(_blocks.store).hasBlockSupport,
      isMatchingSearchTerm: select(_blocks.store).isMatchingSearchTerm,
      numberOfHiddenBlocks: Array.isArray(hiddenBlockTypes) && hiddenBlockTypes.length
    };
  }, []);
  function enableAllBlockTypes(newBlockTypes) {
    const blockNames = newBlockTypes.map(({
      name
    }) => name);
    showBlockTypes(blockNames);
  }
  const filteredBlockTypes = blockTypes.filter(blockType => hasBlockSupport(blockType, 'inserter', true) && (!search || isMatchingSearchTerm(blockType, search)) && (!blockType.parent || blockType.parent.includes('core/post-content')));

  // Announce search results on change
  (0, _element.useEffect)(() => {
    if (!search) {
      return;
    }
    const count = filteredBlockTypes.length;
    const resultsFoundMessage = (0, _i18n.sprintf)( /* translators: %d: number of results. */
    (0, _i18n._n)('%d result found.', '%d results found.', count), count);
    debouncedSpeak(resultsFoundMessage);
  }, [filteredBlockTypes?.length, search, debouncedSpeak]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "editor-block-manager__content",
    children: [!!numberOfHiddenBlocks && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "editor-block-manager__disabled-blocks-count",
      children: [(0, _i18n.sprintf)( /* translators: %d: number of blocks. */
      (0, _i18n._n)('%d block is hidden.', '%d blocks are hidden.', numberOfHiddenBlocks), numberOfHiddenBlocks), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
        __next40pxDefaultSize: true,
        variant: "link",
        onClick: () => enableAllBlockTypes(filteredBlockTypes),
        children: (0, _i18n.__)('Reset')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SearchControl, {
      __nextHasNoMarginBottom: true,
      label: (0, _i18n.__)('Search for a block'),
      placeholder: (0, _i18n.__)('Search for a block'),
      value: search,
      onChange: nextSearch => setSearch(nextSearch),
      className: "editor-block-manager__search"
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      tabIndex: "0",
      role: "region",
      "aria-label": (0, _i18n.__)('Available block types'),
      className: "editor-block-manager__results",
      children: [filteredBlockTypes.length === 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        className: "editor-block-manager__no-results",
        children: (0, _i18n.__)('No blocks found.')
      }), categories.map(category => /*#__PURE__*/(0, _jsxRuntime.jsx)(_category.default, {
        title: category.title,
        blockTypes: filteredBlockTypes.filter(blockType => blockType.category === category.slug)
      }, category.slug)), /*#__PURE__*/(0, _jsxRuntime.jsx)(_category.default, {
        title: (0, _i18n.__)('Uncategorized'),
        blockTypes: filteredBlockTypes.filter(({
          category
        }) => !category)
      })]
    })]
  });
}
//# sourceMappingURL=index.js.map