/* wp:polyfill */
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getExamplesByCategory = getExamplesByCategory;
exports.getTopLevelStyleBookCategories = getTopLevelStyleBookCategories;
var _blocks = require("@wordpress/blocks");
var _constants = require("./constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/**
 * Returns category examples for a given category definition and list of examples.
 * @param {StyleBookCategory} categoryDefinition The category definition.
 * @param {BlockExample[]}    examples           An array of block examples.
 * @return {CategoryExamples|undefined} An object containing the category examples.
 */
function getExamplesByCategory(categoryDefinition, examples) {
  if (!categoryDefinition?.slug || !examples?.length) {
    return;
  }
  if (categoryDefinition?.subcategories?.length) {
    return categoryDefinition.subcategories.reduce((acc, subcategoryDefinition) => {
      const subcategoryExamples = getExamplesByCategory(subcategoryDefinition, examples);
      if (subcategoryExamples) {
        acc.subcategories = [...acc.subcategories, subcategoryExamples];
      }
      return acc;
    }, {
      title: categoryDefinition.title,
      slug: categoryDefinition.slug,
      subcategories: []
    });
  }
  const blocksToInclude = categoryDefinition?.blocks || [];
  const blocksToExclude = categoryDefinition?.exclude || [];
  const categoryExamples = examples.filter(example => {
    return !blocksToExclude.includes(example.name) && (example.category === categoryDefinition.slug || blocksToInclude.includes(example.name));
  });
  if (!categoryExamples.length) {
    return;
  }
  return {
    title: categoryDefinition.title,
    slug: categoryDefinition.slug,
    examples: categoryExamples
  };
}

/**
 * Returns category examples for a given category definition and list of examples.
 *
 * @return {StyleBookCategory[]} An array of top-level category definitions.
 */
function getTopLevelStyleBookCategories() {
  const reservedCategories = [..._constants.STYLE_BOOK_THEME_SUBCATEGORIES, ..._constants.STYLE_BOOK_CATEGORIES].map(({
    slug
  }) => slug);
  const extraCategories = (0, _blocks.getCategories)().filter(({
    slug
  }) => !reservedCategories.includes(slug));
  return [..._constants.STYLE_BOOK_CATEGORIES, ...extraCategories];
}
//# sourceMappingURL=categories.js.map