"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _blocks = require("@wordpress/blocks");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _store = require("../../../store");
var _utils = require("../../../store/utils");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/**
 * Retrieves the block types inserter state.
 *
 * @param {string=}  rootClientId Insertion's root client ID.
 * @param {Function} onInsert     function called when inserter a list of blocks.
 * @param {boolean}  isQuick
 * @return {Array} Returns the block types state. (block types, categories, collections, onSelect handler)
 */
const useBlockTypesState = (rootClientId, onInsert, isQuick) => {
  const options = (0, _element.useMemo)(() => ({
    [_utils.withRootClientIdOptionKey]: !isQuick
  }), [isQuick]);
  const [items] = (0, _data.useSelect)(select => [select(_store.store).getInserterItems(rootClientId, options)], [rootClientId, options]);
  const [categories, collections] = (0, _data.useSelect)(select => {
    const {
      getCategories,
      getCollections
    } = select(_blocks.store);
    return [getCategories(), getCollections()];
  }, []);
  const onSelectItem = (0, _element.useCallback)(({
    name,
    initialAttributes,
    innerBlocks,
    syncStatus,
    content,
    rootClientId: _rootClientId
  }, shouldFocusBlock) => {
    const insertedBlock = syncStatus === 'unsynced' ? (0, _blocks.parse)(content, {
      __unstableSkipMigrationLogs: true
    }) : (0, _blocks.createBlock)(name, initialAttributes, (0, _blocks.createBlocksFromInnerBlocksTemplate)(innerBlocks));
    onInsert(insertedBlock, undefined, shouldFocusBlock, _rootClientId);
  }, [onInsert]);
  return [items, categories, collections, onSelectItem];
};
var _default = exports.default = useBlockTypesState;
//# sourceMappingURL=use-block-types-state.js.map