"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-types-tab.js
var block_types_tab_exports = {};
__export(block_types_tab_exports, {
  BlockTypesTab: () => BlockTypesTab,
  BlockTypesTabPanel: () => BlockTypesTabPanel,
  default: () => block_types_tab_default
});
module.exports = __toCommonJS(block_types_tab_exports);
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_block_types_list = __toESM(require("../block-types-list"));
var import_panel = __toESM(require("./panel"));
var import_use_block_types_state = __toESM(require("./hooks/use-block-types-state"));
var import_inserter_listbox = __toESM(require("../inserter-listbox"));
var import_sorting = require("../../utils/sorting");
var import_no_results = __toESM(require("./no-results"));
var import_jsx_runtime = require("react/jsx-runtime");
var getBlockNamespace = (item) => item.name.split("/")[0];
var MAX_SUGGESTED_ITEMS = 6;
var EMPTY_ARRAY = [];
function BlockTypesTabPanel({
  items,
  collections,
  categories,
  onSelectItem,
  onHover,
  showMostUsedBlocks,
  className
}) {
  const suggestedItems = (0, import_element.useMemo)(() => {
    return (0, import_sorting.orderBy)(items, "frecency", "desc").slice(
      0,
      MAX_SUGGESTED_ITEMS
    );
  }, [items]);
  const uncategorizedItems = (0, import_element.useMemo)(() => {
    return items.filter((item) => !item.category);
  }, [items]);
  const itemsPerCollection = (0, import_element.useMemo)(() => {
    const result = { ...collections };
    Object.keys(collections).forEach((namespace) => {
      result[namespace] = items.filter(
        (item) => getBlockNamespace(item) === namespace
      );
      if (result[namespace].length === 0) {
        delete result[namespace];
      }
    });
    return result;
  }, [items, collections]);
  (0, import_element.useEffect)(() => () => onHover(null), []);
  const currentlyRenderedCategories = (0, import_compose.useAsyncList)(categories);
  const didRenderAllCategories = categories.length === currentlyRenderedCategories.length;
  const collectionEntries = (0, import_element.useMemo)(() => {
    return Object.entries(collections);
  }, [collections]);
  const currentlyRenderedCollections = (0, import_compose.useAsyncList)(
    didRenderAllCategories ? collectionEntries : EMPTY_ARRAY
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className, children: [
    showMostUsedBlocks && // Only show the most used blocks if the total amount of block
    // is larger than 1 row, otherwise it is not so useful.
    items.length > 3 && !!suggestedItems.length && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_panel.default, { title: (0, import_i18n._x)("Most used", "blocks"), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_block_types_list.default,
      {
        items: suggestedItems,
        onSelect: onSelectItem,
        onHover,
        label: (0, import_i18n._x)("Most used", "blocks")
      }
    ) }),
    currentlyRenderedCategories.map((category) => {
      const categoryItems = items.filter(
        (item) => item.category === category.slug
      );
      if (!categoryItems || !categoryItems.length) {
        return null;
      }
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_panel.default,
        {
          title: category.title,
          icon: category.icon,
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_block_types_list.default,
            {
              items: categoryItems,
              onSelect: onSelectItem,
              onHover,
              label: category.title
            }
          )
        },
        category.slug
      );
    }),
    didRenderAllCategories && uncategorizedItems.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_panel.default,
      {
        className: "block-editor-inserter__uncategorized-blocks-panel",
        title: (0, import_i18n.__)("Uncategorized"),
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_block_types_list.default,
          {
            items: uncategorizedItems,
            onSelect: onSelectItem,
            onHover,
            label: (0, import_i18n.__)("Uncategorized")
          }
        )
      }
    ),
    currentlyRenderedCollections.map(
      ([namespace, collection]) => {
        const collectionItems = itemsPerCollection[namespace];
        if (!collectionItems || !collectionItems.length) {
          return null;
        }
        return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_panel.default,
          {
            title: collection.title,
            icon: collection.icon,
            children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_block_types_list.default,
              {
                items: collectionItems,
                onSelect: onSelectItem,
                onHover,
                label: collection.title
              }
            )
          },
          namespace
        );
      }
    )
  ] });
}
function BlockTypesTab({ rootClientId, onInsert, onHover, showMostUsedBlocks }, ref) {
  const [items, categories, collections, onSelectItem] = (0, import_use_block_types_state.default)(
    rootClientId,
    onInsert
  );
  if (!items.length) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_no_results.default, {});
  }
  const itemsForCurrentRoot = [];
  const itemsRemaining = [];
  for (const item of items) {
    if (item.category === "reusable") {
      continue;
    }
    if (item.isAllowedInCurrentRoot) {
      itemsForCurrentRoot.push(item);
    } else {
      itemsRemaining.push(item);
    }
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_inserter_listbox.default, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { ref, children: [
    !!itemsForCurrentRoot.length && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      BlockTypesTabPanel,
      {
        items: itemsForCurrentRoot,
        categories,
        collections,
        onSelectItem,
        onHover,
        showMostUsedBlocks,
        className: "block-editor-inserter__insertable-blocks-at-selection"
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      BlockTypesTabPanel,
      {
        items: itemsRemaining,
        categories,
        collections,
        onSelectItem,
        onHover,
        showMostUsedBlocks,
        className: "block-editor-inserter__all-blocks"
      }
    )
  ] }) });
}
var block_types_tab_default = (0, import_element.forwardRef)(BlockTypesTab);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BlockTypesTab,
  BlockTypesTabPanel
});
//# sourceMappingURL=block-types-tab.js.map
