// packages/block-library/src/navigation/edit/use-convert-classic-menu-to-block-menu.js
import { useRegistry, useDispatch } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { useState, useCallback } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import menuItemsToBlocks from "../menu-items-to-blocks";
var CLASSIC_MENU_CONVERSION_SUCCESS = "success";
var CLASSIC_MENU_CONVERSION_ERROR = "error";
var CLASSIC_MENU_CONVERSION_PENDING = "pending";
var CLASSIC_MENU_CONVERSION_IDLE = "idle";
var classicMenuBeingConvertedId = null;
function useConvertClassicToBlockMenu(createNavigationMenu, { throwOnError = false } = {}) {
  const registry = useRegistry();
  const { editEntityRecord } = useDispatch(coreStore);
  const [status, setStatus] = useState(CLASSIC_MENU_CONVERSION_IDLE);
  const [error, setError] = useState(null);
  const convertClassicMenuToBlockMenu = useCallback(
    async (menuId, menuName, postStatus = "publish") => {
      let navigationMenu;
      let classicMenuItems;
      try {
        classicMenuItems = await registry.resolveSelect(coreStore).getMenuItems({
          menus: menuId,
          per_page: -1,
          context: "view"
        });
      } catch (err) {
        throw new Error(
          sprintf(
            // translators: %s: The name of a menu (e.g. Header menu).
            __(`Unable to fetch classic menu "%s" from API.`),
            menuName
          ),
          {
            cause: err
          }
        );
      }
      if (classicMenuItems === null) {
        throw new Error(
          sprintf(
            // translators: %s: The name of a menu (e.g. Header menu).
            __(`Unable to fetch classic menu "%s" from API.`),
            menuName
          )
        );
      }
      const { innerBlocks } = menuItemsToBlocks(classicMenuItems);
      try {
        navigationMenu = await createNavigationMenu(
          menuName,
          innerBlocks,
          postStatus
        );
        await editEntityRecord(
          "postType",
          "wp_navigation",
          navigationMenu.id,
          {
            status: "publish"
          },
          { throwOnError: true }
        );
      } catch (err) {
        throw new Error(
          sprintf(
            // translators: %s: The name of a menu (e.g. Header menu).
            __(`Unable to create Navigation Menu "%s".`),
            menuName
          ),
          {
            cause: err
          }
        );
      }
      return navigationMenu;
    },
    [createNavigationMenu, editEntityRecord, registry]
  );
  const convert = useCallback(
    async (menuId, menuName, postStatus) => {
      if (classicMenuBeingConvertedId === menuId) {
        return;
      }
      classicMenuBeingConvertedId = menuId;
      if (!menuId || !menuName) {
        setError("Unable to convert menu. Missing menu details.");
        setStatus(CLASSIC_MENU_CONVERSION_ERROR);
        return;
      }
      setStatus(CLASSIC_MENU_CONVERSION_PENDING);
      setError(null);
      return await convertClassicMenuToBlockMenu(
        menuId,
        menuName,
        postStatus
      ).then((navigationMenu) => {
        setStatus(CLASSIC_MENU_CONVERSION_SUCCESS);
        classicMenuBeingConvertedId = null;
        return navigationMenu;
      }).catch((err) => {
        setError(err?.message);
        setStatus(CLASSIC_MENU_CONVERSION_ERROR);
        classicMenuBeingConvertedId = null;
        if (throwOnError) {
          throw new Error(
            sprintf(
              // translators: %s: The name of a menu (e.g. Header menu).
              __(`Unable to create Navigation Menu "%s".`),
              menuName
            ),
            {
              cause: err
            }
          );
        }
      });
    },
    [convertClassicMenuToBlockMenu, throwOnError]
  );
  return {
    convert,
    status,
    error
  };
}
var use_convert_classic_menu_to_block_menu_default = useConvertClassicToBlockMenu;
export {
  CLASSIC_MENU_CONVERSION_ERROR,
  CLASSIC_MENU_CONVERSION_IDLE,
  CLASSIC_MENU_CONVERSION_PENDING,
  CLASSIC_MENU_CONVERSION_SUCCESS,
  use_convert_classic_menu_to_block_menu_default as default
};
//# sourceMappingURL=use-convert-classic-menu-to-block-menu.js.map
