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

/**
 * Options for controlling sticky post behavior in the query.
 *
 * Defines how sticky posts are handled in the post query results.
 *
 * @type {Array<Object>}
 */
const stickyOptions = [
	{ label: __( 'Include', 'spectra-pro' ), value: '' }, // Default behavior: sticky posts are included naturally.
	{ label: __( 'Exclude', 'spectra-pro' ), value: 'exclude' }, // Exclude sticky posts from the results.
	{ label: __( 'Ignore', 'spectra-pro' ), value: 'ignore' }, // Ignore sticky status, treat as regular posts.
	{ label: __( 'Only', 'spectra-pro' ), value: 'only' }, // Show only sticky posts.
];

/**
 * Generates helper text explaining the selected sticky post behavior.
 *
 * Provides clear and concise descriptions to help users understand
 * the impact of their selection.
 *
 * @param {string} type The selected sticky post handling method.
 * @return {string} The corresponding help text.
 */
const renderHelpText = ( type ) => {
	switch ( type ) {
		case 'exclude':
			return __(
				'Sticky posts will be hidden from the results.',
				'spectra-pro'
			);
		case 'ignore':
			return __(
				'Sticky posts will appear like regular posts, without special placement.',
				'spectra-pro'
			);
		case 'only':
			return __(
				'Only sticky posts will be shown in the results.',
				'spectra-pro'
			);
		default:
			return __(
				'Sticky posts will be displayed at the top of the results.',
				'spectra-pro'
			);
	}
};

/**
 * A control component for managing sticky post behavior in a query.
 *
 * This component renders a dropdown menu allowing users to specify how sticky posts
 * should be handled in the query results. Options include including, excluding,
 * ignoring, or showing only sticky posts. The selected value is passed directly to
 * the `onChange` callback to update the query.
 *
 * @param {Object}   props          The component props.
 * @param {string}   props.value    The current sticky post setting ('' | 'exclude' | 'ignore' | 'only').
 * @param {Function} props.onChange Callback function to update the sticky value in the query.
 * @return {Element} The rendered SelectControl component.
 */
const StickyControl = ( { value, onChange } ) => {
	return (
		<SelectControl
			__nextHasNoMarginBottom
			__next40pxDefaultSize
			label={ __( 'Sticky posts', 'spectra-pro' ) }
			options={ stickyOptions }
			value={ value }
			onChange={ onChange }
			help={ renderHelpText( value ) }
		/>
	);
};

export default StickyControl;
