"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
 */
// @wordpress/blocks imports are not typed.
// @ts-expect-error

/**
 * 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) {
  var _categoryDefinition$s;
  if (!categoryDefinition?.slug || !examples?.length) {
    return;
  }
  const categories = (_categoryDefinition$s = categoryDefinition?.subcategories) !== null && _categoryDefinition$s !== void 0 ? _categoryDefinition$s : [];
  if (categories.length) {
    return categories.reduce((acc, subcategoryDefinition) => {
      const subcategoryExamples = getExamplesByCategory(subcategoryDefinition, examples);
      if (subcategoryExamples) {
        if (!acc.subcategories) {
          acc.subcategories = [];
        }
        acc.subcategories = [...acc.subcategories, subcategoryExamples];
      }
      return acc;
    }, {
      title: categoryDefinition.title,
      slug: categoryDefinition.slug
    });
  }
  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)();
  const extraCategoriesFiltered = extraCategories.filter(({
    slug
  }) => !reservedCategories.includes(slug));
  return [..._constants.STYLE_BOOK_CATEGORIES, ...extraCategoriesFiltered];
}
//# sourceMappingURL=categories.js.map