// packages/block-library/src/navigation/edit/navigation-menu-selector.js
import {
  MenuGroup,
  MenuItem,
  MenuItemsChoice,
  DropdownMenu
} from "@wordpress/components";
import { moreVertical } from "@wordpress/icons";
import { __, sprintf } from "@wordpress/i18n";
import { decodeEntities } from "@wordpress/html-entities";
import { useEffect, useMemo, useState } from "@wordpress/element";
import { useEntityProp } from "@wordpress/core-data";
import useNavigationMenu from "../use-navigation-menu";
import useNavigationEntities from "../use-navigation-entities";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function buildMenuLabel(title, id, status) {
  if (!title) {
    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.).
    __("%1$s (%2$s)"),
    decodeEntities(title),
    status
  );
}
function NavigationMenuSelector({
  currentMenuId,
  onSelectNavigationMenu,
  onSelectClassicMenu,
  onCreateNew,
  actionLabel,
  createNavigationMenuIsSuccess,
  createNavigationMenuIsError
}) {
  const createActionLabel = __("Create from '%s'");
  const [isUpdatingMenuRef, setIsUpdatingMenuRef] = useState(false);
  actionLabel = actionLabel || createActionLabel;
  const { menus: classicMenus } = useNavigationEntities();
  const {
    navigationMenus,
    isResolvingNavigationMenus,
    hasResolvedNavigationMenus,
    canUserCreateNavigationMenus,
    canSwitchNavigationMenu,
    isNavigationMenuMissing
  } = useNavigationMenu(currentMenuId);
  const [currentTitle] = useEntityProp(
    "postType",
    "wp_navigation",
    "title"
  );
  const menuChoices = useMemo(() => {
    return navigationMenus?.map(({ id, title, status }, index) => {
      const label = buildMenuLabel(
        title?.rendered,
        index + 1,
        status
      );
      return {
        value: id,
        label,
        ariaLabel: sprintf(actionLabel, label),
        disabled: isUpdatingMenuRef || isResolvingNavigationMenus || !hasResolvedNavigationMenus
      };
    }) || [];
  }, [
    navigationMenus,
    actionLabel,
    isResolvingNavigationMenus,
    hasResolvedNavigationMenus,
    isUpdatingMenuRef
  ]);
  const hasNavigationMenus = !!navigationMenus?.length;
  const hasClassicMenus = !!classicMenus?.length;
  const showNavigationMenus = !!canSwitchNavigationMenu;
  const showClassicMenus = !!canUserCreateNavigationMenus;
  const noMenuSelected = hasNavigationMenus && !currentMenuId;
  const noBlockMenus = !hasNavigationMenus && hasResolvedNavigationMenus;
  const menuUnavailable = hasResolvedNavigationMenus && currentMenuId === null;
  const navMenuHasBeenDeleted = currentMenuId && isNavigationMenuMissing;
  let selectorLabel = "";
  if (isResolvingNavigationMenus) {
    selectorLabel = __("Loading\u2026");
  } else if (noMenuSelected || noBlockMenus || menuUnavailable || navMenuHasBeenDeleted) {
    selectorLabel = __("Choose or create a Navigation Menu");
  } else {
    selectorLabel = currentTitle;
  }
  useEffect(() => {
    if (isUpdatingMenuRef && (createNavigationMenuIsSuccess || createNavigationMenuIsError)) {
      setIsUpdatingMenuRef(false);
    }
  }, [
    hasResolvedNavigationMenus,
    createNavigationMenuIsSuccess,
    canUserCreateNavigationMenus,
    createNavigationMenuIsError,
    isUpdatingMenuRef,
    menuUnavailable,
    noBlockMenus,
    noMenuSelected
  ]);
  const NavigationMenuSelectorDropdown = /* @__PURE__ */ jsx(
    DropdownMenu,
    {
      label: selectorLabel,
      icon: moreVertical,
      toggleProps: { size: "small" },
      children: ({ onClose }) => /* @__PURE__ */ jsxs(Fragment, { children: [
        showNavigationMenus && hasNavigationMenus && /* @__PURE__ */ jsx(MenuGroup, { label: __("Menus"), children: /* @__PURE__ */ jsx(
          MenuItemsChoice,
          {
            value: currentMenuId,
            onSelect: (menuId) => {
              onSelectNavigationMenu(menuId);
              onClose();
            },
            choices: menuChoices
          }
        ) }),
        showClassicMenus && hasClassicMenus && /* @__PURE__ */ jsx(MenuGroup, { label: __("Import Classic Menus"), children: classicMenus?.map((menu) => {
          const label = decodeEntities(menu.name);
          return /* @__PURE__ */ jsx(
            MenuItem,
            {
              onClick: async () => {
                setIsUpdatingMenuRef(true);
                await onSelectClassicMenu(menu);
                setIsUpdatingMenuRef(false);
                onClose();
              },
              "aria-label": sprintf(
                createActionLabel,
                label
              ),
              disabled: isUpdatingMenuRef || isResolvingNavigationMenus || !hasResolvedNavigationMenus,
              children: label
            },
            menu.id
          );
        }) }),
        canUserCreateNavigationMenus && /* @__PURE__ */ jsx(MenuGroup, { label: __("Tools"), children: /* @__PURE__ */ jsx(
          MenuItem,
          {
            onClick: async () => {
              setIsUpdatingMenuRef(true);
              await onCreateNew();
              setIsUpdatingMenuRef(false);
              onClose();
            },
            disabled: isUpdatingMenuRef || isResolvingNavigationMenus || !hasResolvedNavigationMenus,
            children: __("Create new Menu")
          }
        ) })
      ] })
    }
  );
  return NavigationMenuSelectorDropdown;
}
var navigation_menu_selector_default = NavigationMenuSelector;
export {
  navigation_menu_selector_default as default
};
//# sourceMappingURL=navigation-menu-selector.js.map
