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

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = usePatternCategories;
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _useDefaultPatternCategories = _interopRequireDefault(require("./use-default-pattern-categories"));
var _useThemePatterns = _interopRequireDefault(require("./use-theme-patterns"));
var _usePatterns = _interopRequireDefault(require("../page-patterns/use-patterns"));
var _constants = require("../../utils/constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function usePatternCategories() {
  const defaultCategories = (0, _useDefaultPatternCategories.default)();
  defaultCategories.push({
    name: _constants.TEMPLATE_PART_AREA_DEFAULT_CATEGORY,
    label: (0, _i18n.__)('Uncategorized')
  });
  const themePatterns = (0, _useThemePatterns.default)();
  const {
    patterns: userPatterns,
    categories: userPatternCategories
  } = (0, _usePatterns.default)(_constants.PATTERN_TYPES.user);
  const patternCategories = (0, _element.useMemo)(() => {
    const categoryMap = {};
    const categoriesWithCounts = [];

    // Create a map for easier counting of patterns in categories.
    defaultCategories.forEach(category => {
      if (!categoryMap[category.name]) {
        categoryMap[category.name] = {
          ...category,
          count: 0
        };
      }
    });
    userPatternCategories.forEach(category => {
      if (!categoryMap[category.name]) {
        categoryMap[category.name] = {
          ...category,
          count: 0
        };
      }
    });

    // Update the category counts to reflect theme registered patterns.
    themePatterns.forEach(pattern => {
      pattern.categories?.forEach(category => {
        if (categoryMap[category]) {
          categoryMap[category].count += 1;
        }
      });
      // If the pattern has no categories, add it to uncategorized.
      if (!pattern.categories?.length) {
        categoryMap.uncategorized.count += 1;
      }
    });

    // Update the category counts to reflect user registered patterns.
    userPatterns.forEach(pattern => {
      pattern.wp_pattern_category?.forEach(catId => {
        const category = userPatternCategories.find(cat => cat.id === catId)?.name;
        if (categoryMap[category]) {
          categoryMap[category].count += 1;
        }
      });
      // If the pattern has no categories, add it to uncategorized.
      if (!pattern.wp_pattern_category?.length || !pattern.wp_pattern_category?.some(catId => userPatternCategories.find(cat => cat.id === catId))) {
        categoryMap.uncategorized.count += 1;
      }
    });

    // Filter categories so we only have those containing patterns.
    [...defaultCategories, ...userPatternCategories].forEach(category => {
      if (categoryMap[category.name].count && !categoriesWithCounts.find(cat => cat.name === category.name)) {
        categoriesWithCounts.push(categoryMap[category.name]);
      }
    });
    const sortedCategories = categoriesWithCounts.sort((a, b) => a.label.localeCompare(b.label));
    sortedCategories.unshift({
      name: _constants.PATTERN_USER_CATEGORY,
      label: (0, _i18n.__)('My patterns'),
      count: userPatterns.length
    });
    sortedCategories.unshift({
      name: _constants.PATTERN_DEFAULT_CATEGORY,
      label: (0, _i18n.__)('All patterns'),
      description: (0, _i18n.__)('A list of all patterns from all sources.'),
      count: themePatterns.length + userPatterns.length
    });
    return sortedCategories;
  }, [defaultCategories, themePatterns, userPatternCategories, userPatterns]);
  return {
    patternCategories,
    hasPatterns: !!patternCategories.length
  };
}
//# sourceMappingURL=use-pattern-categories.js.map