"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useTemplatePartAreas;
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _constants = require("../../utils/constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const useTemplatePartsGroupedByArea = items => {
  const allItems = items || [];
  const templatePartAreas = (0, _data.useSelect)(select => select(_coreData.store).getCurrentTheme()?.default_template_part_areas || [], []);

  // Create map of template areas ensuring that default areas are displayed before
  // any custom registered template part areas.
  const knownAreas = {
    header: {},
    footer: {},
    sidebar: {},
    uncategorized: {}
  };
  templatePartAreas.forEach(templatePartArea => knownAreas[templatePartArea.area] = {
    ...templatePartArea,
    templateParts: []
  });
  const groupedByArea = allItems.reduce((accumulator, item) => {
    const key = accumulator[item.area] ? item.area : _constants.TEMPLATE_PART_AREA_DEFAULT_CATEGORY;
    accumulator[key]?.templateParts?.push(item);
    return accumulator;
  }, knownAreas);
  return groupedByArea;
};
function useTemplatePartAreas() {
  const {
    records: templateParts,
    isResolving: isLoading
  } = (0, _coreData.useEntityRecords)('postType', _constants.TEMPLATE_PART_POST_TYPE, {
    per_page: -1
  });
  return {
    hasTemplateParts: templateParts ? !!templateParts.length : false,
    isLoading,
    templatePartAreas: useTemplatePartsGroupedByArea(templateParts)
  };
}
//# sourceMappingURL=use-template-part-areas.js.map