{
  "version": 3,
  "sources": ["../../../src/navigation/edit/use-convert-classic-menu-to-block-menu.js"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRegistry, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useState, useCallback } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport menuItemsToBlocks from '../menu-items-to-blocks';\n\nexport const CLASSIC_MENU_CONVERSION_SUCCESS = 'success';\nexport const CLASSIC_MENU_CONVERSION_ERROR = 'error';\nexport const CLASSIC_MENU_CONVERSION_PENDING = 'pending';\nexport const CLASSIC_MENU_CONVERSION_IDLE = 'idle';\n\n// This is needed to ensure that multiple components using this hook\n// do not import the same classic menu twice.\nlet classicMenuBeingConvertedId = null;\n\nfunction useConvertClassicToBlockMenu(\n\tcreateNavigationMenu,\n\t{ throwOnError = false } = {}\n) {\n\tconst registry = useRegistry();\n\tconst { editEntityRecord } = useDispatch( coreStore );\n\n\tconst [ status, setStatus ] = useState( CLASSIC_MENU_CONVERSION_IDLE );\n\tconst [ error, setError ] = useState( null );\n\n\tconst convertClassicMenuToBlockMenu = useCallback(\n\t\tasync ( menuId, menuName, postStatus = 'publish' ) => {\n\t\t\tlet navigationMenu;\n\t\t\tlet classicMenuItems;\n\n\t\t\t// 1. Fetch the classic Menu items.\n\t\t\ttry {\n\t\t\t\tclassicMenuItems = await registry\n\t\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t\t.getMenuItems( {\n\t\t\t\t\t\tmenus: menuId,\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t} );\n\t\t\t} catch ( err ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to fetch classic menu \"%s\" from API.` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t),\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: err,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Handle offline response which resolves to `null`.\n\t\t\tif ( classicMenuItems === null ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to fetch classic menu \"%s\" from API.` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 2. Convert the classic items into blocks.\n\t\t\tconst { innerBlocks } = menuItemsToBlocks( classicMenuItems );\n\n\t\t\t// 3. Create the `wp_navigation` Post with the blocks.\n\t\t\ttry {\n\t\t\t\tnavigationMenu = await createNavigationMenu(\n\t\t\t\t\tmenuName,\n\t\t\t\t\tinnerBlocks,\n\t\t\t\t\tpostStatus\n\t\t\t\t);\n\n\t\t\t\t/**\n\t\t\t\t * Immediately trigger editEntityRecord to change the wp_navigation post status to 'publish'.\n\t\t\t\t * This status change causes the menu to be displayed on the front of the site and sets the post state to be \"dirty\".\n\t\t\t\t * The problem being solved is if saveEditedEntityRecord was used here, the menu would be updated on the frontend and the editor _automatically_,\n\t\t\t\t * without user interaction.\n\t\t\t\t * If the user abandons the site editor without saving, there would still be a wp_navigation post created as draft.\n\t\t\t\t */\n\t\t\t\tawait editEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_navigation',\n\t\t\t\t\tnavigationMenu.id,\n\t\t\t\t\t{\n\t\t\t\t\t\tstatus: 'publish',\n\t\t\t\t\t},\n\t\t\t\t\t{ throwOnError: true }\n\t\t\t\t);\n\t\t\t} catch ( err ) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t__( `Unable to create Navigation Menu \"%s\".` ),\n\t\t\t\t\t\tmenuName\n\t\t\t\t\t),\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: err,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn navigationMenu;\n\t\t},\n\t\t[ createNavigationMenu, editEntityRecord, registry ]\n\t);\n\n\tconst convert = useCallback(\n\t\tasync ( menuId, menuName, postStatus ) => {\n\t\t\t// Check whether this classic menu is being imported already.\n\t\t\tif ( classicMenuBeingConvertedId === menuId ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Set the ID for the currently importing classic menu.\n\t\t\tclassicMenuBeingConvertedId = menuId;\n\n\t\t\tif ( ! menuId || ! menuName ) {\n\t\t\t\tsetError( 'Unable to convert menu. Missing menu details.' );\n\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_ERROR );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_PENDING );\n\t\t\tsetError( null );\n\n\t\t\treturn await convertClassicMenuToBlockMenu(\n\t\t\t\tmenuId,\n\t\t\t\tmenuName,\n\t\t\t\tpostStatus\n\t\t\t)\n\t\t\t\t.then( ( navigationMenu ) => {\n\t\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_SUCCESS );\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tclassicMenuBeingConvertedId = null;\n\t\t\t\t\treturn navigationMenu;\n\t\t\t\t} )\n\t\t\t\t.catch( ( err ) => {\n\t\t\t\t\tsetError( err?.message );\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tsetStatus( CLASSIC_MENU_CONVERSION_ERROR );\n\n\t\t\t\t\t// Reset the ID for the currently importing classic menu.\n\t\t\t\t\tclassicMenuBeingConvertedId = null;\n\n\t\t\t\t\t// Rethrow error for debugging.\n\t\t\t\t\tif ( throwOnError ) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\tsprintf(\n\t\t\t\t\t\t\t\t// translators: %s: The name of a menu (e.g. Header menu).\n\t\t\t\t\t\t\t\t__( `Unable to create Navigation Menu \"%s\".` ),\n\t\t\t\t\t\t\t\tmenuName\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tcause: err,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} );\n\t\t},\n\t\t[ convertClassicMenuToBlockMenu, throwOnError ]\n\t);\n\n\treturn {\n\t\tconvert,\n\t\tstatus,\n\t\terror,\n\t};\n}\n\nexport default useConvertClassicToBlockMenu;\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAyC;AACzC,uBAAmC;AACnC,qBAAsC;AACtC,kBAA4B;AAK5B,kCAA8B;AAEvB,IAAM,kCAAkC;AACxC,IAAM,gCAAgC;AACtC,IAAM,kCAAkC;AACxC,IAAM,+BAA+B;AAI5C,IAAI,8BAA8B;AAElC,SAAS,6BACR,sBACA,EAAE,eAAe,MAAM,IAAI,CAAC,GAC3B;AACD,QAAM,eAAW,yBAAY;AAC7B,QAAM,EAAE,iBAAiB,QAAI,yBAAa,iBAAAA,KAAU;AAEpD,QAAM,CAAE,QAAQ,SAAU,QAAI,yBAAU,4BAA6B;AACrE,QAAM,CAAE,OAAO,QAAS,QAAI,yBAAU,IAAK;AAE3C,QAAM,oCAAgC;AAAA,IACrC,OAAQ,QAAQ,UAAU,aAAa,cAAe;AACrD,UAAI;AACJ,UAAI;AAGJ,UAAI;AACH,2BAAmB,MAAM,SACvB,cAAe,iBAAAA,KAAU,EACzB,aAAc;AAAA,UACd,OAAO;AAAA,UACP,UAAU;AAAA,UACV,SAAS;AAAA,QACV,CAAE;AAAA,MACJ,SAAU,KAAM;AACf,cAAM,IAAI;AAAA,cACT;AAAA;AAAA,gBAEC,gBAAI,6CAA8C;AAAA,YAClD;AAAA,UACD;AAAA,UACA;AAAA,YACC,OAAO;AAAA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