/* wp:polyfill */
"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 _i18n = require("@wordpress/i18n");
var _router = require("@wordpress/router");
var _url = require("@wordpress/url");
var _sidebarNavigationItem = _interopRequireDefault(require("../sidebar-navigation-item"));
var _hooks = require("../page-templates/hooks");
var _icons = require("@wordpress/icons");
var _constants = require("../../utils/constants");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
const EMPTY_ARRAY = [];
function TemplateDataviewItem({
  template,
  isActive
}) {
  const {
    text,
    icon
  } = (0, _hooks.useAddedBy)(template.type, template.id);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationItem.default, {
    to: (0, _url.addQueryArgs)('/template', {
      activeView: text
    }),
    icon: icon,
    "aria-current": isActive,
    children: text
  });
}
function DataviewsTemplatesSidebarContent() {
  const {
    query: {
      activeView = 'all'
    }
  } = useLocation();
  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, {
    className: "edit-site-sidebar-navigation-screen-templates-browse",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationItem.default, {
      to: "/template",
      icon: _icons.layout,
      "aria-current": activeView === 'all',
      children: (0, _i18n.__)('All templates')
    }), firstItemPerAuthorText.map(template => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(TemplateDataviewItem, {
        template: template,
        isActive: activeView === template.author_text
      }, template.author_text);
    })]
  });
}
//# sourceMappingURL=content.js.map