/**
 * Internal dependencies.
 */
import { isAllowedBlockForImage, isAllowedBlockForLink } from '@spectra-pro-extensions/dynamic-content/utils/helpers';

/**
 * Extends block attributes with properties for dynamic image and link content.
 *
 * @since x.x.x
 *
 * @param {Object} settings - The original block settings object.
 * @param {string} name     - The block name (e.g., 'core/image', 'spectra/button').
 * @return {Object} The modified settings object with added dynamic content attributes.
 */
const withDynamicContentAttributes = ( settings, name ) => {
	// Create a copy of the existing attributes to avoid mutating the original.
	let newAttributes = { ...settings.attributes };

	// Add dynamic image attributes for supported blocks.
	if ( isAllowedBlockForImage( { name } ) ) {
		newAttributes = {
			...newAttributes,
			// Enable/disable flag for dynamic image content.
			spectraProEnableDynamicImage: {
				type: 'boolean',
				default: false,
			},
			// Stores dynamic image configuration (e.g., source, field).
			spectraProDynamicImage: {
				type: 'object',
				default: null,
			},
		};
	}

	// Add dynamic link attributes for supported blocks.
	if ( isAllowedBlockForLink( { name } ) ) {
		newAttributes = {
			...newAttributes,
			// Enable/disable flag for dynamic link content.
			spectraProEnableDynamicLink: {
				type: 'boolean',
				default: false,
			},
			// Stores dynamic link configuration (e.g., source, field).
			spectraProDynamicLink: {
				type: 'object',
				default: null,
			},
		};
	}

	// Return the updated settings with new attributes.
	return {
		...settings,
		attributes: newAttributes,
	};
};

export default withDynamicContentAttributes;
