"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = DataviewsTemplatesSidebarContent;
var _coreData = require("@wordpress/core-data");
var _element = require("@wordpress/element");
var _components = require("@wordpress/components");
var _dataviewItem = _interopRequireDefault(require("../sidebar-dataviews/dataview-item"));
var _hooks = require("../page-templates/hooks");
var _icons = require("@wordpress/icons");
var _constants = require("../../utils/constants");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const EMPTY_ARRAY = [];
function TemplateDataviewItem({
  template,
  isActive
}) {
  const {
    text,
    icon
  } = (0, _hooks.useAddedBy)(template.type, template.id);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dataviewItem.default, {
    slug: text,
    title: text,
    icon: icon,
    isActive: isActive,
    isCustom: false
  }, text);
}
function DataviewsTemplatesSidebarContent({
  activeView,
  title
}) {
  const {
    records
  } = (0, _coreData.useEntityRecords)('postType', _constants.TEMPLATE_POST_TYPE, {
    per_page: -1
  });
  const firstItemPerAuthorText = (0, _element.useMemo)(() => {
    var _ref;
    const firstItemPerAuthor = records?.reduce((acc, template) => {
      const author = template.author_text;
      if (author && !acc[author]) {
        acc[author] = template;
      }
      return acc;
    }, {});
    return (_ref = firstItemPerAuthor && Object.values(firstItemPerAuthor)) !== null && _ref !== void 0 ? _ref : EMPTY_ARRAY;
  }, [records]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalItemGroup, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_dataviewItem.default, {
      slug: "all",
      title: title,
      icon: _icons.layout,
      isActive: activeView === 'all',
      isCustom: false
    }), firstItemPerAuthorText.map(template => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(TemplateDataviewItem, {
        template: template,
        isActive: activeView === template.author_text
      }, template.author_text);
    })]
  });
}
//# sourceMappingURL=content.js.map