// packages/block-library/src/navigation/edit/use-generate-default-navigation-title.js
import { Disabled } from "@wordpress/components";
import { store as coreStore } from "@wordpress/core-data";
import { useRegistry } from "@wordpress/data";
import { useContext, useCallback } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import useTemplatePartAreaLabel from "../use-template-part-area-label";
var DRAFT_MENU_PARAMS = [
  "postType",
  "wp_navigation",
  { status: "draft", per_page: -1 }
];
var PUBLISHED_MENU_PARAMS = [
  "postType",
  "wp_navigation",
  { per_page: -1, status: "publish" }
];
function useGenerateDefaultNavigationTitle(clientId) {
  const isDisabled = useContext(Disabled.Context);
  const area = useTemplatePartAreaLabel(isDisabled ? void 0 : clientId);
  const registry = useRegistry();
  return useCallback(async () => {
    if (isDisabled) {
      return "";
    }
    const { getEntityRecords } = registry.resolveSelect(coreStore);
    const [draftNavigationMenus, navigationMenus] = await Promise.all([
      getEntityRecords(...DRAFT_MENU_PARAMS),
      getEntityRecords(...PUBLISHED_MENU_PARAMS)
    ]);
    const title = area ? sprintf(
      // translators: %s: the name of a menu (e.g. Header menu).
      __("%s menu"),
      area
    ) : (
      // translators: 'menu' as in website navigation menu.
      __("Menu")
    );
    const matchingMenuTitleCount = [
      ...draftNavigationMenus,
      ...navigationMenus
    ].reduce(
      (count, menu) => menu?.title?.raw?.startsWith(title) ? count + 1 : count,
      0
    );
    const titleWithCount = matchingMenuTitleCount > 0 ? `${title} ${matchingMenuTitleCount + 1}` : title;
    return titleWithCount || "";
  }, [isDisabled, area, registry]);
}
export {
  useGenerateDefaultNavigationTitle as default
};
//# sourceMappingURL=use-generate-default-navigation-title.js.map
