{"version":3,"file":"use-handle-link-change.mjs","names":["blockEditorStore","stripHTML"],"sources":["../../../src/navigation-link/shared/use-handle-link-change.js"],"sourcesContent":["import { 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';\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\t// The sidebar link picker and the page creator send no link settings,\n\t\t\t// so only apply the setting when the change includes it.\n\t\t\tconst settingsUpdateAttributes =\n\t\t\t\tupdatedLink.opensInNewTab !== undefined\n\t\t\t\t\t? { opensInNewTab: updatedLink.opensInNewTab }\n\t\t\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\t...settingsUpdateAttributes,\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...settingsUpdateAttributes,\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}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