/**
 * External dependencies.
 */
import { InspectorControls } from '@wordpress/block-editor';
import {
	__experimentalToolsPanel as ToolsPanel,
	__experimentalToolsPanelItem as ToolsPanelItem,
	TextControl,
} from '@wordpress/components';
import { memo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies.
 */
import InspectorColor from '@spectra-components/inspector-color';

/**
 * Element Sub-settings: General settings.
 *
 * @param {Object} props The element props.
 * @since x.x.x
 * @return {Element} The rendered block settings.
 */
const BlockSettings = memo( ( props ) => {
	// Destructure the required props.
	const {
		setAttributes,
		attributes: { placeholder },
	} = props;

	const resetAllSettings = () => {
		setAttributes( {
			placeholder: '',
		} );
	};

	const defaultPlaceholder = '';

	return (
		<InspectorControls group="settings">
			{ /* This ToolsPanel contains general settings for the block:
				- Select Placeholder
			*/ }
			<ToolsPanel
				label={ __( 'General', 'spectra-pro' ) }
				resetAll={ resetAllSettings }
			>
				{ /* This ToolsPanelItem will reset when the following condition is met:
					- The "placeholder" attribute is set to "Select". Default: 'Select'.
				*/ }
				<ToolsPanelItem
					hasValue={ () =>
						!! placeholder && defaultPlaceholder !== placeholder
					}
					label={ __( 'Select Placeholder', 'spectra-pro' ) }
					onDeselect={ () =>
						setAttributes( { placeholder: defaultPlaceholder } )
					}
					resetAllFilter={ () => ( {
						placeholder: defaultPlaceholder,
					} ) }
					isShownByDefault
				>
					<TextControl
						__next40pxDefaultSize
						__nextHasNoMarginBottom
						label={ __( 'Placeholder', 'spectra-pro' ) }
						value={ placeholder }
						onChange={ ( value ) => {
							setAttributes( { placeholder: value } );
						} }
					/>
				</ToolsPanelItem>
			</ToolsPanel>
		</InspectorControls>
	);
} );

/**
 * The color settings.
 *
 * @since x.x.x
 *
 * @param {Object} props The element props.
 * @return {Element} The rendered settings.
 */
const ColorSettings = memo( ( props ) => {
	const {
		clientId,
		setAttributes,
		attributes: {
			textColorHover,
			backgroundColorHover,
			backgroundGradientHover,
		},
	} = props;

	return (
		<InspectorColor
			settings={ [
				{
					colorValue: textColorHover,
					label: __( 'Text Focus/Hover', 'spectra-pro' ),
					onColorChange: ( value ) =>
						setAttributes( { textColorHover: value } ),
					resetAllFilter: () =>
						setAttributes( { textColorHover: undefined } ),
				},
				{
					colorValue: backgroundColorHover,
					gradientValue: backgroundGradientHover,
					label: __( 'Background Focus/Hover', 'spectra-pro' ),
					onColorChange: ( value ) =>
						setAttributes( { backgroundColorHover: value } ),
					onGradientChange: ( value ) =>
						setAttributes( { backgroundGradientHover: value } ),
					resetAllFilter: () =>
						setAttributes( {
							backgroundColorHover: undefined,
							backgroundGradientHover: undefined,
						} ),
				},
			] }
			panelId={ clientId }
		/>
	);
} );

/**
 * The Editor settings.
 *
 * @param {Object} props The element props.
 * @since x.x.x
 * @return {Element} The rendered settings.
 */
const Settings = ( props ) => (
	<>
		<BlockSettings { ...{ ...props } } />
		<ColorSettings { ...{ ...props } } />
	</>
);

export default memo( Settings );
