"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SidebarNavigationScreenPatterns;
var _components = require("@wordpress/components");
var _editor = require("@wordpress/editor");
var _i18n = require("@wordpress/i18n");
var _icons = require("@wordpress/icons");
var _router = require("@wordpress/router");
var _sidebarNavigationScreen = _interopRequireDefault(require("../sidebar-navigation-screen"));
var _categoryItem = _interopRequireDefault(require("./category-item"));
var _constants = require("../../utils/constants");
var _usePatternCategories = _interopRequireDefault(require("./use-pattern-categories"));
var _useTemplatePartAreas = _interopRequireDefault(require("./use-template-part-areas"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
function CategoriesGroup({
  templatePartAreas,
  patternCategories,
  currentCategory,
  currentType
}) {
  const [allPatterns, ...otherPatterns] = patternCategories;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalItemGroup, {
    className: "edit-site-sidebar-navigation-screen-patterns__group",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_categoryItem.default, {
      count: Object.values(templatePartAreas).map(({
        templateParts
      }) => templateParts?.length || 0).reduce((acc, val) => acc + val, 0),
      icon: (0, _editor.getTemplatePartIcon)() /* no name, so it provides the fallback icon */,
      label: (0, _i18n.__)('All template parts'),
      id: _constants.TEMPLATE_PART_ALL_AREAS_CATEGORY,
      type: _constants.TEMPLATE_PART_POST_TYPE,
      isActive: currentCategory === _constants.TEMPLATE_PART_ALL_AREAS_CATEGORY && currentType === _constants.TEMPLATE_PART_POST_TYPE
    }, "all"), Object.entries(templatePartAreas).map(([area, {
      label,
      templateParts
    }]) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_categoryItem.default, {
      count: templateParts?.length,
      icon: (0, _editor.getTemplatePartIcon)(area),
      label: label,
      id: area,
      type: _constants.TEMPLATE_PART_POST_TYPE,
      isActive: currentCategory === area && currentType === _constants.TEMPLATE_PART_POST_TYPE
    }, area)), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "edit-site-sidebar-navigation-screen-patterns__divider"
    }), allPatterns && /*#__PURE__*/(0, _jsxRuntime.jsx)(_categoryItem.default, {
      count: allPatterns.count,
      label: allPatterns.label,
      icon: _icons.file,
      id: allPatterns.name,
      type: _constants.PATTERN_TYPES.user,
      isActive: currentCategory === `${allPatterns.name}` && currentType === _constants.PATTERN_TYPES.user
    }, allPatterns.name), otherPatterns.map(category => /*#__PURE__*/(0, _jsxRuntime.jsx)(_categoryItem.default, {
      count: category.count,
      label: category.label,
      icon: _icons.file,
      id: category.name,
      type: _constants.PATTERN_TYPES.user,
      isActive: currentCategory === `${category.name}` && currentType === _constants.PATTERN_TYPES.user
    }, category.name))]
  });
}
function SidebarNavigationScreenPatterns({
  backPath
}) {
  const {
    query: {
      postType = 'wp_block',
      categoryId
    }
  } = useLocation();
  const currentCategory = categoryId || (postType === _constants.PATTERN_TYPES.user ? _constants.PATTERN_DEFAULT_CATEGORY : _constants.TEMPLATE_PART_ALL_AREAS_CATEGORY);
  const {
    templatePartAreas,
    hasTemplateParts,
    isLoading
  } = (0, _useTemplatePartAreas.default)();
  const {
    patternCategories,
    hasPatterns
  } = (0, _usePatternCategories.default)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreen.default, {
    title: (0, _i18n.__)('Patterns'),
    description: (0, _i18n.__)('Manage what patterns are available when editing the site.'),
    isRoot: !backPath,
    backPath: backPath,
    content: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [isLoading && (0, _i18n.__)('Loading items…'), !isLoading && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [!hasTemplateParts && !hasPatterns && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalItemGroup, {
          className: "edit-site-sidebar-navigation-screen-patterns__group",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalItem, {
            children: (0, _i18n.__)('No items found')
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(CategoriesGroup, {
          templatePartAreas: templatePartAreas,
          patternCategories: patternCategories,
          currentCategory: currentCategory,
          currentType: postType
        })]
      })]
    })
  });
}
//# sourceMappingURL=index.js.map