/**
 * WordPress dependencies
 */
import { __, _x, sprintf } from '@wordpress/i18n';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { __experimentalItemGroup as ItemGroup, Spinner } from '@wordpress/components';
import { navigation } from '@wordpress/icons';

/**
 * Internal dependencies
 */
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import SidebarNavigationItem from '../sidebar-navigation-item';
import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants';
import SingleNavigationMenu from '../sidebar-navigation-screen-navigation-menu/single-navigation-menu';
import useNavigationMenuHandlers from '../sidebar-navigation-screen-navigation-menu/use-navigation-menu-handlers';
import { unlock } from '../../lock-unlock';
import { NAVIGATION_POST_TYPE } from '../../utils/constants';

// Copied from packages/block-library/src/navigation/edit/navigation-menu-selector.js.
import { jsx as _jsx } from "react/jsx-runtime";
function buildMenuLabel(title, id, status) {
  if (!title) {
    /* translators: %s: the index of the menu in the list of menus. */
    return sprintf(__('(no title %s)'), id);
  }
  if (status === 'publish') {
    return decodeEntities(title);
  }
  return sprintf(
  // translators: 1: title of the menu. 2: status of the menu (draft, pending, etc.).
  _x('%1$s (%2$s)', 'menu label'), decodeEntities(title), status);
}
export default function SidebarNavigationScreenNavigationMenus({
  backPath
}) {
  const {
    records: navigationMenus,
    isResolving: isResolvingNavigationMenus,
    hasResolved: hasResolvedNavigationMenus
  } = useEntityRecords('postType', NAVIGATION_POST_TYPE, PRELOADED_NAVIGATION_MENUS_QUERY);
  const isLoading = isResolvingNavigationMenus && !hasResolvedNavigationMenus;
  const {
    getNavigationFallbackId
  } = unlock(useSelect(coreStore));
  const isCreatingNavigationFallback = useSelect(select => select(coreStore).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
  } = useNavigationMenuHandlers();
  const hasNavigationMenus = !!navigationMenus?.length;
  if (isLoading) {
    return /*#__PURE__*/_jsx(SidebarNavigationScreenWrapper, {
      backPath: backPath,
      children: /*#__PURE__*/_jsx(Spinner, {
        className: "edit-site-sidebar-navigation-screen-navigation-menus__loading"
      })
    });
  }
  if (!isLoading && !hasNavigationMenus) {
    return /*#__PURE__*/_jsx(SidebarNavigationScreenWrapper, {
      description: __('No Navigation Menus found.'),
      backPath: backPath
    });
  }

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