"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/inserter/block-patterns-tab/use-pattern-categories.js
var use_pattern_categories_exports = {};
__export(use_pattern_categories_exports, {
  usePatternCategories: () => usePatternCategories
});
module.exports = __toCommonJS(use_pattern_categories_exports);
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_a11y = require("@wordpress/a11y");
var import_use_patterns_state = __toESM(require("../hooks/use-patterns-state"));
var import_utils = require("./utils");
function hasRegisteredCategory(pattern, allCategories) {
  if (!pattern.categories || !pattern.categories.length) {
    return false;
  }
  return pattern.categories.some(
    (cat) => allCategories.some((category) => category.name === cat)
  );
}
function usePatternCategories(rootClientId, sourceFilter = "all") {
  const [patterns, allCategories] = (0, import_use_patterns_state.default)(
    void 0,
    rootClientId
  );
  const filteredPatterns = (0, import_element.useMemo)(
    () => sourceFilter === "all" ? patterns : patterns.filter(
      (pattern) => !(0, import_utils.isPatternFiltered)(pattern, sourceFilter)
    ),
    [sourceFilter, patterns]
  );
  const populatedCategories = (0, import_element.useMemo)(() => {
    const categories = allCategories.filter(
      (category) => filteredPatterns.some(
        (pattern) => pattern.categories?.includes(category.name)
      )
    ).sort((a, b) => a.label.localeCompare(b.label));
    if (filteredPatterns.some(
      (pattern) => !hasRegisteredCategory(pattern, allCategories)
    ) && !categories.find(
      (category) => category.name === "uncategorized"
    )) {
      categories.push({
        name: "uncategorized",
        label: (0, import_i18n._x)("Uncategorized")
      });
    }
    if (filteredPatterns.some(
      (pattern) => pattern.blockTypes?.includes("core/post-content")
    )) {
      categories.unshift(import_utils.starterPatternsCategory);
    }
    if (filteredPatterns.some(
      (pattern) => pattern.type === import_utils.INSERTER_PATTERN_TYPES.user
    )) {
      categories.unshift(import_utils.myPatternsCategory);
    }
    if (filteredPatterns.length > 0) {
      categories.unshift({
        name: import_utils.allPatternsCategory.name,
        label: import_utils.allPatternsCategory.label
      });
    }
    (0, import_a11y.speak)(
      (0, import_i18n.sprintf)(
        /* translators: %d: number of categories . */
        (0, import_i18n._n)(
          "%d category button displayed.",
          "%d category buttons displayed.",
          categories.length
        ),
        categories.length
      )
    );
    return categories;
  }, [allCategories, filteredPatterns]);
  return populatedCategories;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usePatternCategories
});
//# sourceMappingURL=use-pattern-categories.js.map
