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

/**
 * Internal dependencies.
 */
import CustomSelectControl from '@spectra-pro-components/CustomSelectControl';
import PostTypeSelector from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/PostTypeSelector';
import PostSelector from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/PostSelector';
import MetaField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/MetaField';
import AuthorInfoField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/AuthorInfoField';
import ArchiveField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/ArchiveField';

/**
 * List of available source fields for post-related data sources.
 *
 * Defines the options for the source field select control, grouped by category
 * (Post and Author) with labels and values.
 *
 * @constant {Object[]}
 */
const POST_FIELDS = [
	{
		label: __( 'Choose', 'spectra-pro' ),
		value: '',
	},
	{
		label: __( 'Post', 'spectra-pro' ),
		options: [
			{ label: __( 'Featured Image', 'spectra-pro' ), value: 'featured_image' },
			{ label: __( 'Post Custom Field', 'spectra-pro' ), value: 'custom_field' },
		],
	},
	{
		label: __( 'Author', 'spectra-pro' ),
		options: [
			{ value: 'avatar', label: __( 'Avatar (Profile Image)', 'spectra-pro' ) },
			{ value: 'author_info', label: __( 'Author Custom Field', 'spectra-pro' ) },
		],
	},
];

/**
 * Renders controls for selecting and configuring a source field for dynamic image content.
 *
 * Displays a select control for choosing a source field (e.g., Featured Image, Custom Field)
 * and additional fields based on the selected source and field (e.g., Post Selector, Meta Field).
 * Updates the dynamic content settings with the selected configurations.
 *
 * @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 source field configuration controls.
 */
const SourceField = ( { settings, updateSetting } ) => (
	<>
		{ /* Post type selector for post_type source */ }
		{ settings.source.type === 'post_type' && (
			<PostTypeSelector settings={ settings } updateSetting={ updateSetting } />
		) }

		{ /* Post selector for post_type source */ }
		{ settings.source.type === 'post_type' && (
			<PostSelector settings={ settings } updateSetting={ updateSetting } />
		) }

		{ /* Source field controls for post_type or current_post sources */ }
		{ ( settings.source.type === 'post_type' || settings.source.type === 'current_post' ) && (
			<>
				{ /* Select control for choosing the source field */ }
				<CustomSelectControl
					label={ __( 'Source Field', 'spectra-pro' ) }
					value={ settings.source.field }
					options={ POST_FIELDS }
					onChange={ ( newValue ) => updateSetting( 'source.field', newValue ) }
				/>
				{ /* Meta field configuration for custom_field selection */ }
				{ settings.source.field === 'custom_field' && (
					<MetaField settings={ settings } updateSetting={ updateSetting } type="image" />
				) }
				{ /* Author info configuration for author_info selection */ }
				{ settings.source.field === 'author_info' && (
					<AuthorInfoField settings={ settings } updateSetting={ updateSetting } type="image" />
				) }
			</>
		) }

		{ /* Archive field configuration for archive_meta field */ }
		{ settings.source.field === 'archive_meta' && (
			<ArchiveField settings={ settings } updateSetting={ updateSetting } type="image" />
		) }
	</>
);

export default SourceField;
