"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-patterns-state.js
var use_patterns_state_exports = {};
__export(use_patterns_state_exports, {
  default: () => use_patterns_state_default
});
module.exports = __toCommonJS(use_patterns_state_exports);
var import_element = require("@wordpress/element");
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_i18n = require("@wordpress/i18n");
var import_notices = require("@wordpress/notices");
var import_store = require("../../../store/index.cjs");
var import_lock_unlock = require("../../../lock-unlock.cjs");
var import_private_keys = require("../../../store/private-keys.cjs");
var import_utils = require("../block-patterns-tab/utils.cjs");
var import_utils2 = require("../../../store/utils.cjs");
var usePatternsState = (onInsert, rootClientId, selectedCategory, isQuick) => {
  const options = (0, import_element.useMemo)(
    () => ({ [import_utils2.isFiltered]: !!isQuick }),
    [isQuick]
  );
  const isWithinNavigationOverlayContext = (0, import_data.useSelect)((select) => {
    const { getSettings } = (0, import_lock_unlock.unlock)(select(import_store.store));
    const settings = getSettings();
    return settings[import_private_keys.isNavigationOverlayContextKey] ?? false;
  }, []);
  const { patternCategories, patterns, userPatternCategories } = (0, import_data.useSelect)(
    (select) => {
      const { getSettings, __experimentalGetAllowedPatterns } = (0, import_lock_unlock.unlock)(
        select(import_store.store)
      );
      const settings = getSettings();
      const userPatternCategoriesSelect = settings[import_private_keys.userPatternCategoriesSelectKey];
      return {
        patterns: __experimentalGetAllowedPatterns(
          rootClientId,
          options
        ),
        userPatternCategories: userPatternCategoriesSelect ? userPatternCategoriesSelect(select) : settings.__experimentalUserPatternCategories,
        patternCategories: settings.__experimentalBlockPatternCategories
      };
    },
    [rootClientId, options]
  );
  const filteredPatterns = (0, import_element.useMemo)(() => {
    return patterns.filter((pattern) => {
      const hasNavigationCategory = pattern.categories?.includes("navigation");
      if (hasNavigationCategory && !isWithinNavigationOverlayContext) {
        return false;
      }
      return true;
    });
  }, [patterns, isWithinNavigationOverlayContext]);
  const { getClosestAllowedInsertionPointForPattern } = (0, import_lock_unlock.unlock)(
    (0, import_data.useSelect)(import_store.store)
  );
  const allCategories = (0, import_element.useMemo)(() => {
    const categories = [...patternCategories];
    userPatternCategories?.forEach((userCategory) => {
      if (!categories.find(
        (existingCategory) => existingCategory.name === userCategory.name
      )) {
        categories.push(userCategory);
      }
    });
    return categories;
  }, [patternCategories, userPatternCategories]);
  const { createSuccessNotice } = (0, import_data.useDispatch)(import_notices.store);
  const onClickPattern = (0, import_element.useCallback)(
    (pattern, blocks) => {
      const destinationRootClientId = isQuick ? rootClientId : getClosestAllowedInsertionPointForPattern(
        pattern,
        rootClientId
      );
      if (destinationRootClientId === null) {
        return;
      }
      const patternBlocks = pattern.type === import_utils.INSERTER_PATTERN_TYPES.user && pattern.syncStatus !== "unsynced" ? [(0, import_blocks.createBlock)("core/block", { ref: pattern.id })] : blocks;
      onInsert(
        (patternBlocks ?? []).map((block) => {
          const clonedBlock = (0, import_blocks.cloneBlock)(block);
          if (clonedBlock.attributes.metadata?.categories?.includes(
            selectedCategory
          )) {
            clonedBlock.attributes.metadata.categories = [
              selectedCategory
            ];
          }
          return clonedBlock;
        }),
        pattern.name,
        false,
        destinationRootClientId
      );
      createSuccessNotice(
        (0, import_i18n.sprintf)(
          /* translators: %s: block pattern title. */
          (0, import_i18n.__)('Pattern "%s" inserted.'),
          pattern.title
        ),
        {
          type: "snackbar",
          id: "inserter-notice"
        }
      );
    },
    [
      createSuccessNotice,
      onInsert,
      selectedCategory,
      rootClientId,
      getClosestAllowedInsertionPointForPattern,
      isQuick
    ]
  );
  return [filteredPatterns, allCategories, onClickPattern];
};
var use_patterns_state_default = usePatternsState;
//# sourceMappingURL=use-patterns-state.cjs.map
