/**
 * External dependencies.
 */
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies.
 */
import CustomSelectControl from '@spectra-pro-components/CustomSelectControl';

/**
 * List of available data sources for dynamic image content.
 *
 * Defines the options for the data source select control, including labels and values.
 *
 * @since x.x.x
 *
 * @constant {Object[]}
 */
const DATA_SOURCES = [
	{
		label: __( 'Choose', 'spectra-pro' ),
		value: '',
	},
	{
		label: __( 'Current Post', 'spectra-pro' ),
		value: 'current_post',
	},
	{
		label: __( 'Post Type', 'spectra-pro' ),
		value: 'post_type',
	},
	{
		label: __( 'Archive', 'spectra-pro' ),
		options: [ { label: __( 'Archive Custom Field', 'spectra-pro' ), value: 'archive' } ],
	},
	{
		label: __( 'Site', 'spectra-pro' ),
		options: [ { label: __( 'Site Logo URL', 'spectra-pro' ), value: 'site' } ],
	},
];

/**
 * Renders a select control for choosing a data source for dynamic image content.
 *
 * Allows users to select a data source (e.g., Current Post, Post Type) and updates
 * the dynamic content settings with the selected source and appropriate default field.
 *
 * @since x.x.x
 * @param {Object}   props               - The component props.
 * @param {Object}   props.settings      - The dynamic content settings from the context.
 * @param {Function} props.updateSetting - Function to update settings in the context.
 * @return {Element} The data source select control.
 */
const DataSourceField = ( { settings, updateSetting } ) => (
	<CustomSelectControl
		label={ __( 'Data Source', 'spectra-pro' ) }
		value={ settings.source.type }
		options={ DATA_SOURCES }
		onChange={ ( newValue ) => {
			// Update the source type in settings.
			updateSetting( 'source.type', newValue );

			// Set default field based on selected source.
			if ( newValue === 'site' ) {
				updateSetting( 'source.field', 'site_logo' );
			} else if ( newValue === 'archive' ) {
				updateSetting( 'source.field', 'archive_meta' );
			} else {
				updateSetting( 'source.field', 'featured_image' );
			}
		} }
	/>
);

export default DataSourceField;
