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