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

/**
 * Internal dependencies.
 */
import CustomSelectControl from '@spectra-pro-components/CustomSelectControl';
import { getSourceFields } from '@spectra-pro-extensions/dynamic-content/utils/helpers';
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 RequestParameterField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/RequestParameterField';
import ArchiveField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/ArchiveField';
import UserField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/UserField';
import ShortcodeField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/ShortcodeField';
import FeatureImageField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/FeatureImageField';
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 DateField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/DateField';
import TimeField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/TimeField';
import CommentsNumberField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/CommentsNumberField';
import TermsField from '@spectra-pro-extensions/dynamic-content/components/Fields/SourceField/PostFields/TermsField';

/**
 * Renders controls for selecting and configuring a source field for dynamic content.
 *
 * Displays a select control for choosing a source 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 } ) => {
	// Get available source fields based on the selected source type.
	const sourceFields = getSourceFields( settings.source.type );

	// Dynamic label based on data source.
	let label = __( 'Source Field', 'spectra-pro' );
	switch ( settings.source.type ) {
		case 'request_parameter':
			label = __( 'Type', 'spectra-pro' );
			break;
		case 'shortcode':
			label = __( 'Shortcode', 'spectra-pro' );
			break;
		case 'current_user':
			label = __( 'User Field', 'spectra-pro' );
			break;
		default:
			break;
	}
	return (
		<>
			{ /* 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 select for non-shortcode and non-current_user sources */ }
			{ settings.source.type !== 'shortcode' && settings.source.type !== 'current_user' && (
				<CustomSelectControl
					label={ label }
					value={ settings.source.field }
					options={ sourceFields }
					onChange={ ( newValue ) => updateSetting( 'source.field', newValue ) }
				/>
			) }

			{ /* Additional fields for post_type or current_post sources */ }
			{ ( settings.source.type === 'post_type' || settings.source.type === 'current_post' ) && (
				<>
					{ /* Meta field configuration for custom_field selection */ }
					{ settings.source.field === 'custom_field' && (
						<MetaField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Terms field configuration for post_terms selection */ }
					{ settings.source.field === 'post_terms' && (
						<TermsField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Comments number field configuration for comments_number selection */ }
					{ settings.source.field === 'comments_number' && (
						<CommentsNumberField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Author info configuration for author_info selection */ }
					{ settings.source.field === 'author_info' && (
						<AuthorInfoField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Date field configuration for post_date selection */ }
					{ settings.source.field === 'post_date' && (
						<DateField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Time field configuration for post_time selection */ }
					{ settings.source.field === 'post_time' && (
						<TimeField settings={ settings } updateSetting={ updateSetting } />
					) }

					{ /* Featured image field configuration for featured_image selection */ }
					{ settings.source.field === 'featured_image' && (
						<FeatureImageField settings={ settings } updateSetting={ updateSetting } />
					) }
				</>
			) }

			{ /* Shortcode field configuration for shortcode source */ }
			{ settings.source.type === 'shortcode' && (
				<ShortcodeField settings={ settings } updateSetting={ updateSetting } />
			) }

			{ /* User field configuration for current_user source */ }
			{ settings.source.type === 'current_user' && (
				<UserField settings={ settings } updateSetting={ updateSetting } />
			) }

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

			{ /* Request parameter field configuration for request_parameter source */ }
			{ settings.source.type === 'request_parameter' && (
				<RequestParameterField settings={ settings } updateSetting={ updateSetting } />
			) }
		</>
	);
};

export default SourceField;
