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

/**
 * Element Sub-settings: General settings.
 *
 * @param {Object} props The element props.
 * @since 2.0.0
 * @return {Element} The rendered block settings.
 */
const BlockSettings = memo( ( props ) => {

	// Destructure the required props.
	const { clientId, setAttributes, attributes } = props;

	const {
		fieldPlaceholder,
	} = attributes;

	return (
		<>
			<InspectorControls group='settings'>
				<ToolsPanel
					label={ __( 'Form Field Wrapper', 'spectra-pro' ) }
					resetAll={ () => {
						setAttributes( {
							fieldPlaceholder: '',
						} );
					} }
					panelId={ clientId }
				>
					<ToolsPanelItem
						hasValue={ () => !! fieldPlaceholder }
						label={ __( 'Field Placeholder', 'spectra-pro' ) }
						panelId={ clientId }
						onDeselect={ () => setAttributes( { fieldPlaceholder: '' } ) }
						isShownByDefault
					>
						<TextControl
							__next40pxDefaultSize
							__nextHasNoMarginBottom
							label={ __( 'Field Placeholder', 'spectra-pro' ) }
							value={ fieldPlaceholder }
							onChange={ ( value ) => setAttributes( { fieldPlaceholder: value } ) }
							placeholder={ __( 'Enter input placeholder', 'spectra-pro' ) }
							help={ __( 'Custom placeholder text for this field', 'spectra-pro' ) }
						/>
					</ToolsPanelItem>
				</ToolsPanel>
			</InspectorControls>
		</>
	);
} );

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

export default memo( Settings );
