"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SidebarNavigationScreenWrapper = SidebarNavigationScreenWrapper;
exports.default = SidebarNavigationScreenNavigationMenus;
var _i18n = require("@wordpress/i18n");
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _htmlEntities = require("@wordpress/html-entities");
var _components = require("@wordpress/components");
var _icons = require("@wordpress/icons");
var _sidebarNavigationScreen = _interopRequireDefault(require("../sidebar-navigation-screen"));
var _sidebarNavigationItem = _interopRequireDefault(require("../sidebar-navigation-item"));
var _constants = require("./constants");
var _singleNavigationMenu = _interopRequireDefault(require("../sidebar-navigation-screen-navigation-menu/single-navigation-menu"));
var _useNavigationMenuHandlers = _interopRequireDefault(require("../sidebar-navigation-screen-navigation-menu/use-navigation-menu-handlers"));
var _lockUnlock = require("../../lock-unlock");
var _constants2 = require("../../utils/constants");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

// Copied from packages/block-library/src/navigation/edit/navigation-menu-selector.js.
function buildMenuLabel(title, id, status) {
  if (!title) {
    /* translators: %s: the index of the menu in the list of menus. */
    return (0, _i18n.sprintf)((0, _i18n.__)('(no title %s)'), id);
  }
  if (status === 'publish') {
    return (0, _htmlEntities.decodeEntities)(title);
  }
  return (0, _i18n.sprintf)(
  // translators: 1: title of the menu. 2: status of the menu (draft, pending, etc.).
  (0, _i18n._x)('%1$s (%2$s)', 'menu label'), (0, _htmlEntities.decodeEntities)(title), status);
}
function SidebarNavigationScreenNavigationMenus({
  backPath
}) {
  const {
    records: navigationMenus,
    isResolving: isResolvingNavigationMenus,
    hasResolved: hasResolvedNavigationMenus
  } = (0, _coreData.useEntityRecords)('postType', _constants2.NAVIGATION_POST_TYPE, _constants.PRELOADED_NAVIGATION_MENUS_QUERY);
  const isLoading = isResolvingNavigationMenus && !hasResolvedNavigationMenus;
  const {
    getNavigationFallbackId
  } = (0, _lockUnlock.unlock)((0, _data.useSelect)(_coreData.store));
  const isCreatingNavigationFallback = (0, _data.useSelect)(select => select(_coreData.store).isResolving('getNavigationFallbackId'), []);
  const firstNavigationMenu = navigationMenus?.[0];

  // If there is no navigation menu found
  // then trigger fallback algorithm to create one.
  if (!firstNavigationMenu && !isResolvingNavigationMenus && hasResolvedNavigationMenus &&
  // Ensure a fallback navigation is created only once
  !isCreatingNavigationFallback) {
    getNavigationFallbackId();
  }
  const {
    handleSave,
    handleDelete,
    handleDuplicate
  } = (0, _useNavigationMenuHandlers.default)();
  const hasNavigationMenus = !!navigationMenus?.length;
  if (isLoading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(SidebarNavigationScreenWrapper, {
      backPath: backPath,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Spinner, {
        className: "edit-site-sidebar-navigation-screen-navigation-menus__loading"
      })
    });
  }
  if (!isLoading && !hasNavigationMenus) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(SidebarNavigationScreenWrapper, {
      description: (0, _i18n.__)('No Navigation Menus found.'),
      backPath: backPath
    });
  }

  // if single menu then render it
  if (navigationMenus?.length === 1) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_singleNavigationMenu.default, {
      navigationMenu: firstNavigationMenu,
      backPath: backPath,
      handleDelete: () => handleDelete(firstNavigationMenu),
      handleDuplicate: () => handleDuplicate(firstNavigationMenu),
      handleSave: edits => handleSave(firstNavigationMenu, edits)
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(SidebarNavigationScreenWrapper, {
    backPath: backPath,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalItemGroup, {
      className: "edit-site-sidebar-navigation-screen-navigation-menus",
      children: navigationMenus?.map(({
        id,
        title,
        status
      }, index) => /*#__PURE__*/(0, _jsxRuntime.jsx)(NavMenuItem, {
        postId: id,
        withChevron: true,
        icon: _icons.navigation,
        children: buildMenuLabel(title?.rendered, index + 1, status)
      }, id))
    })
  });
}
function SidebarNavigationScreenWrapper({
  children,
  actions,
  title,
  description,
  backPath
}) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreen.default, {
    title: title || (0, _i18n.__)('Navigation'),
    actions: actions,
    description: description || (0, _i18n.__)('Manage your Navigation Menus.'),
    backPath: backPath,
    content: children
  });
}
const NavMenuItem = ({
  postId,
  ...props
}) => {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationItem.default, {
    to: `/wp_navigation/${postId}`,
    ...props
  });
};
//# sourceMappingURL=index.js.map