{
  "version": 3,
  "sources": ["../../../src/navigation/edit/navigation-link-ui.js"],
  "sourcesContent": ["import { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport {\n\tLinkUI,\n\tupdateAttributes,\n\tuseEntityBinding,\n} from '../../navigation-link/shared';\n\nconst BLOCKS_WITH_LINK_UI_SUPPORT = [\n\t'core/navigation-link',\n\t'core/navigation-submenu',\n];\n\nexport function NavigationLinkUI( {\n\tinsertedBlockClientId,\n\tsetInsertedBlockClientId,\n} ) {\n\tconst { updateBlockAttributes, removeBlock } =\n\t\tuseDispatch( blockEditorStore );\n\n\tconst { insertedBlockName, insertedBlockAttributes } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockName, getBlockAttributes } =\n\t\t\t\tselect( blockEditorStore );\n\n\t\t\treturn {\n\t\t\t\tinsertedBlockName: getBlockName( insertedBlockClientId ),\n\t\t\t\tinsertedBlockAttributes: getBlockAttributes(\n\t\t\t\t\tinsertedBlockClientId\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ insertedBlockClientId ]\n\t);\n\n\tconst showLinkControls =\n\t\tBLOCKS_WITH_LINK_UI_SUPPORT?.includes( insertedBlockName );\n\n\t// Get binding utilities for the inserted block\n\tconst { createBinding, clearBinding } = useEntityBinding( {\n\t\tclientId: insertedBlockClientId,\n\t\tattributes: insertedBlockAttributes || {},\n\t} );\n\n\tif ( ! showLinkControls ) {\n\t\treturn null;\n\t}\n\n\t/**\n\t * Cleanup function for auto-inserted Navigation Link blocks.\n\t *\n\t * Removes the block if it has no URL and clears the inserted block state.\n\t * This ensures consistent cleanup behavior across different contexts.\n\t */\n\tconst cleanupInsertedBlock = () => {\n\t\t// Prevent automatic block selection when removing blocks in list view context\n\t\t// This avoids focus stealing that would close the list view and switch to canvas\n\t\tconst shouldAutoSelectBlock = false;\n\n\t\t// Follows the exact same pattern as Navigation Link block's onClose handler\n\t\t// If there is no URL then remove the auto-inserted block to avoid empty blocks\n\t\tif ( ! insertedBlockAttributes?.url && insertedBlockClientId ) {\n\t\t\t// Remove the block entirely to avoid poor UX\n\t\t\t// This matches the Navigation Link block's behavior\n\t\t\tremoveBlock( insertedBlockClientId, shouldAutoSelectBlock );\n\t\t}\n\t\tsetInsertedBlockClientId( null );\n\t};\n\n\tconst setInsertedBlockAttributes =\n\t\t( _insertedBlockClientId ) => ( _updatedAttributes ) => {\n\t\t\tif ( ! _insertedBlockClientId ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tupdateBlockAttributes( _insertedBlockClientId, _updatedAttributes );\n\t\t};\n\n\t// Wrapper function to clean up original block when a new block is selected\n\tconst handleSetInsertedBlock = ( newBlock ) => {\n\t\t// Prevent automatic block selection when removing blocks in list view context\n\t\t// This avoids focus stealing that would close the list view and switch to canvas\n\t\tconst shouldAutoSelectBlock = false;\n\n\t\t// If we have an existing inserted block and a new block is being set,\n\t\t// remove the original block to avoid duplicates\n\t\tif ( insertedBlockClientId && newBlock ) {\n\t\t\tremoveBlock( insertedBlockClientId, shouldAutoSelectBlock );\n\t\t}\n\t\tsetInsertedBlockClientId( newBlock?.clientId ?? null );\n\t};\n\n\treturn (\n\t\t<LinkUI\n\t\t\tclientId={ insertedBlockClientId }\n\t\t\tlink={ insertedBlockAttributes }\n\t\t\tonBlockInsert={ handleSetInsertedBlock }\n\t\t\tonClose={ () => {\n\t\t\t\t// Use cleanup function\n\t\t\t\tcleanupInsertedBlock();\n\t\t\t} }\n\t\t\tonChange={ ( updatedValue ) => {\n\t\t\t\t// updateAttributes determines the final state and returns metadata\n\t\t\t\tconst { isEntityLink, attributes: updatedAttributes } =\n\t\t\t\t\tupdateAttributes(\n\t\t\t\t\t\tupdatedValue,\n\t\t\t\t\t\tsetInsertedBlockAttributes( insertedBlockClientId ),\n\t\t\t\t\t\tinsertedBlockAttributes\n\t\t\t\t\t);\n\n\t\t\t\t// Handle URL binding based on the final computed state\n\t\t\t\t// Only create bindings for entity links (posts, pages, taxonomies)\n\t\t\t\t// Never create bindings for custom links (manual URLs)\n\t\t\t\tif ( isEntityLink ) {\n\t\t\t\t\tcreateBinding( updatedAttributes );\n\t\t\t\t} else {\n\t\t\t\t\tclearBinding();\n\t\t\t\t}\n\n\t\t\t\tsetInsertedBlockClientId( null );\n\t\t\t} }\n\t\t/>\n\t);\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAA0C;AAC1C,kBAAuC;AACvC,oBAIO;AAsFL;AApFF,IAAM,8BAA8B;AAAA,EACnC;AAAA,EACA;AACD;AAEO,SAAS,iBAAkB;AAAA,EACjC;AAAA,EACA;AACD,GAAI;AACH,QAAM,EAAE,uBAAuB,YAAY,QAC1C,yBAAa,oBAAAA,KAAiB;AAE/B,QAAM,EAAE,mBAAmB,wBAAwB,QAAI;AAAA,IACtD,CAAE,WAAY;AACb,YAAM,EAAE,cAAc,mBAAmB,IACxC,OAAQ,oBAAAA,KAAiB;AAE1B,aAAO;AAAA,QACN,mBAAmB,aAAc,qBAAsB;AAAA,QACvD,yBAAyB;AAAA,UACxB;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,qBAAsB;AAAA,EACzB;AAEA,QAAM,mBACL,6BAA6B,SAAU,iBAAkB;AAG1D,QAAM,EAAE,eAAe,aAAa,QAAI,gCAAkB;AAAA,IACzD,UAAU;AAAA,IACV,YAAY,2BAA2B,CAAC;AAAA,EACzC,CAAE;AAEF,MAAK,CAAE,kBAAmB;AACzB,WAAO;AAAA,EACR;AAQA,QAAM,uBAAuB,MAAM;AAGlC,UAAM,wBAAwB;AAI9B,QAAK,CAAE,yBAAyB,OAAO,uBAAwB;AAG9D,kBAAa,uBAAuB,qBAAsB;AAAA,IAC3D;AACA,6BAA0B,IAAK;AAAA,EAChC;AAEA,QAAM,6BACL,CAAE,2BAA4B,CAAE,uBAAwB;AACvD,QAAK,CAAE,wBAAyB;AAC/B;AAAA,IACD;AACA,0BAA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