"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/inserter/hooks/use-block-types-state.js
var use_block_types_state_exports = {};
__export(use_block_types_state_exports, {
  default: () => use_block_types_state_default
});
module.exports = __toCommonJS(use_block_types_state_exports);
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_notices = require("@wordpress/notices");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../../../store");
var import_utils = require("../../../store/utils");
var import_lock_unlock = require("../../../lock-unlock");
var useBlockTypesState = (rootClientId, onInsert, isQuick) => {
  const options = (0, import_element.useMemo)(
    () => ({ [import_utils.isFiltered]: !!isQuick }),
    [isQuick]
  );
  const [items] = (0, import_data.useSelect)(
    (select) => [
      select(import_store.store).getInserterItems(
        rootClientId,
        options
      )
    ],
    [rootClientId, options]
  );
  const { getClosestAllowedInsertionPoint } = (0, import_lock_unlock.unlock)(
    (0, import_data.useSelect)(import_store.store)
  );
  const { createErrorNotice } = (0, import_data.useDispatch)(import_notices.store);
  const [categories, collections] = (0, import_data.useSelect)((select) => {
    const { getCategories, getCollections } = select(import_blocks.store);
    return [getCategories(), getCollections()];
  }, []);
  const onSelectItem = (0, import_element.useCallback)(
    ({ name, initialAttributes, innerBlocks, syncStatus, content }, shouldFocusBlock) => {
      const destinationClientId = getClosestAllowedInsertionPoint(
        name,
        rootClientId
      );
      if (destinationClientId === null) {
        const title = (0, import_blocks.getBlockType)(name)?.title ?? name;
        createErrorNotice(
          (0, import_i18n.sprintf)(
            /* translators: %s: block pattern title. */
            (0, import_i18n.__)(`Block "%s" can't be inserted.`),
            title
          ),
          {
            type: "snackbar",
            id: "inserter-notice"
          }
        );
        return;
      }
      const insertedBlock = syncStatus === "unsynced" ? (0, import_blocks.parse)(content, {
        __unstableSkipMigrationLogs: true
      }) : (0, import_blocks.createBlock)(
        name,
        initialAttributes,
        (0, import_blocks.createBlocksFromInnerBlocksTemplate)(innerBlocks)
      );
      onInsert(
        insertedBlock,
        void 0,
        shouldFocusBlock,
        destinationClientId
      );
    },
    [
      getClosestAllowedInsertionPoint,
      rootClientId,
      onInsert,
      createErrorNotice
    ]
  );
  return [items, categories, collections, onSelectItem];
};
var use_block_types_state_default = useBlockTypesState;
//# sourceMappingURL=use-block-types-state.js.map
