{
  "version": 3,
  "sources": ["../../../src/navigation-link/shared/use-handle-link-change.js"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { escapeHTML } from '@wordpress/escape-html';\n\n/**\n * Internal dependencies\n */\nimport { updateAttributes } from './update-attributes';\nimport { useEntityBinding } from './use-entity-binding';\n\n/**\n * Custom hook that returns a callback for handling link selection/change.\n * Manages the transition between entity links and custom links,\n * including proper binding creation and cleanup.\n *\n * @param {Object}   options                 - Configuration options\n * @param {string}   options.clientId        - Block client ID\n * @param {Object}   options.attributes      - Current block attributes\n * @param {Function} options.setAttributes   - Standard setAttribute function\n * @param {boolean}  options.allowTextUpdate - Whether this control can update the link text\n * @return {Function} Callback function to handle link changes\n */\nexport function useHandleLinkChange( {\n\tclientId,\n\tattributes,\n\tsetAttributes,\n\tallowTextUpdate = false,\n} ) {\n\tconst { updateBlockAttributes } = useDispatch( blockEditorStore );\n\tconst { hasUrlBinding, createBinding, clearBinding } = useEntityBinding( {\n\t\tclientId,\n\t\tattributes,\n\t} );\n\n\treturn useCallback(\n\t\t( updatedLink ) => {\n\t\t\tif ( ! updatedLink ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst attrs = {\n\t\t\t\turl: updatedLink.url,\n\t\t\t\tkind: updatedLink.kind,\n\t\t\t\ttype: updatedLink.type,\n\t\t\t\tid: updatedLink.id,\n\t\t\t};\n\n\t\t\tconst currentText = attributes.label\n\t\t\t\t? stripHTML( attributes.label )\n\t\t\t\t: '';\n\t\t\tconst updatedText = updatedLink.title ?? '';\n\t\t\tconst hasTextUpdate =\n\t\t\t\tallowTextUpdate &&\n\t\t\t\tupdatedLink.title !== undefined &&\n\t\t\t\tupdatedText !== currentText;\n\t\t\tconst textUpdateAttributes = hasTextUpdate\n\t\t\t\t? { label: escapeHTML( updatedText ) }\n\t\t\t\t: {};\n\n\t\t\tif (\n\t\t\t\t! attributes.label ||\n\t\t\t\tattributes.label === '' ||\n\t\t\t\thasTextUpdate\n\t\t\t) {\n\t\t\t\tattrs.title = updatedLink.title;\n\t\t\t}\n\t\t\t// Check if transitioning from entity to custom link\n\t\t\tconst willBeCustomLink = ! updatedLink.id && hasUrlBinding;\n\n\t\t\tif ( willBeCustomLink ) {\n\t\t\t\t// Clear the binding first\n\t\t\t\tclearBinding();\n\n\t\t\t\t// Use direct store dispatch to bypass setBoundAttributes wrapper\n\t\t\t\t// which prevents updates to bound attributes.\n\t\t\t\tupdateBlockAttributes( clientId, {\n\t\t\t\t\turl: updatedLink.url,\n\t\t\t\t\tkind: 'custom',\n\t\t\t\t\ttype: 'custom',\n\t\t\t\t\tid: undefined,\n\t\t\t\t\t...textUpdateAttributes,\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Normal flow for entity links or unbound custom links\n\t\t\t\tconst { isEntityLink, attributes: updatedAttributes } =\n\t\t\t\t\tupdateAttributes( attrs, setAttributes, attributes );\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\tif ( Object.keys( textUpdateAttributes ).length ) {\n\t\t\t\t\tupdateBlockAttributes( clientId, textUpdateAttributes );\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tattributes,\n\t\t\tallowTextUpdate,\n\t\t\tclientId,\n\t\t\thasUrlBinding,\n\t\t\tcreateBinding,\n\t\t\tclearBinding,\n\t\t\tsetAttributes,\n\t\t\tupdateBlockAttributes,\n\t\t]\n\t);\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;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