"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/navigation/edit/navigation-menu-selector.js
var navigation_menu_selector_exports = {};
__export(navigation_menu_selector_exports, {
  default: () => navigation_menu_selector_default
});
module.exports = __toCommonJS(navigation_menu_selector_exports);
var import_components = require("@wordpress/components");
var import_icons = require("@wordpress/icons");
var import_i18n = require("@wordpress/i18n");
var import_html_entities = require("@wordpress/html-entities");
var import_element = require("@wordpress/element");
var import_core_data = require("@wordpress/core-data");
var import_use_navigation_menu = __toESM(require("../use-navigation-menu.cjs"));
var import_jsx_runtime = require("react/jsx-runtime");
function buildMenuLabel(title, id, status) {
  if (!title) {
    return (0, import_i18n.sprintf)((0, import_i18n.__)("(no title %s)"), id);
  }
  if (status === "publish") {
    return (0, import_html_entities.decodeEntities)(title);
  }
  return (0, import_i18n.sprintf)(
    // translators: 1: title of the menu. 2: status of the menu (draft, pending, etc.).
    (0, import_i18n.__)("%1$s (%2$s)"),
    (0, import_html_entities.decodeEntities)(title),
    status
  );
}
function NavigationMenuSelector({
  currentMenuId,
  onSelectNavigationMenu,
  onSelectClassicMenu,
  onCreateNew,
  actionLabel,
  createNavigationMenuIsSuccess,
  createNavigationMenuIsError
}) {
  const createActionLabel = (0, import_i18n.__)("Create from '%s'");
  const [isUpdatingMenuRef, setIsUpdatingMenuRef] = (0, import_element.useState)(false);
  actionLabel = actionLabel || createActionLabel;
  const { records: classicMenus } = (0, import_core_data.useEntityRecords)("root", "menu", {
    per_page: -1,
    context: "view"
  });
  const {
    navigationMenus,
    isResolvingNavigationMenus,
    hasResolvedNavigationMenus,
    canUserCreateNavigationMenus,
    canSwitchNavigationMenu,
    isNavigationMenuMissing
  } = (0, import_use_navigation_menu.default)(currentMenuId);
  const [currentTitle] = (0, import_core_data.useEntityProp)(
    "postType",
    "wp_navigation",
    "title",
    currentMenuId
  );
  const menuChoices = (0, import_element.useMemo)(() => {
    return navigationMenus?.map(({ id, title, status }, index) => {
      const label = buildMenuLabel(
        title?.rendered,
        index + 1,
        status
      );
      return {
        value: id,
        label,
        ariaLabel: (0, import_i18n.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 = (0, import_i18n.__)("Loading…");
  } else if (noMenuSelected || noBlockMenus || menuUnavailable || navMenuHasBeenDeleted) {
    selectorLabel = (0, import_i18n.__)("Choose or create a Navigation Menu");
  } else {
    selectorLabel = currentTitle;
  }
  (0, import_element.useEffect)(() => {
    if (isUpdatingMenuRef && (createNavigationMenuIsSuccess || createNavigationMenuIsError)) {
      setIsUpdatingMenuRef(false);
    }
  }, [
    hasResolvedNavigationMenus,
    createNavigationMenuIsSuccess,
    canUserCreateNavigationMenus,
    createNavigationMenuIsError,
    isUpdatingMenuRef,
    menuUnavailable,
    noBlockMenus,
    noMenuSelected
  ]);
  const NavigationMenuSelectorDropdown = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.DropdownMenu,
    {
      label: selectorLabel,
      icon: import_icons.moreVertical,
      toggleProps: { size: "small" },
      children: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
        showNavigationMenus && hasNavigationMenus && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { label: (0, import_i18n.__)("Menus"), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.MenuItemsChoice,
          {
            value: currentMenuId,
            onSelect: (menuId) => {
              onSelectNavigationMenu(menuId);
              onClose();
            },
            choices: menuChoices
          }
        ) }),
        showClassicMenus && hasClassicMenus && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { label: (0, import_i18n.__)("Import Classic Menus"), children: classicMenus?.map((menu) => {
          const label = (0, import_html_entities.decodeEntities)(menu.name);
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.MenuItem,
            {
              onClick: async () => {
                setIsUpdatingMenuRef(true);
                await onSelectClassicMenu(menu);
                setIsUpdatingMenuRef(false);
                onClose();
              },
              "aria-label": (0, import_i18n.sprintf)(
                createActionLabel,
                label
              ),
              disabled: isUpdatingMenuRef || isResolvingNavigationMenus || !hasResolvedNavigationMenus,
              children: label
            },
            menu.id
          );
        }) }),
        canUserCreateNavigationMenus && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { label: (0, import_i18n.__)("Tools"), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.MenuItem,
          {
            onClick: async () => {
              setIsUpdatingMenuRef(true);
              await onCreateNew();
              setIsUpdatingMenuRef(false);
              onClose();
            },
            disabled: isUpdatingMenuRef || isResolvingNavigationMenus || !hasResolvedNavigationMenus,
            children: (0, import_i18n.__)("Create new Menu")
          }
        ) })
      ] })
    }
  );
  return NavigationMenuSelectorDropdown;
}
var navigation_menu_selector_default = NavigationMenuSelector;
//# sourceMappingURL=navigation-menu-selector.cjs.map
