/* wp:polyfill */
/**
 * WordPress dependencies
 */
import { useEntityRecords } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { __experimentalItemGroup as ItemGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { addQueryArgs } from '@wordpress/url';

/**
 * Internal dependencies
 */
import SidebarNavigationItem from '../sidebar-navigation-item';
import { useAddedBy } from '../page-templates/hooks';
import { layout } from '@wordpress/icons';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const {
  useLocation
} = unlock(routerPrivateApis);
const EMPTY_ARRAY = [];
function TemplateDataviewItem({
  template,
  isActive
}) {
  const {
    text,
    icon
  } = useAddedBy(template.type, template.id);
  return /*#__PURE__*/_jsx(SidebarNavigationItem, {
    to: addQueryArgs('/template', {
      activeView: text
    }),
    icon: icon,
    "aria-current": isActive,
    children: text
  });
}
export default function DataviewsTemplatesSidebarContent() {
  const {
    query: {
      activeView = 'all'
    }
  } = useLocation();
  const {
    records
  } = useEntityRecords('postType', TEMPLATE_POST_TYPE, {
    per_page: -1
  });
  const firstItemPerAuthorText = 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__*/_jsxs(ItemGroup, {
    className: "edit-site-sidebar-navigation-screen-templates-browse",
    children: [/*#__PURE__*/_jsx(SidebarNavigationItem, {
      to: "/template",
      icon: layout,
      "aria-current": activeView === 'all',
      children: __('All templates')
    }), firstItemPerAuthorText.map(template => {
      return /*#__PURE__*/_jsx(TemplateDataviewItem, {
        template: template,
        isActive: activeView === template.author_text
      }, template.author_text);
    })]
  });
}
//# sourceMappingURL=content.js.map